gpt4 book ai didi

r - 在给定的 x 处添加垂直线段,在两条回归线的截距之间延伸

转载 作者:行者123 更新时间:2023-12-05 09:25:45 25 4
gpt4 key购买 nike

我想在 days==0 处添加一条垂直线,它从 y 的值延伸,其中 days==0x==0,到 y 的值,其中 days==0x==1 中。

df <- structure(list(y = c(3, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 
4, 3, 3, 4, 3, 3, 4, 4, 4, 3, 4, 3, 3, 4, 4, 3, 4, 5, 4, 4, 4,
5, 4, 5, 4, 4, 5, 4, 4, 5, 4, 4, 5, 4, 4, 4, 5, 5, 4, 4, 4, 4,
5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 7, 6, 6, 6, 7,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6), x = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("0",
"1"), class = "factor"), days = c(-8, -50, -84, -91, -69, -87,
-89, -19, -61, -18, -46, -26, -35, -51, -88, -55, -36, -44, -24,
-45, -78, -41, -38, -81, -74, -22, -82, -86, -39, -64, -66, -58,
-25, -5, -29, -34, -30, -75, -57, -37, -32, -77, -31, -59, -67,
-83, -70, -1, -65, -15, -27, -56, -71, -80, -12, -3, -76, -54,
-52, -6, 35, 20, 53, 61, 43, 71, 88, 31, 17, 85, 21, 25, 16,
46, 45, 41, 15, 48, 72, 63, 24, 12, 83, 40, 13, 10, 11, 79, 81,
64, 38, 59, 3, 77, 39, 26, 68, 49, 87, 69, 75, 33, 34, 76, 78,
86, 14, 36, 0, 44, 54, 58, 18, 80, 82, 89, 56, 2, 28, 74)), row.names = c(NA,
-120L), class = c("tbl_df", "tbl", "data.frame"))
# https://evalf20.classes.andrewheiss.com/example/rdd/
library(tidyverse)

df %>%
ggplot(aes(x = days, y = y, color = x)) +
geom_point(size = 2, alpha = 0.5, position = position_jitter(seed = 42)) +
geom_smooth(data = filter(df, days < 0), method = "lm") +
geom_smooth(data = filter(df, days >= 0), method = "lm") +
geom_vline(xintercept = 0) +
labs(x = "Days from cutoff", y = "Outcome") +
guides(color = FALSE)

每条线的斜率可以变化,因此我们不能假设 ydays==0 处的值 x== 0(左)始终位于该段的 y 的最小值,如图所示。

最佳答案

您可以提取geom_smooth 回归线的截距并将它们作为geom_segment 添加到您的图中:

library(tidyverse)

df %>%
ggplot(aes(x = days, y = y, color = x)) +
geom_point(size = 2, alpha = 0.5, position = position_jitter(seed = 42)) +
geom_smooth(data = filter(df, days < 0), method = "lm") +
geom_smooth(data = filter(df, days >= 0), method = "lm") +
geom_vline(xintercept = 0) +
labs(x = "Days from cutoff", y = "Outcome") +
guides(color = "none") -> gg


lm(y~days, data = filter(df, days < 0))-> lm_neg
lm(y~days, data = filter(df, days >= 0))-> lm_pos



gg +
geom_segment(aes(x = 0, y = lm_neg$coefficients[1],
xend = 0, yend = lm_pos$coefficients[1]),
colour = "yellow", size = 2)

关于r - 在给定的 x 处添加垂直线段,在两条回归线的截距之间延伸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75137075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com