Glmer通过纯R和rmagic导致差异

时间:2015-02-20 10:39:25

标签: r pandas ipython-notebook rpy2 ipython-magic

我正在尝试复制使用lme4 glmer和Pandas的纯R代码的结果 - > 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 <- 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,我得到了

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

输出

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: df

     AIC      BIC   logLik deviance df.resid 
   539.2    573.7   -261.6    523.2      547 

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-3.8025 -0.4123  0.1637  0.4295  2.4482 

Random effects:
 Groups  Name        Variance Std.Dev.
 subject (Intercept) 1.829    1.353   
Number of obs: 555, groups:  subject, 111

Fixed effects:
                   Estimate Std. Error z value Pr(>|z|)    
(Intercept)         1.39252    0.60229   2.312   0.0208 *  
baselinepoor       -3.42262    0.46095  -7.425 1.13e-13 ***
month               0.12730    0.08465   1.504   0.1326    
treatmenttreatment  1.59332    0.39981   3.985 6.74e-05 ***
gendermale          0.12915    0.49291   0.262   0.7933    
age                -0.01833    0.01480  -1.238   0.2157    
centre2             0.70520    0.39676   1.777   0.0755 .  

结果有些不同。当R读取文件本身时,它将月份变成称为“序数因子”的东西;而来自熊猫 - &gt; R这种类型被视为数值,也许这是区别?我相信我能够正确复制派生列基线,但我必须将状态转换为1/0数值,而纯R可以将此列用作字符串(好/差)。

注意:更正 - 我错过了Python部分中的过滤条件,其中只有月份> 0。一旦完成

df = df[df['month'] > 0]

然后治疗处理系数为2.16,接近纯R.R仍显示正基线好,而Pandas - > R显示具有负系数的baselinepoor,但我猜这是一个小的差异。

0 个答案:

没有答案