2016-08-25 112 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錢全額退款......

相關問題