从二维到一维,如何在混合模型中传递第二个随机效应 [Python, Statsmodel]

From 2D to 1D, how to pass a second random effect in mixed model [Python, Statsmodel]

构建此问题:

假设我有一个这样的数据框:

import pandas as pd
d = {'y':[1.2,2.41,3.12,4.76],'x':['A','B'],'r1':['a','b','c','d'],'r2':['a2','b2','c2','d2']}
df = pd.DataFrame(d)

y 是连续变量。 x 是分类的并且是固定分量。它是二进制的。 r1, r2 是绝对的。它们是随机成分。

我会把它传递给混合模型:

import statsmodels.formula.api as smf
md = smf.mixedlm("y ~ x", df, groups=df["r1"], re_formula="~ r1")

这很好用。

但现在我想添加第二个随机变量,但这只能作为一维数组来完成... 我不知道如何重新排列我将 2 个变量传递给 groups 的数据,作为一维数组

总结:如何以这种方式重新排列数据帧,以便我可以将 2 个变量作为一维数组传递给 groups?请显示此语法。

所以你需要crossed random effects models.

来自文档:

Statsmodels MixedLM handles most non-crossed random effects models, and some crossed models. To include crossed random effects in a model, it is necessary to treat the entire dataset as a single group. The variance components arguments to the model can then be used to define models with various combinations of crossed and non-crossed random effects.


由于需要一个没有独立组的交叉模型,所以需要将所有人放在同一个组中,并使用方差分量指定随机效应。

import pandas as pd                                                                                                        
import statsmodels.api as sm                                                                                               

d = {'y':[1,2,3,4],'x':[1,2,3,4],'r1':[1,2,3,4],'r2':[1,2,3,4]}
df = pd.DataFrame(d)                                                                                                          
df["group"] = 1    # all in the case group                                                                                                        

vcf = {"r1": "0 + C(r1)", "r2": "0 + C(r2)"}  # formula                                                        
model = sm.MixedLM.from_formula("y ~ x", groups="group",                                                    
                                vc_formula=vcf, re_formula="~r1", data=df)                                                   
result = model.fit()