使用merTools中的predictInterval()从glmer对象生成边际预测置信区间
我正在尝试使用使用merTools中的predictInterval()从glmer对象生成边际预测置信区间,r,predict,lme4,R,Predict,Lme4,我正在尝试使用predictInterval函数为边际预测生成置信区间,该函数为 在此,我使用ResourceSelection软件包中的山羊数据,其中包含已使用和可用的位置(分别编码为1和0)以及相关协变量的值(例如海拔、坡度等),以构建可复制的模型 包裹 library(lme4) library(ResourceSelection) library(merTools) df包含10只动物的已使用和可用位置 table(goats$ID, goats$STATUS) 0
predictInterval
函数为边际预测生成置信区间,该函数为
在此,我使用ResourceSelection软件包中的山羊
数据,其中包含已使用和可用的位置(分别编码为1和0)以及相关协变量的值(例如海拔、坡度等),以构建可复制的模型
包裹
library(lme4)
library(ResourceSelection)
library(merTools)
df包含10只动物的已使用和可用位置
table(goats$ID, goats$STATUS)
0 1
1 1404 702
2 1112 556
3 1026 513
4 634 317
5 1272 636
6 1456 728
7 1394 697
8 1468 734
9 1608 804
10 1302 651
下面是一个为单个(ID)指定随机截距的示例模型。使用scale()
任何关于如何从glmer对象生成边际预测的建议都将受到极大的欢迎
突出的会话信息粘贴在仅供参考的下方
> sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
other attached packages:
[1] merTools_0.2.0 plyr_1.8.3
[3] arm_1.8-6 MASS_7.3-45
[5] ResourceSelection_0.2-5 lme4_1.1-10
[7] Matrix_1.2-3 sp_1.2-1
此处是merTools
的软件包维护程序。我们实现这一功能的方式并不简单,但这是可能的
您需要添加一个步骤,将中间随机效果添加到data.frame。在大多数情况下,中值随机效应应为0,或足够接近,以使其接近您所寻找的。为此,只需稍微修改代码,并使用merTools
中的REquantile
功能即可:
medEff = REquantile(mod, quantile = 0.5,
groupFctr = "ID",
term = "(Intercept)")
PredDat <- data.frame(ELEVATION = seq(-1.97056, 2.52926, length.out = 1000),
SLOPE = 0, ET = 0, HLI = 0, ID = medEff)
Preds <- predictInterval(mod, newdata = PredDat, type = "probability")
你应该得到这样的东西:
head(Preds)
fit upr lwr
1 0.1860053 0.2482220 0.1427370
2 0.1860058 0.2482226 0.1427373
3 0.1860062 0.2482231 0.1427377
4 0.1860066 0.2482237 0.1427380
5 0.1860071 0.2482242 0.1427384
6 0.1860075 0.2482248 0.1427388
这不包括与分组因素或模型本身的变化相关的观察水平的任何不确定性 感谢您提供的信息和详细的答复。非常有用。事实上,您的代码的第二部分就是我一直在寻找的。我将添加它作为扩展,以在将来包含在merTools
中。对于我们来说,将其包含在predictInterval
> sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
other attached packages:
[1] merTools_0.2.0 plyr_1.8.3
[3] arm_1.8-6 MASS_7.3-45
[5] ResourceSelection_0.2-5 lme4_1.1-10
[7] Matrix_1.2-3 sp_1.2-1
medEff = REquantile(mod, quantile = 0.5,
groupFctr = "ID",
term = "(Intercept)")
PredDat <- data.frame(ELEVATION = seq(-1.97056, 2.52926, length.out = 1000),
SLOPE = 0, ET = 0, HLI = 0, ID = medEff)
Preds <- predictInterval(mod, newdata = PredDat, type = "probability")
PredDat <- data.frame(Intercept = 1,
ELEVATION = seq(-1.97056, 2.52926,length.out = 1000),
SLOPE = 0, ET = 0, HLI = 0)
fe.tmp <- fixef(mod)
vcov.tmp <- as.matrix(vcov(mod))
n.sims <- 1000
sigmahat <- rep(1, n.sims)
# Make n.sims draws for each element of the fixed effects
betaSim <- abind::abind(lapply(1:n.sims,
function(x) mvtnorm::rmvnorm(n = 1, mean = fe.tmp,
sigma = sigmahat[x]*vcov.tmp, method = "chol")), along=1)
# Calculate n.sims predictions for each row in PredDat
fixed <- as.matrix(PredDat) %*% t(betaSim)
# For each row (observation) in PredDat calculate the median, upr and lwr
Preds <- data.frame(fit = apply(fixed, 1, median),
upr = apply(fixed, 1, quantile, 0.9),
lwr = apply(fixed, 1, quantile, 0.1))
# Calculate the probability from the linear predictor
Preds <- apply(Preds, 2, invlogit)
head(Preds)
fit upr lwr
1 0.1860053 0.2482220 0.1427370
2 0.1860058 0.2482226 0.1427373
3 0.1860062 0.2482231 0.1427377
4 0.1860066 0.2482237 0.1427380
5 0.1860071 0.2482242 0.1427384
6 0.1860075 0.2482248 0.1427388