2016-08-25 113 views
3

我正在尝试使用Python statsmodels线性混合效果模型来拟合具有两个随机截取的模型,例如,两组。我无法弄清楚如何初始化模型,以便我可以做到这一点。如何在Python statsmodels线性混合效果模型中拥有多个组?

下面是这个例子。我有数据,如下所示(从here拍摄):

subject gender scenario attitude frequency 
F1 F 1 pol 213.3 
F1 F 1 inf 204.5 
F1 F 2 pol 285.1 
F1 F 2 inf 259.7 
F1 F 3 pol 203.9 
F1 F 3 inf 286.9 
F1 F 4 pol 250.8 
F1 F 4 inf 276.8 

我想打一个线性混合效应模型有两个随机效应 - 一个学科组和一个方案组。我试图做到这一点:

import statsmodels.api as sm 
model = sm.MixedLM.from_formula("frequency ~ attitude + gender", data, groups=data[['subject', 'scenario']]) 
result = model.fit() 
print result.summary() 

我不断收到此错误:

LinAlgError: Singular matrix 

它,当我在R中,使用lme4与工作正常R.公式为基础的渲染它适合就好了:

politeness.model = lmer(frequency ~ attitude + gender + 
     (1|subject) + (1|scenario), data=politeness) 

我不明白为什么会发生这种情况。当我使用任何一个随机效果/组时,它就可以工作,例如

model = sm.MixedLM.from_formula("frequency ~ attitude + gender", data, groups=data['subject']) 

然后我得到:

    Mixed Linear Model Regression Results 
=============================================================== 
Model:    MixedLM Dependent Variable: frequency 
No. Observations:  83  Method:    REML  
No. Groups:   6   Scale:    850.9456 
Min. group size:  13  Likelihood:   -393.3720 
Max. group size:  14  Converged:   Yes  
Mean group size:  13.8          
--------------------------------------------------------------- 
       Coef. Std.Err. z P>|z| [0.025 0.975] 
--------------------------------------------------------------- 
Intercept  256.785 15.226 16.864 0.000 226.942 286.629 
attitude[T.pol] -19.415 6.407 -3.030 0.002 -31.972 -6.858 
gender[T.M]  -108.325 21.064 -5.143 0.000 -149.610 -67.041 
Intercept RE  603.948 23.995        
=============================================================== 

或者,如果我这样做:

model = sm.MixedLM.from_formula("frequency ~ attitude + gender", data, groups=data['scenario']) 

这是结果我得到:

   Mixed Linear Model Regression Results 
================================================================ 
Model:    MixedLM Dependent Variable: frequency 
No. Observations: 83   Method:    REML  
No. Groups:   7   Scale:     1110.3788 
Min. group size:  11   Likelihood:   -402.5003 
Max. group size:  12   Converged:    Yes  
Mean group size:  11.9          
---------------------------------------------------------------- 
       Coef. Std.Err. z P>|z| [0.025 0.975] 
---------------------------------------------------------------- 
Intercept  256.892 8.120 31.637 0.000 240.977 272.807 
attitude[T.pol] -19.807 7.319 -2.706 0.007 -34.153 -5.462 
gender[T.M]  -108.603 7.319 -14.838 0.000 -122.948 -94.257 
Intercept RE  182.718 5.502        
================================================================ 

我不知道是什么继续。我觉得我错过了统计问题的基础。

+0

请不要包含您的数据的图像,而是包括文本​​。更好的是,以允许可重现的例子的方式包含数据。当你这样做的时候,花点时间仔细阅读你的问题并修复一些缺失的元素。例如,“或者,如果我这样做:”后面是空格。 – lmo

+0

@lmo感谢您的反馈。我不知道如何将表格格式化为类似文本的代码?还修复了原始帖子中的错误。 –

+0

使用statmodel标签查看python问题可能值得看看其他人如何做。 – lmo

回答

7

您正在尝试使用交叉随机效应来拟合模型,即您希望允许各个场景中的主题之间保持一致的变化以及各主题之间的场景之间的一致变化。你可以在statsmodels中使用多个随机效应术语,但它们必须嵌套。拟合交叉(相对于嵌套)随机效应需要更复杂的算法,实际上也是statsmodels documentation说(如2016年8月25日的,加上强调):

Some limitations of the current implementation are that it does not support structure more complex on the residual errors (they are always homoscedastic), and it does not support crossed random effects. We hope to implement these features for the next release.

据我所看到的,你的选择是:(1 )回退到嵌套模型(即适合模型,就好像两种情况都嵌套在主题中)或反之亦然 - 或者尝试两种方法并查看差异是否重要)。 (2)在R内或通过rpy2回落到lme4

与往常一样,你有权要求你支付使用statsmodels钱全额退款......

相关问题