AttributeError:' OLSResults'对象没有属性' norm_resid'

时间:2016-05-19 15:17:10

标签: python numpy statsmodels

当我运行时,我有以下错误:

AttributeError: 'OLSResults' object has no attribute 'norm_resid'

我有最新版本的OLS,因此属性norm_resid应该在那里。

有什么想法吗?

from scipy import stats
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
import matplotlib.pylab as plt
from sklearn import datasets, linear_model
from statsmodels.formula.api import ols
"""
Data Management
"""
data = pd.read_csv("TestExer1-sales-round1.csv")
X_train = data["Advertising"]
Y_train  = data["Sales"]


# use of linregregress
model = ols("Y_train ~ X_train", data).fit()
print(model.summary()) 

plt.plot(X_train,Y_train , 'ro')
plt.plot(X_train, model.fittedvalues, 'b')
plt.legend(['Sales', 'Advertising'])
plt.ylim(0, 70)
plt.xlim(5, 18)
plt.hist(model.norm_resid())
plt.ylabel('Count')
plt.xlabel('Normalized residuals')

plt.xlabel('Temperature')
plt.ylabel('Gas')
plt.title('Before Insulation')

2 个答案:

答案 0 :(得分:2)

我有同样的问题,但以下工作:

plt.hist(model.resid_pearson)

因此,您的解决方案应如下所示:

from scipy import stats
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
import matplotlib.pylab as plt
from sklearn import datasets, linear_model
from statsmodels.formula.api import ols
"""
Data Management
"""
data = pd.read_csv("TestExer1-sales-round1.csv")
X_train = data["Advertising"]
Y_train  = data["Sales"]


# use of linregregress
model = ols("Y_train ~ X_train", data).fit()
print(model.summary()) 

plt.plot(X_train,Y_train , 'ro')
plt.plot(X_train, model.fittedvalues, 'b')
plt.legend(['Sales', 'Advertising'])
plt.ylim(0, 70)
plt.xlim(5, 18)
plt.hist(model.resid_pearson)
plt.ylabel('Count')
plt.xlabel('Normalized residuals')

plt.xlabel('Temperature')
plt.ylabel('Gas')
plt.title('Before Insulation')

使用statsmodel 0.8.0或更高版本时。

注意:皮尔逊残差仅将每个残值除以残差的标准误差。归一化也将每个残差除以所有残差的总和。有关详情,请参阅here

来自docs

答案 1 :(得分:-1)

尝试

plt.hist(model.pearson_resid)