使用 statsmodels 创建残差图
creating residual plots using statsmodels
我正在尝试使用 statsmodels.graphics.regressionplots.plot_regress_exog 创建残差图,但我收到错误消息,指出未找到独立的 Var。具体错误如下:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-43dae6c58d5d> in <module>
28
29 #produce regression plots
---> 30 fig = sm.graphics.plot_regress_exog(model,'Pow', fig=fig)
~\Anaconda3\lib\site-packages\statsmodels\graphics\regressionplots.py in plot_regress_exog(results, exog_idx, fig)
218 fig = utils.create_mpl_fig(fig)
219
--> 220 exog_name, exog_idx = utils.maybe_name_or_idx(exog_idx, results.model)
221 results = maybe_unwrap_results(results)
222
~\Anaconda3\lib\site-packages\statsmodels\graphics\utils.py in maybe_name_or_idx(idx, model)
110 else: # assume we've got a string variable
111 exog_name = idx
--> 112 exog_idx = model.exog_names.index(idx)
113
114 return exog_name, exog_idx
ValueError: 'Pow' is not in list
你能帮我解决这个问题吗?
这是我的代码:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
from statsmodels.formula.api import ols
wt_160 = [575, 542, 530, 539, 570]
wt_180 = [565, 593, 590, 579, 610]
wt_200 = [600, 651, 610, 637, 629]
wt_220 = [725, 700, 715, 685, 710]
pwr = [160, 180, 200, 220]
e_rates = wt_160 + wt_180+ wt_200 +wt_220
pow_lvl = (['160 W']*len(wt_160)) + (['180 W']*len(wt_180)) + (['200 W']*len(wt_200)) + (['220 W']*len(wt_220))
df = pd.DataFrame({'Pow': pow_lvl, 'E_Rates':e_rates})
model = ols('E_Rates ~ C(Pow)', df).fit()
anova_result = anova_lm(model, type=2)
print(model.summary())
import statsmodels.api as sm
fig = plt.figure(figsize=(12,8))
#produce regression plots
fig = sm.graphics.plot_regress_exog(model,'Pow', fig=fig)
请注意 Pow
是一个分类预测变量,因此在访问它时您应该考虑它的类别级别。例如,
import statsmodels.api as sm
fig = plt.figure(figsize=(12,8))
#produce regression plots
fig = sm.graphics.plot_regress_exog(model,'C(Pow)[T.180 W]', fig=fig)
会产生
要访问您的预测变量,您可以访问 model
的 params
属性
model.params
>>>
Intercept 551.2
C(Pow)[T.180 W] 36.2
C(Pow)[T.200 W] 74.2
C(Pow)[T.220 W] 155.8
我正在尝试使用 statsmodels.graphics.regressionplots.plot_regress_exog 创建残差图,但我收到错误消息,指出未找到独立的 Var。具体错误如下:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-43dae6c58d5d> in <module>
28
29 #produce regression plots
---> 30 fig = sm.graphics.plot_regress_exog(model,'Pow', fig=fig)
~\Anaconda3\lib\site-packages\statsmodels\graphics\regressionplots.py in plot_regress_exog(results, exog_idx, fig)
218 fig = utils.create_mpl_fig(fig)
219
--> 220 exog_name, exog_idx = utils.maybe_name_or_idx(exog_idx, results.model)
221 results = maybe_unwrap_results(results)
222
~\Anaconda3\lib\site-packages\statsmodels\graphics\utils.py in maybe_name_or_idx(idx, model)
110 else: # assume we've got a string variable
111 exog_name = idx
--> 112 exog_idx = model.exog_names.index(idx)
113
114 return exog_name, exog_idx
ValueError: 'Pow' is not in list
你能帮我解决这个问题吗?
这是我的代码:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
from statsmodels.formula.api import ols
wt_160 = [575, 542, 530, 539, 570]
wt_180 = [565, 593, 590, 579, 610]
wt_200 = [600, 651, 610, 637, 629]
wt_220 = [725, 700, 715, 685, 710]
pwr = [160, 180, 200, 220]
e_rates = wt_160 + wt_180+ wt_200 +wt_220
pow_lvl = (['160 W']*len(wt_160)) + (['180 W']*len(wt_180)) + (['200 W']*len(wt_200)) + (['220 W']*len(wt_220))
df = pd.DataFrame({'Pow': pow_lvl, 'E_Rates':e_rates})
model = ols('E_Rates ~ C(Pow)', df).fit()
anova_result = anova_lm(model, type=2)
print(model.summary())
import statsmodels.api as sm
fig = plt.figure(figsize=(12,8))
#produce regression plots
fig = sm.graphics.plot_regress_exog(model,'Pow', fig=fig)
请注意 Pow
是一个分类预测变量,因此在访问它时您应该考虑它的类别级别。例如,
import statsmodels.api as sm
fig = plt.figure(figsize=(12,8))
#produce regression plots
fig = sm.graphics.plot_regress_exog(model,'C(Pow)[T.180 W]', fig=fig)
会产生
要访问您的预测变量,您可以访问 model
params
属性
model.params
>>>
Intercept 551.2
C(Pow)[T.180 W] 36.2
C(Pow)[T.200 W] 74.2
C(Pow)[T.220 W] 155.8