簡體   English   中英

混合模型中估計值的置信區間

[英]confidence intervals of estimates in mixed models

我可以得到像這樣的混合模型的預測值:

mod <- lmer(sales1 ~ price1 + (1|store), oranges)
X <- with(oranges, expand.grid(price1=c(30,50,70)))
X$pred <- predict(mod, newdata=X, re.form=NA)

> X
      price1      pred
    1     30 23.843916
    2     50 11.001901
    3     70 -1.840114

但是如何獲得這三個估計的上下置信區間?

我安裝了merTools軟件包並嘗試

predictInterval(mod, newdata = X, n.sims = 999) 

但是有一個錯誤

Error in eval(predvars, data, env) : object 'store' not found

predictInterval which設置為"fixed"應該足夠了,但事實並非如此。 因此,它看起來像個錯誤。 但是,如果我們為分組變量提供任何值,則與此參數一起,一切正常。

library(lme4)
library(merTools)
fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
X1 <- data.frame(Reaction = 250, Days = 4, Subject = 309)
predictInterval(fm1, newdata = X1, n.sims = 999, seed = 1)
#        fit      upr      lwr
# 1 216.8374 256.8839 181.1969
X1 <- data.frame(Reaction = 250, Days = 4, Subject = 310)
predictInterval(fm1, newdata = X1, n.sims = 999, seed = 1)
#       fit      upr      lwr
# 1 230.959 271.0055 195.3185

不出所料,不同的受試者給出不同的預測。 但是,將which設置為"fixed"有助於:

X1 <- data.frame(Reaction = 250, Days = 4, Subject = 309)
predictInterval(fm1, newdata = X1, n.sims = 999, seed = 1, which = "fixed")
#        fit      upr      lwr
# 1 291.9062 328.5429 256.2472
X1 <- data.frame(Reaction = 250, Days = 4, Subject = 310)
predictInterval(fm1, newdata = X1, n.sims = 999, seed = 1, which = "fixed")
#        fit      upr      lwr
# 1 291.9062 328.5429 256.2472

分組值甚至不必有意義,因為最終會被忽略:

X1 <- data.frame(Reaction = 250, Days = 4, Subject = -1)
predictInterval(fm1, newdata = X1, n.sims = 999, seed = 1, which = "fixed")
#        fit      upr      lwr
# 1 291.9062 328.5429 256.2472
# Warning message:
#      The following levels of Subject from newdata 
#  -- -1 -- are not in the model data. 
#      Currently, predictions for these values are based only on the 
#  fixed coefficients and the observation-level error. 

您還可以使用ggeffects-package (例如,在此package-vignette中為示例),因為您無需為newdata創建數據框,因此可以節省一些時間:

library(ggeffects)
library(lme4)
#> Loading required package: Matrix
data("sleepstudy")
m <- lmer(Reaction ~ Days + (1 + Days | Subject), data = sleepstudy)
ggpredict(m, "Days")
#> 
#> # Predicted values of Reaction 
#> # x = Days 
#> 
#>  x predicted std.error conf.low conf.high
#>  0   251.405     6.825  238.029   264.781
#>  1   261.872     6.787  248.570   275.174
#>  2   272.340     7.094  258.435   286.244
#>  3   282.807     7.705  267.705   297.909
#>  5   303.742     9.581  284.963   322.520
#>  6   314.209    10.732  293.174   335.244
#>  7   324.676    11.973  301.210   348.142
#>  9   345.611    14.629  316.939   374.283
#> 
#> Adjusted for:
#> * Subject = 308

# example solution for the case mentioned
# in the comments...
r <- c(2,4,6)
s <- paste0("Days [", toString(sprintf("%s", r)), "]", collapse = "")

ggpredict(m, s)
#> 
#> # Predicted values of Reaction 
#> # x = Days 
#> 
#>  x predicted std.error conf.low conf.high
#>  2   272.340     7.094  258.435   286.244
#>  4   293.274     8.556  276.506   310.043
#>  6   314.209    10.732  293.174   335.244
#> 
#> Adjusted for:
#> * Subject = 308

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM