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 行:whenlowerID=0和 when lowerID=1。所以我想pharmaexp_2001在 y 轴上相对于yearx 轴绘制,但我只需要 2 行(by lowerID)。我知道我必须使用predict.merMod,但我怎样才能绘制这些结果,只绘制这两行?目前我的情节有 21 行(因为我分析了 21 个国家的药品支出)。

4

1 回答 1

3

欢迎来到该网站,@Eszter Takács!

您只需要在newdata. 这是一个基于sleepstudy. R我假设您想在 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" ...
于 2014-03-25T15:14:58.180 回答