Glmer通过纯R和rmagic产生差异
我试图复制纯R代码的结果,该代码使用lme4 glmer和Pandas->R->glmer。原始输出为Glmer通过纯R和rmagic产生差异,r,pandas,ipython-notebook,rpy2,ipython-magic,R,Pandas,Ipython Notebook,Rpy2,Ipython Magic,我试图复制纯R代码的结果,该代码使用lme4 glmer和Pandas->R->glmer。原始输出为 %load_ext rpy2.ipython %R library(lme4) %R data("respiratory", package = "HSAUR2") %R write.csv(respiratory, 'respiratory2.csv') %R resp <- subset(respiratory, month > "0") %R resp$baseline &l
%load_ext rpy2.ipython
%R library(lme4)
%R data("respiratory", package = "HSAUR2")
%R write.csv(respiratory, 'respiratory2.csv')
%R resp <- subset(respiratory, month > "0")
%R resp$baseline <- rep(subset(respiratory, month == "0")$status,rep(4, 111))
%R resp_lmer <- glmer(status ~ baseline + month + treatment + gender + age + centre + (1 | subject),family = binomial(), data = resp)
%R -o resp_lmer_summary resp_lmer_summary = summary(resp_lmer)
%R -o exp_res exp_res = exp(fixef(resp_lmer))
print resp_lmer_summary
print exp_res
Generalized linear mixed model fit by maximum likelihood (Laplace
Approximation) [glmerMod]
Family: binomial ( logit )
Formula: status ~ baseline + month + treatment + gender + age + centre +
(1 | subject)
Data: resp
AIC BIC logLik deviance df.resid
446.6 487.6 -213.3 426.6 434
Scaled residuals:
Min 1Q Median 3Q Max
-2.5855 -0.3609 0.1430 0.3640 2.2119
Random effects:
Groups Name Variance Std.Dev.
subject (Intercept) 3.779 1.944
Number of obs: 444, groups: subject, 111
Fixed effects:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.65460 0.77621 -2.132 0.0330 *
baselinegood 3.08897 0.59859 5.160 2.46e-07 ***
month.L -0.20348 0.27957 -0.728 0.4667
month.Q -0.02821 0.27907 -0.101 0.9195
month.C -0.35571 0.28085 -1.267 0.2053
treatmenttreatment 2.16620 0.55157 3.927 8.59e-05 ***
gendermale 0.23836 0.66606 0.358 0.7204
age -0.02557 0.01994 -1.283 0.1997
centre2 1.03850 0.54182 1.917 0.0553 .
...
另一方面,当我通过Pandas读取文件时,通过rmagic将其传递给glmer to R,我得到
import pandas as pd
df = pd.read_csv('respiratory2.csv',index_col=0)
baseline = df[df['month'] == 0][['subject','status']].set_index('subject')
df['status'] = (df['status'] == 'good').astype(int)
df['baseline'] = df.apply(lambda x: baseline.ix[x['subject']],axis=1)
df['centre'] = df['centre'].astype(str)
%R -i df
%R resp_lmer <- glmer(status ~ baseline + month + treatment + gender + age + centre + (1 | subject),family = binomial(), data = df)
%R -o res res = summary(resp_lmer)
%R -o exp_res exp_res = exp(fixef(resp_lmer))
print res
结果有些不同。当R读取文件本身时,它将月份转换为一种称为“顺序因子”的东西;然而,从Pandas->R中,这种类型被视为数值,也许这就是区别?我相信我能够正确地复制派生列基线,但是我必须将状态转换为1/0数值,而纯R可以将此列作为字符串(好/差)使用
注意:更正-我错过了Python部分中的过滤条件,其中只取了月份>0。一旦这样做了
df = df[df['month'] > 0]
然后治疗治疗系数为2.16,接近纯R。R仍然显示正基线良好,而熊猫->R显示负系数基线较差,但我猜这是一个微小的差异 是的,
month
是R中的一个有序因子,因此,glmer
使用多项式对比度。相比之下,month
是rmagic输出中的一个连续变量。什么是多项式对比?这个月是不是自己乘以2,3。。时代?在将数据交给R之前,我可以通过自己在Pandas中执行乘法来模拟这一点吗?如果您想查看由多项式对比度建立的虚拟变量,请查看zapsmall(model.matrix(~f,d=data.frame(f=ordered(1:4)))
。您应该能够用模型中的相应值替换三个数值预测值,以替换您的月
有序变量。PS问题不是“R vs.rpy2”;这是因为将数据作为数字而不是有序因子读回。(如果你在R中写入然后读取数据,你也会以同样的方式丢失信息。)我做的简短浏览表明,pandas
没有太多处理分类变量的基础设施(我可能错了!),因此自己设置假人可能是你最好的选择。
df = df[df['month'] > 0]