在ggplot2中,如何为多因素数据绘制95%置信区间?

huangapple go评论107阅读模式
英文:

How to plot 95% confidence intervals for multiple factor data in ggplot2 R?

问题

以下是翻译好的代码部分:

  1. df <- data.frame(
  2. yaxis = rnorm(120, 5, 1),
  3. xaxis = rep(c("A", "B", "C", "D", "E", "F"), times = 20),
  4. factor = rep(c("1", "2", "3", "4", "5", "6"), each = 20)
  5. )
  6. df$xaxis <- as.factor(df$xaxis)
  7. df$factor <- as.factor(df$factor)
  8. library(lmerTest)
  9. mod <- lmer(yaxis ~ xaxis + (1|factor), df)
  10. mod.fit <- predictInterval(mod, df)
  11. df.fit <- cbind(df, mod.fit)
  12. ggplot(df.fit, aes(x = xaxis, y = yaxis, color = xaxis, group = xaxis)) +
  13. geom_point() +
  14. theme_classic() +
  15. geom_errorbar(aes(min = lwr, max = upr), color = "black", width = 0.3) +
  16. geom_point(aes(y = mean(fit), x = xaxis), position = position_dodge(width = 0.25), color = "black", size = 5)

请告诉我,您还有其他需要吗?

英文:
  1. df &lt;- data.frame (yaxis = rnorm(120,5,1),
  2. xaxis = rep(c(&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;D&quot;,&quot;E&quot;, &quot;F&quot;), times = 20),
  3. factor = rep(c(&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;, &quot;6&quot;), each = 20))
  4. df$xaxis &lt;- as.factor(df$xaxis)
  5. df$factor &lt;- as.factor(df$factor )
  6. library(lmerTest)
  7. mod &lt;- lmer(yaxis ~ xaxis + (1|factor), df)
  8. mod.fit &lt;- predictInterval(mod, df)
  9. df.fit &lt;- cbind(df, mod.fit)
  10. ggplot(df.fit, aes(x = xaxis, y = yaxis, color = xaxis, group = xaxis)) +
  11. geom_point() +
  12. theme_classic() +
  13. geom_errorbar(aes(min = lwr, max = upr), color = &quot;black&quot;, width = 0.3) +
  14. geom_point(aes(y = mean(fit), x = xaxis), position = position_dodge(width = 0.25), color = &quot;black&quot;,size = 5)

I want to plot the raw data with the predicted 95% CI and means. I tried the above but I get multiple confidence intervals for the fitted data. How can I correct it?

在ggplot2中,如何为多因素数据绘制95%置信区间?

答案1

得分: 2

You can extract the fixed effect from your model using summary(mod)$coefficients. This allows you to get the prediction for each x-axis value and calculate the 95% confidence interval of that prediction using simple arithmetic.

英文:

You can extract the fixed effect from your model using summary(mod)$coefficients. This allows you to get the prediction for each x axis value and calculate the 95% confidence interval of that prediction using simple arithmetic.

  1. library(tidyverse)
  2. library(lme4)
  3. mod &lt;- lmer(yaxis ~ xaxis + (1|factor) + 0, df)
  4. summary_df &lt;- summary(mod)$coefficients %&gt;%
  5. as_tibble() %&gt;%
  6. mutate(xaxis = LETTERS[1:6]) %&gt;%
  7. rename(yaxis = Estimate) %&gt;%
  8. mutate(ymin = yaxis - 1.96 * `Std. Error`,
  9. ymax = yaxis + 1.96 * `Std. Error`)
  10. ggplot(df, aes(x = xaxis, y = yaxis, color = xaxis, group = xaxis)) +
  11. geom_jitter(width = 0.1) +
  12. geom_errorbar(aes(ymin = ymin, ymax = ymax), data = summary_df,
  13. width = 0.4, linewidth = 1, alpha = 0.4) +
  14. geom_point(color = &#39;black&#39;, size = 2, data = summary_df) +
  15. theme_classic() +
  16. scale_color_brewer(palette = &#39;Dark2&#39;)

在ggplot2中,如何为多因素数据绘制95%置信区间?

huangapple
  • 本文由 发表于 2023年5月7日 21:39:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定