在lmer回归中预测(),但我只需要2个类别

时间:2014-03-25 12:19:00

标签: r data-visualization lmer

我试图估计一个多级模型。我的代码是:

fullModel2 <- lmer(pharmexp_2001 ~ gdp_1000_gm + health_exp_per_cap_1000_gm + life_exp +
                   labour_cost_1000_gm + (year_gm|lowerID), data=adat, REML=F)

产生以下模型:

Linear mixed model fit by maximum likelihood  ['lmerMod']
Formula: pharmexp_2001 ~ gdp_1000_gm + health_exp_per_cap_1000_gm + life_exp +      
         labour_cost_1000_gm + (year_gm | lowerID)
   Data: adat

     AIC      BIC   logLik deviance df.resid 
  1830.2   1859.9   -906.1   1812.2      191 

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-2.5360 -0.6853 -0.0842  0.4923  4.0051 

Random effects:
 Groups   Name        Variance Std.Dev. Corr 
 lowerID  (Intercept) 134.6851 11.6054       
          year_gm       0.4214  0.6492  -1.00
 Residual             487.5324 22.0801       
Number of obs: 200, groups: lowerID, 2

Fixed effects:
                            Estimate Std. Error t value
(Intercept)                -563.7924    75.4125  -7.476
gdp_1000_gm                  -0.9050     0.2051  -4.413
health_exp_per_cap_1000_gm   37.5394     6.3943   5.871
life_exp                      8.8571     0.9498   9.326
labour_cost_1000_gm          -1.3573     0.4684  -2.898

Correlation of Fixed Effects:
            (Intr) g_1000 h____1 lif_xp
gdp_1000_gm -0.068                     
hl____1000_  0.374 -0.254              
life_exp    -0.996  0.072 -0.393       
lbr_c_1000_ -0.133 -0.139 -0.802  0.142

我知道随机效应相关性为-1是一个问题,但我有一个更大的问题。我必须绘制我的结果,但只需要2行:lowerID=0lowerID=1时。所以我想在y轴上针对pharmaexp_2001在x轴上绘制year,但我只需要2行(lowerID)。我知道我必须使用predict.merMod,但我如何绘制这些结果,只绘制这两行?目前我的情节有21行(因为我分析了21个国家的药品支出)。

1 个答案:

答案 0 :(得分:3)

欢迎访问网站@EszterTakács!

您只需在newdata中指定两个ID即可。以下是基于sleepstudyR数据的示例。我假设你想在y轴上绘制预测值。只需将代码替换为您的数据和变量,您将获得lowerID==0lowerID==1的预测值。然后,您可以使用代码绘制两个ID的两行。

> (fm1 <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy, REML=F))
Linear mixed model fit by maximum likelihood ['lmerMod']
Formula: Reaction ~ Days + (Days | Subject) 
   Data: sleepstudy 
      AIC       BIC    logLik  deviance 
1763.9393 1783.0971 -875.9697 1751.9393 
Random effects:
 Groups   Name        Std.Dev. Corr
 Subject  (Intercept) 23.781       
          Days         5.717   0.08
 Residual             25.592       
Number of obs: 180, groups: Subject, 18
Fixed Effects:
(Intercept)         Days  
     251.41        10.47  

> newdata = sleepstudy[sleepstudy$Subject==308 | sleepstudy$Subject==333,]
> str(p <- predict(fm1,newdata)) # new data, all RE
 Named num [1:20] 254 274 293 313 332 ...
 - attr(*, "names")= chr [1:20] "1" "2" "3" "4" ...