是否可以在 Python 中执行 glmm?
Is it possible to do glmm in Python?
是否可以在 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(诚然,不是最近)post,Python 中的 运行 glmms 仍然没有很好的解决方案。但是,如果您只是在寻找 运行 SPSS 测试的免费(而且更灵活!)替代方案,请查看 R 的 lme4 包。您甚至可以使用 rpy2 等包,并且直接从 Python 调用 R,但这可能有点问题。
是否可以在 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(诚然,不是最近)post,Python 中的 运行 glmms 仍然没有很好的解决方案。但是,如果您只是在寻找 运行 SPSS 测试的免费(而且更灵活!)替代方案,请查看 R 的 lme4 包。您甚至可以使用 rpy2 等包,并且直接从 Python 调用 R,但这可能有点问题。