【发布时间】:2020-07-03 02:50:22
【问题描述】:
我想很好地填充置信区间边界(区域的上限或下限之间)和 0 之间的区域,使用不同的颜色表示正数或负数(低于或高于 0)。
我设法接近了我的预期,但每个区域的边界都有一些人工制品。关于如何解决出现在边缘的这些问题的任何想法?谢谢!
library(tidyverse)
library(modelbased)
data <- data.frame(x = rnorm(100, 0, 3))
data$y <- cos(data$x) + rnorm(100, 0, 0.5)
newdata <- lm(y ~ poly(x, 6), data=data) %>%
modelbased::estimate_link(length=50)
head(newdata)
#> x Predicted CI_low CI_high
#> 1 -6.387080 0.9694282 0.1751638 1.763693
#> 2 -6.114378 1.3088500 0.7334309 1.884269
#> 3 -5.841676 1.3578983 0.8677253 1.848071
#> 4 -5.568974 1.2084960 0.7437400 1.673252
#> 5 -5.296272 0.9360337 0.4889862 1.383081
#> 6 -5.023570 0.6010678 0.1821074 1.020028
newdata <- newdata %>%
rowwise() %>%
mutate(Limit = min(abs(CI_low), abs(CI_high)),
Positive = ifelse(CI_low > 0, Limit, NA),
Negative = ifelse(CI_low < 0, -Limit, NA))
newdata %>%
ggplot(aes(x=x, y=Predicted)) +
geom_ribbon(aes(ymin = 0, ymax = Positive), fill="green") +
geom_ribbon(aes(ymin = Negative, ymax = 0), fill="red") +
geom_ribbon(aes(ymin=CI_low, ymax=CI_high), alpha=0.2) +
geom_line() +
geom_hline(yintercept=0)
由reprex package (v0.3.0) 于 2020 年 7 月 3 日创建
【问题讨论】: