【第十四周】Anscombe's quartet

本次要求出均值、方差、相关系数和线性回归的β1、β2

代码如下:

import pandas as pd
import scipy as sp
import statsmodels.api as sm
from statsmodels.formula.api import ols
anascombe = pd.read_csv('anscombe.csv')
anascombe.head()
print(anascombe.groupby('dataset')['x'].mean())
print(anascombe.groupby('dataset')['y'].mean())
print("--------------------")
print(anascombe.groupby('dataset')['x'].var())
print(anascombe.groupby('dataset')['y'].var())
print("--------------------")
y1 = anascombe.y[0:10].values
y2 = anascombe.y[11:21].values
y3 = anascombe.y[22:32].values
y4 = anascombe.y[33:43].values
x1 = anascombe.x[0:10].values
x2 = anascombe.x[11:21].values
x3 = anascombe.x[22:32].values
x4 = anascombe.x[33:43].values
print(sp.stats.pearsonr(x1, y1)[0])
print(sp.stats.pearsonr(x2, y2)[0])
print(sp.stats.pearsonr(x3, y3)[0])
print(sp.stats.pearsonr(x4, y4)[0])
print("--------------------")
x12=sm.add_constant(x1)
y12=sm.OLS(y1,x12)
y12=y12.fit()
print("I:β0 = "+str(y12.params[0])+" β1="+str(y12.params[1]))
x22=sm.add_constant(x2)
y22=sm.OLS(y2,x22)
y22=y22.fit()
print("II:β0 = "+str(y22.params[0])+" β1="+str(y22.params[1]))
x32=sm.add_constant(x3)
y32=sm.OLS(y3,x32)
y32=y32.fit()
print("III:β0 = "+str(y32.params[0])+" β1="+str(y32.params[1]))
x42=sm.add_constant(x4)
y42=sm.OLS(y1,x42)
y42=y42.fit()
print("I:β0 = "+str(y42.params[0])+" β1="+str(y42.params[1]))

结果如下:

dataset
I      9.0
II     9.0
III    9.0
IV     9.0
Name: x, dtype: float64
dataset
I      7.500909
II     7.500909
III    7.500000
IV     7.500909
Name: y, dtype: float64
--------------------
dataset
I      11.0
II     11.0
III    11.0
IV     11.0
Name: x, dtype: float64
dataset
I      4.127269
II     4.127629
III    4.122620
IV     4.123249
Name: y, dtype: float64
--------------------
0.797081575906
0.777309302078
0.798563261709
0.814672214693
--------------------
I:β0 = 2.90181818182 β1=0.508636363636
II:β0 = 3.4175974026 β1=0.463766233766
III:β0 = 2.8770995671 β1=0.510627705628
I:β0 = 10.8293939394 β1=-0.345757575758

Process finished with exit code 0

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值