欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

第十四周作业

程序员文章站 2022-07-01 18:23:02
...

Anscombe's quartet

Anscombe's quartet comprises of four datasets, and is rather famous. Why? You'll find out in this exercise.


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)
(1)
%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")
anascombe = pd.read_csv('data/anscombe.csv')
print('The average x is {:.3f}'.format(anascombe['x'].mean()))
print('The variance of x is {:.3f}'.format(anascombe['x'].var()))
print('The average y is {:.3f}'.format(anascombe['y'].mean()))
print('The variance of y is {:.3f}'.format(anascombe['y'].var()))

第十四周作业

(2)

print('The correlation coefficient between x and y is')
r = anascombe['x'].corr(anascombe['y'])
print(r)

第十四周作业

(3)

x = anascombe.x
y = anascombe.y
X = sm.add_constant(x)
model = sm.OLS(y,X)
results = model.fit()
print('beta1, beta0 = ', results.params[0],results.params[1])

第十四周作业


Part 2

Using Seaborn, visualize all four datasets.

hint: use sns.FacetGrid combined with plt.scatter

sns.FacetGrid(data=anascombe, col='dataset', col_wrap=2).map(plt.scatter, 'x', 'y')
plt.show()
第十四周作业