使用Bootstrapping为GLMM中的回归曲线创建置信区间 [英] Creating confidence intervals for regression curve in GLMM using Bootstrapping
问题描述
已创建GLMM模型并绘制了每个因素的预测概率.但是,我无法理解如何使用BootMer函数创建置信区间.我不断收到错误消息,无法从非整数优先权进行模拟.我希望有人能够提供帮助?预先感谢.
Have created a GLMM model and plotted the predicted probabilities of each factor. However, I cannot fathom how to create confidence intervals using the BootMer function. I keep getting the error message cannot simulate from non integer prior weights. I'm hoping someone would be able to help? Thanks in advance.
glmm1 <- glmer(cbind(Total_Dead, Total_Collected - Total_Dead) ~
Species + timeseries + (1|Location),
data = dat, family= "binomial")
dat$timeseries <- dat$Study_Date - 1998
plot(predict(glmm1, data.frame(Species="An. Arab", timeseries= dat$timeseries),
type="response", re.form = NA) ~
dat$timeseries, frame=FALSE, bty="n", pch="", xaxt="n", ylim=c(0, 0.5),
ylab="Predicted mortality", xlab="Year",
main = "Predicted mortality by species",
cex.lab=1.6, yaxt="n")
axis(1, at=c(1:17), labels=1999:2015, cex.axis=1.8)
axis(2, las=2, at=seq(0, 1, 0.2), labels=seq(0, 1, 0.2), cex.axis=1.8)
COLS <- c("blue", "red", "purple", "aquamarine3", "orange")
PCH <- c(17, 15, 19, 20, 5)
for(i in 1:length(unique(levels(dat$Species)))){
points((dat$Total_Dead[dat$Species == levels(dat$Species)[i]] /
dat$Total_Collected[dat$Species == levels(dat$Species)[i]]) ~
dat$timeseries[dat$Species == levels(dat$Species)[i]],
pch=PCH[i], col=COLS[i])
lines(predict(glmm1, data.frame(Species=levels(dat$Species)[i],
timeseries = dat$timeseries), type="response",
re.form = NA) ~ dat$timeseries, lty=4, col=COLS[i])
}
bootstrap <- bootMer(x=glmm1, FUN= fixef, nsim=200)
推荐答案
出于某种原因,Bootmer遇到了问题,您必须使用mertools软件包
for some reason Bootmer has problems with that, you have to use the mertools package
library(merTools)
preds <- predictInterval(glmm1, newdata = your.datarame, n.sims = 1000)
然后我将使用preds data.frame进行绘制,结果data.frame具有适合,上限和下限,然后可以使用geom_ribbon进行绘制,如果需要更多帮助,请告诉我.
I would use then then the preds data.frame to plot, the resulting data.frame has the fit, upper and lower limit, then you can use geom_ribbon to plot it, if you need more help let me know.
现在请耐心等待,您实际上想为图形创建一个新的标准化数据集.如果您使用此代码,它将起作用:
now bear with me, you actually want to make a new standardized dataset for your graph. If you use this code it will work:
glmm1 <- glmer(cbind(Total_Dead, Total_Collected - Total_Dead) ~
Species + timeseries + (1|Location),
data = dat,family= "binomial")
拟合模型,然后创建新的数据集,这将在第一个位置(阿克伦)中将每个物种的时间序列从1到16,请注意,如果您想要每个位置的图表,只需将[]之间的数字从1更改为2,最多可以更改为17个位置即可.
fit your model, then create your new data set, this will have your timeseries from 1 to 16 for each species, in your first location (Akron), note that you will have to do this for each location if you want the graph for each location, you can do that just by changing the number between [] from 1, to 2 up to your 17 locations
new.data <-data.frame(timeseries = rep(1:16, times = 5), Species = rep(unique(dat$Species), each = 16), Location = rep(unique(dat$Location)[1], each = 80))
然后预测此类数据集的值和间隔
Then predict the values and intervals for such dataset
preds <- predictInterval(glmm1, newdata = new.data, n.sims = 1000)
现在将此预测加入您的new.data
now join this prediction to your new.data
new.data <- cbind(new.data, preds)
最后为每个物种用不同的颜色绘制它
and finally plot it with different colors for each species
ggplot(new.data, aes(x = timeseries, y = fit)) + geom_ribbon(aes(ymax=upr, ymin =lwr, fill=Species), alpha = 0.9)+ geom_line(aes(color=Species))
如果您不了解某些内容,可以继续询问,当前您的标准错误很大,因此请先检查一下您是否更喜欢
If you don't understand something don't hesitate to keep asking, currently your standard errors are quite big so first check to see if you like that better
ggplot(new.data, aes(x = timeseries, y = fit)) + geom_line(aes(color=Species))
这篇关于使用Bootstrapping为GLMM中的回归曲线创建置信区间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!