如何在R GGPLOT2中与相互作用拟合混合效应模型回归?

发布于 2025-01-29 16:55:29 字数 649 浏览 4 评论 0原文

我与之合作的数据集具有互动术语。我想将模型与x轴中的交互项和y轴中的y.var一起拟合。我尝试关注这个例子关于如何在ggplot2中复制它太远(即绘图样式函数不再起作用..所以我不知道如何复制结果)。

temp <- rnorm(100, 2,1)set.seed(111)
temp <- rnorm(100, 3,1)
rainfall <- rnorm(100,5,1)
y.var <- rnorm(100, 2,1)
site <- rep(c("A","B","C","D"), each = 25)     

df <- data.frame(temp, rainfall, y.var, site)
df$site <- as.factor(as.character(df$site))

mod <- lmer(y.var ~ temp * rainfall + (1|site), data  = df)
summary(mod)

The dataset I'm working with has an interaction term. I want to fit the model with the interaction term in the x-axis and the y.var in the y-axis. I tried following this example but didn't get too far on how to replicate it in ggplot2 (i.e. the plot style function doesn't work anymore..so I didn't know how to replicate the results).

temp <- rnorm(100, 2,1)set.seed(111)
temp <- rnorm(100, 3,1)
rainfall <- rnorm(100,5,1)
y.var <- rnorm(100, 2,1)
site <- rep(c("A","B","C","D"), each = 25)     

df <- data.frame(temp, rainfall, y.var, site)
df$site <- as.factor(as.character(df$site))

mod <- lmer(y.var ~ temp * rainfall + (1|site), data  = df)
summary(mod)

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

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

发布评论

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

评论(1

┊风居住的梦幻卍 2025-02-05 16:55:29

如果要直接在GGPLOT中绘制模型,而不是使用扩展程序包,则需要生成一个数据框架以绘制。这样做的好处意味着您还可以在图中包含原始数据点。

由于您在Y轴上有y.var,因此您只剩下一个轴来绘制两个固定效应变量。这意味着您需要为X轴选择降雨或温度,并用其他美学(例如颜色)表示另一个变量。在此示例中,我将使用温度作为X轴。显然,为了使图可解释,我们需要限制我们预测的降雨的“切片”数量。在这里,我将使用5。

随着降雨量的增加,线路的变化在这里可见。

library(geomtextpath)
library(lme4)

mod <- lmer(y.var ~ temp * rainfall + (1|site), data  = df)

newdf <- expand.grid(temp = seq(min(df$temp), max(df$temp), length = 100),
                     rainfall = seq(min(df$rainfall), max(df$rainfall), 
                                    length = 5))

newdf$y.var <- predict(mod, newdata = newdf)

ggplot(newdf, aes(x = temp, y = y.var, group = rainfall)) +
  geom_point(data = df, aes(shape = site, color = rainfall)) +
  geom_textline(aes(color = rainfall, label = round(rainfall, 2)), 
                hjust = 0.95) +
  scale_color_gradient(low = 'navy', high = 'red4') +
  theme_light(base_size = 16)

If you want to plot the model in ggplot directly rather than using an extension package, you need to generate a data frame of predictions to plot. The benefit of doing it this way means you can also include your original data points on the plot.

Since you have y.var on the y axis, you are only left with one axis to plot two fixed effect variables. This means you will need to choose either rainfall or temperature for the x axis, and represent the other variable with another aesthetic such as color. In this example I'll use temperature for the x axis. Obviously to make the plot interpretable, we need to limit the number of "slices" of rainfall that we predict. Here I'll use 5.

The interaction effect is visible here as the change in slope of the line as rainfall increases.

library(geomtextpath)
library(lme4)

mod <- lmer(y.var ~ temp * rainfall + (1|site), data  = df)

newdf <- expand.grid(temp = seq(min(df$temp), max(df$temp), length = 100),
                     rainfall = seq(min(df$rainfall), max(df$rainfall), 
                                    length = 5))

newdf$y.var <- predict(mod, newdata = newdf)

ggplot(newdf, aes(x = temp, y = y.var, group = rainfall)) +
  geom_point(data = df, aes(shape = site, color = rainfall)) +
  geom_textline(aes(color = rainfall, label = round(rainfall, 2)), 
                hjust = 0.95) +
  scale_color_gradient(low = 'navy', high = 'red4') +
  theme_light(base_size = 16)

enter image description here

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