如何添加多个随机效果?[Python,统计模型] [英] How to add multiple random effects? [Python, Statsmodel]

查看:50
本文介绍了如何添加多个随机效果?[Python,统计模型]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 Python 完成令人惊叹的混合模型教程统计模型库.

I've been walking through the amazing tutorial on mixed models in python using the statsmodel libary.

我已经指定了一个模型:

I've specified a model as such:

md = smf.mixedlm("Y~ X", df, groups=df["random"])
mdf = md.fit()

但现在我想添加第二个随机变量.我认为这将非常简单,只需将 list 传递到我的数据框 df 中:

but now i want to add a second random variable. I thought it would be pretty straight forward, by just passing a list into my dataframe df as such:

md = smf.mixedlm("Y~ X", df, groups=df[["random1","random2"]])
mdf = md.fit()

但这行不通.

如何在混合模型中添加第二个随机变量?

推荐答案

当你这样做时,你并没有传递一个列表:

You're not passing it a list when you do:

groups=df[["random1","random2"]]

您正在向它传递一个由这两列构成的新数据框.

You're passing it a new data frame constructed of those two columns.

见:

import pandas as pd

df = pd.read_csv('something.csv')

print(type(df['a']))

>>><class 'pandas.core.series.Series'>

print(type(df[['a', 'b']]))

>>><class 'pandas.core.frame.DataFrame'>

print(type([df['a'], df['b']]))

>>><class 'list'>

如果您查看 mixlm 的文档(https://www.statsmodels.org/devel/generated/statsmodels.regression.mixed_linear_model.MixedLM.html),你可以看到groups属性只接受一维数组的输入,这就是为什么你可以't 传递一个数据帧.文档还说:

If you look at the documentation for the mixedlm (https://www.statsmodels.org/devel/generated/statsmodels.regression.mixed_linear_model.MixedLM.html), you can see that the groups attribute only takes inputs that are 1d array like, which is why you can't pass it a data frame. The documentation also says:

要在模型中包含交叉随机效应,必须将整个数据集视为一个组.

To include crossed random effects in a model, it is necessary to treat the entire dataset as a single group.

因此,您需要将整个数据集放入一维数组结构中,以便将第二个随机变量传递给它.

So you need to get the entire dataset into a structure that is 1D array like in order pass it a second random variable.

这篇关于如何添加多个随机效果?[Python,统计模型]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆