事实证明更容易使用predict
than broom.mixed::augment
.
构建预测
(老鼠/国家/天数 0-150 的所有组合(天数达到 200 会导致一些极端的预测,从而超出了垂直范围)
library(tidyverse)
dc <- distinct(dplyr::select(dat1, Rat, Country))
pframe <- (with(dat1,
expand_grid(Rat = unique(Rat),
Day = 0:150))
%>% full_join(dc, by = "Rat")
%>% mutate(lVolume = predict(m1, newdata = .))
)
将数据和预测合并到一个数据框中(您不必这样做,但它使图例变得容易)
comb <- dplyr::bind_rows(list(data = dat1, model = pframe),
.id = "type")
Plot:
ggplot(comb, aes(Day, exp(lVolume), colour = type)) +
geom_point(alpha = 0.2) +
geom_line(aes(group = interaction(type, Rat))) +
scale_colour_manual(values = c("black", "red"))
重建数据:
dat0 <- list(
list("rat1", vol=c(78,304,352,690,952,1250), days = c(89,110,117,124,131,138), country = "Chile"),
list("rat2", vol=c(202,440,520,870,1380), days = c(75,89,96,103,110), country = "Chile"),
list("rat3", vol=c(186,370,620,850,1150), days = c(75,89,96,103,110), country = "Chile"),
list("rat4", vol=c(92,250,430,450,510,850,1000,1200), days = c(47,61,75,82,89,97,103,110), country = "England"),
list("rat5", vol=c(110,510,710,1200), days = c(47,61,75,82), country = "England"),
list("rat6", vol=c(115,380,480,540,560,850,1150,1350), days = c(47,61,75,82,89,97,103,110), country = "England"))
dat1 <- purrr::map_dfr(dat0,
~ data.frame(Rat = .[[1]],
lVolume = log(.$vol), Day = .$days,
Country = .$country))
m1 <- lmer(lVolume ~ Country*Day + (1|Rat), data = dat1)