在lmer回归中使用predict(),但我只需要它针对2个类别。

3
我将尝试估计一个多层模型。我的代码如下:
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=0时和当lowerID=1时。因此,我想在y轴上绘制pharmaexp_2001,在x轴上绘制year,但我只需要这两条线(通过lowerID)。我知道我必须使用predict.merMod,但如何绘制这些结果,并仅绘制这两条线?目前我的图表有21条线(因为我分析了21个国家的药品支出)。

请问您能否解释一下随机效应部分的 year_gm 是什么意思? - Michael M
1个回答

3
欢迎来到本站,@Eszter Takács!
您只需要在“newdata”中指定两个ID。以下是基于R中“sleepstudy”数据的示例。我假设您想在y轴上绘制“预测值”。只需用您的数据和变量替换代码,您将获得“lowerID==0”和“lowerID==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" ...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接