gpt4 book ai didi

python - 是否可以在 Python 中执行 glmm?

转载 作者:太空狗 更新时间:2023-10-30 01:12:25 31 4
gpt4 key购买 nike

是否可以在 Python 中执行 glmm(如 SPSS 中的 GENLINMIXED 分析)?我是 statsmodels 的忠实粉丝,但这个库似乎不支持 glmm...还有其他选择吗?

-编辑-

决定使用 R 和 r2py...

def RunAnalyseMLMlogit(dataset, outcomevars, meeneemvars, randintercept, randslope):

from rpy2.robjects import pandas2ri
from rpy2.robjects.packages import importr
base = importr('base')
stats = importr('stats')
lme4 = importr('lme4')

#data
with SavReaderNp(dataset) as reader_np:
array = reader_np.to_structured_array()

df = pd.DataFrame(array)

variabelen = ' '.join(outcomevars) + ' ~ ' + '+'.join(meeneemvars)
randintercept2 = ['(1|'+i+')' for i in randintercept]
intercept = '+'.join(randintercept2)
randslope2 = ['(1+'+meeneemvars[0]+'|'+i+')' for i in randslope]
slope = ' '.join(randslope2)

pandas2ri.activate()
r_df = pandas2ri.py2ri(df)

#model
#random intercepts + random slopes
if len(randslope) > 0:
formula = variabelen + '+' + intercept + '+' + slope

#only random intercepts
else:
formula = variabelen + '+' + intercept

model = lme4.glmer(formula, data=r_df, family= 'binomial')
resultaat = base.summary(model).rx2('coefficients')
uitkomst = base.summary(model)

return uitkomst

最佳答案

根据 this (诚​​然,不是最近的)帖子,在 Python 中运行 glmms 仍然没有很好的解决方案。但是,如果您只是在寻找一种免费(而且更灵活!)的替代方法来在 SPSS 中运行测试,请查看 R 的 lme4 包。您甚至可以使用诸如 rpy2 之类的包,并直接从Python,但这可能有点问题。

关于python - 是否可以在 Python 中执行 glmm?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43043519/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com