bootMer CI 的问题:上限和下限相同
problem with bootMer CI: upper and lower limits are identical
我最难为我的 glmer 泊松模型生成置信区间。在遵循了几个非常有用的教程(例如 https://drewtyre.rbind.io/classes/nres803/week_12/lab_12/)以及 Whosebug 帖子之后,我不断得到非常奇怪的结果,即 CI 的上限和下限相同。
这是一个可重现的示例,其中包含一个名为“production”的响应变量、一个名为“Treatment_Num”的固定效应和一个名为“Genotype”的随机效应:
df1 <- data.frame(production=c(15,12,10,9,6,8,9,5,3,3,2,1,0,0,0,0), Treatment_Num=c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4), Genotype=c(1,1,2,2,1,1,2,2,1,1,2,2,1,1,2,2))
#运行 glmer 模型
df1_glmer <- glmer(production ~ Treatment_Num +(1|Genotype),
data = df1, family = poisson(link = "log"))
#制作一个空数据集进行预测,其中包含解释变量但没有响应
require(magrittr)
df_empty <- df1 %>%
tidyr::expand(Treatment_Num, Genotype)
#创建包含预测的新列
df_empty$PopPred <- predict(df1_glmer, newdata = df_empty, type="response",re.form = ~0)
#bootMer 函数
myFunc_df1_glmer <- function(mm) {
predict(df1_glmer, newdata = df_empty, type="response",re.form=~0)
}
#运行 bootMer
require(lme4)
merBoot_df1_glmer <- bootMer(df1_glmer, myFunc_df1_glmer, nsim = 10)
#从中获取置信区间
predCL <- t(apply(merBoot_df1_glmer$t, MARGIN = 2, FUN = quantile, probs = c(0.025, 0.975)))
#将置信区间的下限和上限输入df_empty
df_empty$lci <- predCL[, 1]
df_empty$uci <- predCL[, 2]
#when viewing df_empty 问题变得很清楚:lci 和 uci 是相同的!
df_empty
如果您能给我任何见解,我们将不胜感激!
忽略我的评论!
问题出在您创建的传递给 bootMer()
的函数上。您写道:
myFunc_df1_glmer <- function(mm) {
predict(df1_glmer, newdata = df_empty, type="response",re.form=~0)
}
参数 mm
应该是从引导数据派生的拟合模型对象。
但是,您不会将此对象传递给 predict()
,而是将原始模型传递给
目的。如果将函数更改为:
myFunc_df1_glmer <- function(mm) {
predict(mm, newdata = df_empty, type="response",re.form=~0)
#^^ pass in the object created by bootMer
}
然后就可以了:
> df_empty
# A tibble: 8 x 5
Treatment_Num Genotype PopPred lci uci
<dbl> <dbl> <dbl> <dbl> <dbl>
1 1 1 12.9 9.63 15.7
2 1 2 12.9 9.63 15.7
3 2 1 5.09 3.87 5.89
4 2 2 5.09 3.87 5.89
5 3 1 2.01 1.20 2.46
6 3 2 2.01 1.20 2.46
7 4 1 0.796 0.361 1.14
8 4 2 0.796 0.361 1.14
顺便说一句——您的实际数据中有多少种基因型?如果少于 5-7 你可能
使用直接向上 glm()
和基因型作为使用总和为零的因素做得更好
对比。
我最难为我的 glmer 泊松模型生成置信区间。在遵循了几个非常有用的教程(例如 https://drewtyre.rbind.io/classes/nres803/week_12/lab_12/)以及 Whosebug 帖子之后,我不断得到非常奇怪的结果,即 CI 的上限和下限相同。
这是一个可重现的示例,其中包含一个名为“production”的响应变量、一个名为“Treatment_Num”的固定效应和一个名为“Genotype”的随机效应:
df1 <- data.frame(production=c(15,12,10,9,6,8,9,5,3,3,2,1,0,0,0,0), Treatment_Num=c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4), Genotype=c(1,1,2,2,1,1,2,2,1,1,2,2,1,1,2,2))
#运行 glmer 模型
df1_glmer <- glmer(production ~ Treatment_Num +(1|Genotype),
data = df1, family = poisson(link = "log"))
#制作一个空数据集进行预测,其中包含解释变量但没有响应
require(magrittr)
df_empty <- df1 %>%
tidyr::expand(Treatment_Num, Genotype)
#创建包含预测的新列
df_empty$PopPred <- predict(df1_glmer, newdata = df_empty, type="response",re.form = ~0)
#bootMer 函数
myFunc_df1_glmer <- function(mm) {
predict(df1_glmer, newdata = df_empty, type="response",re.form=~0)
}
#运行 bootMer
require(lme4)
merBoot_df1_glmer <- bootMer(df1_glmer, myFunc_df1_glmer, nsim = 10)
#从中获取置信区间
predCL <- t(apply(merBoot_df1_glmer$t, MARGIN = 2, FUN = quantile, probs = c(0.025, 0.975)))
#将置信区间的下限和上限输入df_empty
df_empty$lci <- predCL[, 1]
df_empty$uci <- predCL[, 2]
#when viewing df_empty 问题变得很清楚:lci 和 uci 是相同的!
df_empty
如果您能给我任何见解,我们将不胜感激!
忽略我的评论!
问题出在您创建的传递给 bootMer()
的函数上。您写道:
myFunc_df1_glmer <- function(mm) {
predict(df1_glmer, newdata = df_empty, type="response",re.form=~0)
}
参数 mm
应该是从引导数据派生的拟合模型对象。
但是,您不会将此对象传递给 predict()
,而是将原始模型传递给
目的。如果将函数更改为:
myFunc_df1_glmer <- function(mm) {
predict(mm, newdata = df_empty, type="response",re.form=~0)
#^^ pass in the object created by bootMer
}
然后就可以了:
> df_empty
# A tibble: 8 x 5
Treatment_Num Genotype PopPred lci uci
<dbl> <dbl> <dbl> <dbl> <dbl>
1 1 1 12.9 9.63 15.7
2 1 2 12.9 9.63 15.7
3 2 1 5.09 3.87 5.89
4 2 2 5.09 3.87 5.89
5 3 1 2.01 1.20 2.46
6 3 2 2.01 1.20 2.46
7 4 1 0.796 0.361 1.14
8 4 2 0.796 0.361 1.14
顺便说一句——您的实际数据中有多少种基因型?如果少于 5-7 你可能
使用直接向上 glm()
和基因型作为使用总和为零的因素做得更好
对比。