如何在 ggplot2 混合效应模型中仅显示一条回归线的置信区间?

问题描述 投票:0回答:1

以下代码创建一个包含 3 列的数据框。

set.seed(142222)
num_lots <- 5

# Create an empty data frame to store the simulated data
data <- data.frame(Lot = rep(1:num_lots, each = 9),
                   Time = rep(3 * 0:8, times = num_lots),
                   Measurement = numeric(num_lots * 9))

# Simulate purity data for each lot and time point
for (lot in 1:num_lots) {
  # Generate random intercept and slope for each lot
  intercept <- rnorm(1, mean = 95, sd = 2)
  slope <- runif(1, min = -.7, max = 0)
  
  for (month in 0:8) {
    # Simulate purity data with noise
    data[data$Lot == lot & data$Time == month * 3, "Purity"] <- intercept + slope * month * 3 + rnorm(1, mean = 0, sd = .35)
  }
}

然后我将混合效应模型拟合到模拟数据。如下:

ggplot(data, aes(x = Time, y = Purity, color = as.factor(Lot), shape = as.factor(Lot))) +
  geom_point() +
  geom_smooth(method = "lm", se=FALSE, type = 1) +
  labs(
    title = "Test",
    x = "month",
    y = "Purity",
    color = "Lot",    # Set legend title for color
    shape = "Lot"     # Set legend title for shape
  ) +
  theme_minimal() +
  scale_x_continuous(breaks = c(0, 3, 6, 9, 12, 15, 18, 21, 24))

结果如下所示:

问题: 我只想在

95% lower confidence bound
上显示
worst regression line
。我怎样才能做到这一点?

Worst regression line
是比其他线早与水平线 == 80 相交的线。 我知道如果我设置
se == TRUE
那么所有行的所有置信界限都会显示出来。但我只想得到最差线的置信下限。

额外问题: 如何修复图例,只显示符号(而不显示符号上方的线条)?

r ggplot2 mixed-models confidence-interval
1个回答
0
投票

您可以绘制两个

geom_smooth()
- 一个用于 4 条“好”线,一个用于 1 条“最差”线,例如

library(tidyverse)

set.seed(142222)
num_lots <- 5

# Create an empty data frame to store the simulated data
data <- data.frame(Lot = rep(1:num_lots, each = 9),
                   Time = rep(3 * 0:8, times = num_lots),
                   Measurement = numeric(num_lots * 9))

# Simulate purity data for each lot and time point
for (lot in 1:num_lots) {
  # Generate random intercept and slope for each lot
  intercept <- rnorm(1, mean = 95, sd = 2)
  slope <- runif(1, min = -.7, max = 0)
  
  for (month in 0:8) {
    # Simulate purity data with noise
    data[data$Lot == lot & data$Time == month * 3, "Purity"] <- intercept + slope * month * 3 + rnorm(1, mean = 0, sd = .35)
  }
}

ggplot(data = data,
       aes(x = Time, y = Purity, 
           color = as.factor(Lot), 
           shape = as.factor(Lot))) +
  geom_point(key_glyph = "point") +
  geom_smooth(data = data %>% filter(Lot == 2),
              method = "lm", se=TRUE, type = 1,
              key_glyph = "point") +
  geom_smooth(data = data %>% filter(Lot != 2),
              method = "lm", se=FALSE, type = 1,
              key_glyph = "point") +
  labs(
    title = "Test",
    x = "month",
    y = "Purity",
    color = "Lot",    # Set legend title for color
    shape = "Lot"     # Set legend title for shape
  ) +
  theme_minimal() +
  scale_x_continuous(breaks = c(0, 3, 6, 9, 12, 15, 18, 21, 24))
#> Warning in geom_smooth(data = data %>% filter(Lot == 2), method = "lm", :
#> Ignoring unknown parameters: `type`
#> Warning in geom_smooth(data = data %>% filter(Lot != 2), method = "lm", :
#> Ignoring unknown parameters: `type`
#> `geom_smooth()` using formula = 'y ~ x'
#> `geom_smooth()` using formula = 'y ~ x'

创建于 2023 年 10 月 12 日,使用 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.