%matplotlib inline import random import numpy as np import scipy as sp import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import statsmodels.api as sm import statsmodels.formula.api as smf sns.set_context("talk")
Anscombe's quartet
Anscombe's quartet comprises of four datasets, and is rather famous. Why? You'll find out in this exercise.
anascombe = pd.read_csv('anscombe.csv')
anascombe.head()
Part 1
For each of the four datasets...
- Compute the mean and variance of both x and y
- Compute the correlation coefficient between x and y
- Compute the linear regression line: y=β0+β1x+ϵy=β0+β1x+ϵ (hint: use statsmodels and look at the Statsmodels notebook)
print(anascombe.groupby('dataset').mean()) print(anascombe.groupby('dataset').var()) print(anascombe.x.corr(anascombe.y))
dataset x y
I 9.0 7.500909
II 9.0 7.500909
III 9.0 7.500000
IV 9.0 7.500909
dataset x y
I 11.0 4.127269
II 11.0 4.127629
III 11.0 4.122620
IV 11.0 4.123249
0.81636624276147
y = anascombe.y X = anascombe.x X = sm.add_constant(X) Linear = sm.OLS(y, X) Linear = Linear.fit() print(Linear.summary())OLS Regression Results
===================================================
Dep. Variable: y R-squared: 0.666
Model: OLS Adj. R-squared: 0.659
Method: Least Squares F-statistic: 83.92
Date: Sun, 10 Jun 2018 Prob (F-statistic): 1.44e-11
Time: 00:10:31 Log-Likelihood: -67.358
No. Observations: 44 AIC: 138.7
Df Residuals: 42 BIC: 142.3
Df Model: 1
Covariance Type: nonrobust
====================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
const 3.0013 0.521 5.765 0.000 1.951 4.052
x 0.4999 0.055 9.161 0.000 0.390 0.610
====================================================
Omnibus: 1.513 Durbin-Watson: 2.327
Prob(Omnibus): 0.469 Jarque-Bera (JB): 0.896
Skew: 0.339 Prob(JB): 0.639
Kurtosis: 3.167 Cond. No. 29.1
====================================================
Part 2
Using Seaborn, visualize all four datasets.
hint: use sns.FacetGrid combined with plt.scatter
g = sns.FacetGrid(anascombe, col='dataset', size=5) g = g.map(plt.scatter, "x", "y") plt.show()