我想构建一个线性混合模型,其中“收益”是响应,“字段”是随机因子,“变异”是固定因子,我在Python的statsmodels包中使用了mixedlm。并且需要计算随机因素的方差分量,我知道需要设置参数“ exog_vc”,但是我不知道如何设置它。
import statsmodels.api as sm
import pandas as pd
yields =[4.10,3.50,3.62,3.14,4.22,2.57,3.31,2.99,3.17,2.75,3.40,2.20,3.26,
3.27,2.93,2.59,3.11,2.38,3.25,3.20,3.09,2.62,3.23,2.37]
variety = [1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6]
field = [1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4]
df = pd.DataFrame({'yields':yields,'variety':variety,'field':field})
X = df.loc[:,['field']]#factor
Y = df['yields']#response
md = sm.MixedLM(Y,X,groups=df['variety'])
#md = sm.MixedLM(Y,X,groups=df['variety'],exog_vc=)#FIXME
mdf = md.fit()
print(mdf.summary())
我想得到以下结果,这些结果是使用minitab获得的。
coefficient
constant 3.094583
variety
1 0.385417
2 0.145417
3 0.107917
4 -0.319583
5 0.395417
6 -0.714583
variance component
source variable
field 0.077919
err 0.028924
total 0.106843