Skip to content
Prev 14030 / 20628 Next

calculation of confidence intervals for random slope model

On 15-11-28 06:18 PM, W. Duncan Wadsworth wrote:
It took me a while to remember what the issue is here -- by default
`bootMer` *resamples* the random effects rather than conditioning on
them.  You need to use "use.u" or "re.form" to specify the
conditioning, as follows ...

sleepy = lmer(Reaction ~ Days + (Days | Subject), data = sleepstudy)
## bootstrap summary function
sumfun = function(.) {
    coef(.)$Subject[,"Days"]
}
## where the fun happens
booty = as.data.frame(bootMer(sleepy, sumfun, nsim = 99,
       re.form=~Days|Subject))
## for plotting
rr <- ranef(sleepy)$Subject
colnames(booty) = rownames(rr)
slope_bs_distros = booty %>% tidyr::gather("Subject", "Value")
## bootstrap distributions of individual slope parameters (??)
## indiv_ests <- rr %>% add_rownames("Subject") %>%
##    mutate(Days=Days+fixef(sleepy)["Days"])
indiv_ests <- add_rownames(coef(sleepy)$Subject,"Subject")
ggplot(slope_bs_distros, aes(x = Value)) + geom_histogram() +
  facet_wrap(~ Subject, ncol = 5) +
  geom_vline(xintercept = 0, color = "red", size = 1.5)+
  geom_vline(aes(xintercept=Days),colour="blue",data=indiv_ests)