Pandas & Statsmodels
Anscombe’s quartet
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")
anascombe = pd.read_csv('anscombe.csv')
print(anascombe.head())
Output
dataset x y
0 I 10.0 8.04
1 I 8.0 6.95
2 I 13.0 7.58
3 I 9.0 8.81
4 I 11.0 8.33
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+ϵ (hint: use statsmodels and look at the Statsmodels notebook)
Solution
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")
anascombe = pd.read_csv('anscombe.csv')
# print(anascombe.head())
print('----- 1 -----')
print('Mean of x')
print(anascombe.groupby('dataset')['x'].mean(),end = '\n\n')
print('Mean of y')
print(anascombe.groupby('dataset')['y'].mean(),end = '\n\n')
print('Variance of x')
print(anascombe.groupby(