繁体   English   中英

在 R 中绘制具有 95% 置信区间的阈值/分段/变化点模型

[英]plotting threshold/piecewise/change point models with 95% confidence intervals in R

我想 plot 阈值 model 线段之间具有平滑的 95% 置信区间线。 您会认为这很简单,但我一直无法找到答案!

我的阈值/断点是已知的,如果有办法可视化这些数据,那就太好了。 我尝试了分段的 package,它产生以下 plot:

plot 显示阈值 model,断点为 5.4。 但是,回归线之间的置信区间并不平滑。

如果有人知道在分段回归线(理想情况下在 ggplot 中)之间产生平滑(即没有线段之间的跳跃)CI 线的任何方法,那将是惊人的。 太感谢了。

我已经包含了示例数据和我在下面尝试过的代码:

x <- c(2.26, 1.95, 1.59, 1.81, 2.01, 1.63, 1.62, 1.19, 1.41, 1.35, 1.32, 1.52, 1.10, 1.12, 1.11, 1.14, 1.23, 1.05, 0.95, 1.30, 0.79,
0.81, 1.15, 1.10, 1.29, 0.97, 1.05, 1.05, 0.84, 0.64, 0.80, 0.81, 0.61, 0.71, 0.75, 0.30, 0.30, 0.49, 1.13, 0.55, 0.77, 0.51,
0.67, 0.43, 1.11, 0.29, 0.36, 0.57, 0.02, 0.22, 3.18, 3.79, 2.49, 2.44, 2.12, 2.45, 3.22, 3.44, 3.86, 3.53, 3.13)

y <- c(22.37, 18.93, 16.99, 15.65, 14.62, 13.79, 13.09, 12.49, 11.95, 11.48, 11.05, 10.66, 10.30,  9.96,  9.65,  9.35,  9.07,  8.81,
       8.56,  8.32,  8.09,  7.87,  7.65,  7.45,  7.25,  7.05,  6.86,  6.68,  6.50,  6.32,  6.15,  5.97,  5.80,  5.63,  5.47,  5.30,
        5.13,  4.96,  4.80,  4.63,  4.45,  4.28,  4.09,  3.90,  3.71,  3.50,  3.27,  3.01,  2.70,  2.28, 22.37, 16.99, 11.05,  8.81,
       8.56,  8.32,  7.25,  7.05,  6.50,  6.15,  5.63)

lin.mod <- lm(y ~  x)
segmented.mod <- segmented(lin.mod, seg.Z = ~x, psi=2)
plot(x, y)
plot(segmented.mod, add=TRUE, conf.level = 0.95)

这会产生以下 plot (以及 95% 置信区间的相关跳跃):

分段 plot

背景:现有变更点包中的不平滑是由于常客包以固定的变更点值运行的事实。 但与所有推断的参数一样,这是错误的,因为变化的位置确实存在不确定性。

解决方案: AFAIK,只有贝叶斯方法可以量化它,而mcp package 填补了这个空间。

library(mcp)
model = list(
  y ~ 1 + x,   # Segment 1: Intercept and slope
  ~ 0 + x  # Segment 2: Joined slope (no intercept change)
)
fit = mcp(model, data = data.frame(x, y))

默认 plot ( plot.mcpfit()返回一个ggplot对象):

plot(fit) + ggtitle("Default plot")

默认 mcp 图

每条线代表一个可能的 model 生成数据。 变化点的后验显示为蓝色密度。 您可以单独使用plot(fit, q_fit = TRUE)或 plot 在顶部添加一个可信区间:

plot(fit, lines = 0, q_fit = c(0.025, 0.975), cp_dens = FALSE) + ggtitle("Credible interval only")

mcp 可信区间图

如果您的更改点确实已知并且您想要 model 每个段的不同剩余比例(即,准模拟segmented ),您可以执行以下操作:

model2 = list(
  y ~ 1 + x,
  ~ 0 + x + sigma(1)  # Add intercept change in residual scale
)
fit = mcp(model2, df, prior = list(cp_1 = 1.9))  # Note: prior is a fixed value - not a distribution.
plot(fit, q_fit = TRUE, cp_dens = FALSE)

mcp 模拟分段

请注意,CI 不会像segmented那样围绕更改点“跳跃”。 我相信这是正确的行为。 披露:我是mcp的作者。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM