Statsmodels GLM和OLS公式缺少参数

时间:2017-03-06 07:29:41

标签: python-3.x pandas statistics statsmodels

我正在尝试使用包含分类变量的数据集上的公式运行一般线性模型。当我列出参数时,结果汇总表似乎遗漏了一个变量?

我无法找到特定于glm的文档,显示带有分类变量的输出,但我have for the OLS看起来它应该单独列出每个分类变量。当它这样做时(使用GLM或OLS),它会为每个类别留下一个值。例如:

import statsmodels.formula.api as smf
import pandas as pd
Data = pd.read_csv(root+'/Illisarvik/TestData.csv')
formula = 'Response~Day+Class+Var'
gm = sm.GLM.from_formula(formula=formula, data=Data,
                           family=sm.families.Gaussian()).fit()
ls = smf.ols(formula=formula,data=Data).fit()

print (Data)
print(gm.params)
print(ls.params)



   Day Class       Var  Response
0   D     A  0.533088  0.582931
1   D     B  0.839837  0.075011
2   D     C  1.454716  0.505442
3   D     A  1.455503  0.188945
4   D     B  1.163155  0.144176
5   N     A  1.072238  0.918962
6   N     B  0.815384  0.249160
7   N     C  1.182626  0.520460
8   N     A  1.448843  0.870644
9   N     B  0.653531  0.460177

Intercept     0.625111
Day[T.N]      0.298084
Class[T.B]   -0.439025
Class[T.C]   -0.104725
Var          -0.118662
dtype: float64

Intercept     0.625111
Day[T.N]      0.298084
Class[T.B]   -0.439025
Class[T.C]   -0.104725
Var          -0.118662
dtype: float64
C:/Users/wesle/Dropbox/PhD_Work/Figures/SkeeterEtAlAnalysis.py:55: FutureWarning: sort is deprecated, use sort_values(inplace=True) for INPLACE sorting
  P.sort()

我的模型有问题吗?当我打印完整的汇总表时,同样的问题就出现了:

print(gm.summary())

print(ls.summary())


                 Generalized Linear Model Regression Results                  
==============================================================================
Dep. Variable:               Response   No. Observations:                   10
Model:                            GLM   Df Residuals:                        5
Model Family:                Gaussian   Df Model:                            4
Link Function:               identity   Scale:                 0.0360609978309
Method:                          IRLS   Log-Likelihood:                 5.8891
Date:                Sun, 05 Mar 2017   Deviance:                      0.18030
Time:                        23:26:48   Pearson chi2:                    0.180
No. Iterations:                     2                                         
==============================================================================
                 coef    std err          z      P>|z|      [0.025      0.975]
------------------------------------------------------------------------------
Intercept      0.6251      0.280      2.236      0.025       0.077       1.173
Day[T.N]       0.2981      0.121      2.469      0.014       0.061       0.535
Class[T.B]    -0.4390      0.146     -3.005      0.003      -0.725      -0.153
Class[T.C]    -0.1047      0.170     -0.617      0.537      -0.438       0.228
Var           -0.1187      0.222     -0.535      0.593      -0.553       0.316
==============================================================================

                            OLS Regression Results                            
==============================================================================
Dep. Variable:               Response   R-squared:                       0.764
Model:                            OLS   Adj. R-squared:                  0.576
Method:                 Least Squares   F-statistic:                     4.055
Date:                Sun, 05 Mar 2017   Prob (F-statistic):             0.0784
Time:                        23:26:48   Log-Likelihood:                 5.8891
No. Observations:                  10   AIC:                            -1.778
Df Residuals:                       5   BIC:                           -0.2652
Df Model:                           4                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
Intercept      0.6251      0.280      2.236      0.076      -0.094       1.344
Day[T.N]       0.2981      0.121      2.469      0.057      -0.012       0.608
Class[T.B]    -0.4390      0.146     -3.005      0.030      -0.815      -0.064
Class[T.C]    -0.1047      0.170     -0.617      0.564      -0.541       0.332
Var           -0.1187      0.222     -0.535      0.615      -0.689       0.451
==============================================================================
Omnibus:                        1.493   Durbin-Watson:                   2.699
Prob(Omnibus):                  0.474   Jarque-Bera (JB):                1.068
Skew:                          -0.674   Prob(JB):                        0.586
Kurtosis:                       2.136   Cond. No.                         9.75
==============================================================================

1 个答案:

答案 0 :(得分:1)

这是线性模型工作方式的结果。

例如,就线性模型而言,如果您有分类变量 Day ,则可以将其表示为单个“假”'变量,对于您首先提到的值,设置为0(零),即 D ,另一个为第二个值,即 N 。从统计学上讲,您只能在此分类变量的两个级别的效果之间恢复差异

如果您现在考虑具有两个级别的 Class ,则您有两个虚拟变量,它们表示此分类变量的可用三个级别之间的两个差异。

事实上,在治疗方法上使用正交多项式来扩展这个想法是完全可能的,但那是另一天的事情。

简短的回答是,至少在此帐户中,您的模型没有任何错误。