回归模型的假设检验

这里主要使用statsmodels模块。

建模

import numpy as np
import statsmodels as sm
from sklearn.model_selection import train_test_split

profit = pd.read_excel('./Predict to Profit.xlsx')
train, test = train_test_split(profit, test_size = 0.2,random_state=1234)
model = sm.formula.ols('Profit ~ RD_Spend + Administration + Marketing_Spend+ C(State)', data=train).fit()
model.params

结果返回模型的偏回归系数:
image.png

上述代码中,将State变了套在C()中,表示State变量需要进行哑变量处理。类似pandas.get_dummies()函数。

假设检验

主要使用一行代码:

model.summary()

返回结果:
image.png

模型的显著性检验、回归系数的显著性检验、残差独立性检验等都可以从上图中直接得到结果。

赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权,转载请注明出处。
文章名称:《回归模型的假设检验》
文章来自:泰恩数据
文章链接:https://tyne.cc/640.html
本站资源仅供个人学习使用,请勿用于商业用途。

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址