Broom.mixed exp 模型预测

发布于 2025-01-09 02:32:50 字数 1410 浏览 1 评论 0原文

我想请求一些帮助来绘制我的模型的预测值以及 lmer() 估计生成的方程。

所以,我得到的数据是不同老鼠在不同日期的质量体积。每只老鼠都有不同的时间点来测量该体积。

因此,我使用的模型是:

 m1 <- lmer(lVolume ~ Country*Day + (1|Rat))

我这样做是因为我对 exp(fitted) 值感兴趣,然后获得该模型的指数方法,而不是使用非线性混合效应模型(对于 在

为了绘制该模型的预测,我的尝试是:

m1%>% 
  augment() %>% 
  clean_names() %>% 
  ggplot(data = .,
         mapping = aes(x = day,
                       y = exp(l_volume),
                       group = rat)) +
  geom_point(alpha = 0.5) +
  geom_line(alpha = 0.5) +
  geom_point(aes(y = exp(fitted)),
             color = "red") + 
  geom_line(aes(y = exp(fitted)),
            color = "red") + 
  expand_limits(x = 0 , y = 0)

这里,我绘制了更多的老鼠,但是,正如您所看到的,(0,0) 与 lmer 的预测相差太远。我想知道如何绘制模型生成的预测以查看 (0,200) 中的点。我尝试从这里创建一个新的数据框,然后再次使用预测(m1,newdata = new_df)进行绘图,但我不知道如何创建这个数据框,因为我有20只老鼠,而且我不知道如何扩展它到预测()。

我的尝试:

pframe <- data.frame(Day=seq(0, 200, length.out=101))

pframe$continuous_outcome <- predict(m1, newdata = pframe, level = 0)
ggplot(data, aes(Day,lVolume)) + 
  geom_point() +
  geom_line(data=pframe)

但出现错误:

eval(predvars, data, env) 中的错误:未找到对象“Rat”

并且,还有一种方法可以绘制根据每个估计生成的方程,即,从每只大鼠中,您有一组固定和随机的估计器,我如何绘制 lmer 为每只老鼠生成的方程(红色曲线)?

I would like to ask for some help with plotting prediction values from my model as well as the equations generated by the estimation of the lmer().

So, the data that I have is the mass volume of different rats across different days. Each rat has different time points where they took the measurement of that volume.

So, then the model that I use is :

 m1 <- lmer(lVolume ~ Country*Day + (1|Rat))

I do this because I am interested in exp(fitted) values and then obtaining an exponential approach for this model instead of using a nonlinear mixed effect model (for the moment)

To plot the predictions from this model, my attempt was:

m1%>% 
  augment() %>% 
  clean_names() %>% 
  ggplot(data = .,
         mapping = aes(x = day,
                       y = exp(l_volume),
                       group = rat)) +
  geom_point(alpha = 0.5) +
  geom_line(alpha = 0.5) +
  geom_point(aes(y = exp(fitted)),
             color = "red") + 
  geom_line(aes(y = exp(fitted)),
            color = "red") + 
  expand_limits(x = 0 , y = 0)

Here I plotted more rats but, as you can see the (0,0) is too far from the predictions of the lmer. I was wondering how I plot the prediction that my model is generating to see points from (0,200). I tried a hint from here by creating a new data frame and then plot using again predict(m1, newdata = new_df), but I am clueless how to create this data frame since I have 20 rats and I do not know how to expand this to the predict().

My attempt:

pframe <- data.frame(Day=seq(0, 200, length.out=101))

pframe$continuous_outcome <- predict(m1, newdata = pframe, level = 0)
ggplot(data, aes(Day,lVolume)) + 
  geom_point() +
  geom_line(data=pframe)

but I got an error:

Error in eval(predvars, data, env) : object 'Rat' not found

And, also is there a way to plot also the equations that you generate from each estimation, i.e, from each rat you have a set of estimators fixed and random, how can I plot the equation (red curves) that the lmer is generating for each of the rats?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅忆 2025-01-16 02:32:50

事实证明,使用 predict 比使用 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")

情节:

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)

It turned out to be easier to use predict than broom.mixed::augment.

construct predictions

(all combinations of Rat/Country/Days 0-150 (taking day up to 200 led to some extreme predictions that blew the vertical scale)

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 = .))
)

Combine data and predictions into a single data frame (you don't have to do this but it makes the legend easy)

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"))

Reconstructing data:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文