Python:如何评估 StatsModels 中的残差?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35417111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:25:19  来源:igfitidea点击:

Python: How to evaluate the residuals in StatsModels?

pythonpandasstatsmodelspatsy

提问by DanielTheRocketMan

I want to evaluate the residuals: (y-hat y).

我想评估残差:(y-hat y)。

I know how to do that:

我知道该怎么做:

df = pd.read_csv('myFile', delim_whitespace = True, header = None)
df.columns = ['column1', 'column2']
y, X = ps.dmatrices('column1 ~ column2',data = df, return_type = 'dataframe')
model = sm.OLS(y,X)
results = model.fit()
predictedValues = results.predict()
#print predictedValues
yData = df.as_matrix(columns = ['column1'])
res = yData - predictedValues

I wonder if there is a Method to do this (?).

我想知道是否有一种方法可以做到这一点(?)。

采纳答案by TomAugspurger

That's stored in the residattribute of the Results class

这存储在Results 类resid属性中

Likewise there's a results.fittedvaluesmethod, so you don't need the results.predict().

同样有一个results.fittedvalues方法,所以你不需要results.predict().

回答by SciPy

Normality of the residuals

残差的正态性

Option 1: Jarque-Bera test

选项 1:Jarque-Bera 测试

name = ['Jarque-Bera', 'Chi^2 two-tail prob.', 'Skew', 'Kurtosis']
test = sms.jarque_bera(results.resid)
lzip(name, test)

Out:

出去:

[('Jarque-Bera', 3.3936080248431666),
 ('Chi^2 two-tail prob.', 0.1832683123166337),
 ('Skew', -0.48658034311223375),
 ('Kurtosis', 3.003417757881633)]
Omni test:

Option 2: Omni test

选项 2:Omni 测试

name = ['Chi^2', 'Two-tail probability']
test = sms.omni_normtest(results.resid)
lzip(name, test)

Out:

出去:

[('Chi^2', 3.713437811597181), ('Two-tail probability', 0.15618424580304824)]