我正试图在Python中实现混合效应逻辑回归。作为比较的一点,我使用了R中
我发现
lme4
包中的glmer
函数。我发现
statsmodels
模块有一个BinomialBayesMixedGLM
, 可以拟合这样的模型。但是,我遇到了一些问题:- 我认为
statsmodels
函数文档并不完全有用或明确,因此我不完全确定如何适当地使用该函数。 - 到目前为止,我的尝试没有产生与我在R中使用
glmer
拟合模型时获得的结果相同的结果。 - 由于它是贝叶斯的,我预计
BinomialBayesMixedGLM
函数不会计算p值,但我似乎无法弄清如何访问参数的完整后验分布。
import os
import pandas as pd
import statsmodels.genmod.bayes_mixed_glm as smgb
titanic = pd.read_csv(os.path.join(os.getcwd(), 'titanic.csv'))
r = {"Pclass": '0 + Pclass'}
mod = smgb.BinomialBayesMixedGLM.from_formula('Survived ~ Age', r, titanic)
fit = mod.fit_map()
fit.summary()
# Type Post. Mean Post. SD SD SD (LB) SD (UB)
# Intercept M 3.1623 0.3616
# Age M -0.0380 0.0061
# Pclass V 0.0754 0.5669 1.078 0.347 3.351
然而,除了年龄对结果的影响,这个句子似乎与我在 R 中使用 glmer(Survived ~ Age + (1 | Pclass), data = titanic, family = "binomial")
得到的结果不符:
Random effects:
Groups Name Variance Std.Dev.
Pclass (Intercept) 0.8563 0.9254
Number of obs: 887, groups: Pclass, 3
Fixed effects:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 0.961780 0.573402 1.677 0.0935 .
Age -0.038708 0.006243 -6.200 5.65e-10 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
我在用Python创建模型的时候出了什么错误?一旦解决了这个问题,我该如何提取后验概率或p值?最后,是否有任何与R中实现更相似的混合效应 logistic 回归的 Python 实现?