3

我试图复制使用 lme4 glmer 和 Pandas -> R -> glmer 的纯 R 代码的结果。原始输出是

%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 到 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

输出

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 读取文件本身时,它会将月份变成称为“序数因子”的东西;而从 Pandas -> R 这种类型被视为数值,也许这就是区别?我相信我能够正确复制派生列基线,但是我确实必须将状态转换为 1/0 数值,而纯 R 可以将此列作为字符串(好/差)使用。

注意:更正 - 我错过了 Python 部分中的过滤条件,其中仅采用了 month>0。一旦完成

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

然后处理处理系数为 2.16,接近纯 R。R 仍然显示正的 baselinegood,而 Pandas -> R 显示带负系数的 baselinepoor,但我想这是一个微小的差异。

4

0 回答 0