[英]plotting threshold/piecewise/change point models with 95% confidence intervals in R
I would like to plot a threshold model with smooth 95% confidence interval lines between line segments.我想 plot 阈值 model 线段之间具有平滑的 95% 置信区间线。 You would think this would be on the simple side but I have not been able to find an answer!
您会认为这很简单,但我一直无法找到答案!
My threshold/breakpoints are known, it would be great if there were a way to visualize this data.我的阈值/断点是已知的,如果有办法可视化这些数据,那就太好了。 I have tried the segmented package which produces the following plot:
我尝试了分段的 package,它产生以下 plot:
The plot shows a threshold model with a breakpoint at 5.4. plot 显示阈值 model,断点为 5.4。 However, the confidence intervals are not smooth between regression lines.
但是,回归线之间的置信区间并不平滑。
If anyone knows of any way to produce smooth (ie without the jump between line segments) CI lines between segmented regression lines (ideally in ggplot) that would be amazing.如果有人知道在分段回归线(理想情况下在 ggplot 中)之间产生平滑(即没有线段之间的跳跃)CI 线的任何方法,那将是惊人的。 Thank you so much.
太感谢了。
I have included sample data and the code I have tried below:我已经包含了示例数据和我在下面尝试过的代码:
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)
which produces the following plot (and associated jumps in 95% confidence intervals):这会产生以下 plot (以及 95% 置信区间的相关跳跃):
Background: The non-smoothness in existing change point packages are due to the fact that frequentist packages operate with a fixed change point value.背景:现有变更点包中的不平滑是由于常客包以固定的变更点值运行的事实。 But as with all inferred parameters, this is wrong because there is indeed uncertainty concerning the location of the change.
但与所有推断的参数一样,这是错误的,因为变化的位置确实存在不确定性。
Solution: AFAIK, only Bayesian methods can quantify that and the mcp
package fills this space.解决方案: 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))
Default plot ( plot.mcpfit()
returns a ggplot
object):默认 plot (
plot.mcpfit()
返回一个ggplot
对象):
plot(fit) + ggtitle("Default plot")
Each line represents a possible model that generated the data.每条线代表一个可能的 model 生成数据。 The posterior for the change point is shown as a blue density.
变化点的后验显示为蓝色密度。 You can add a credible interval on top using
plot(fit, q_fit = TRUE)
or plot it alone:您可以单独使用
plot(fit, q_fit = TRUE)
或 plot 在顶部添加一个可信区间:
plot(fit, lines = 0, q_fit = c(0.025, 0.975), cp_dens = FALSE) + ggtitle("Credible interval only")
If your change point is indeed known and if you want to model different residual scales for each segment (ie, quasi-emulate segmented
), you can do:如果您的更改点确实已知并且您想要 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)
Notice that the CI does not "jump" around the change point as in segmented
.请注意,CI 不会像
segmented
那样围绕更改点“跳跃”。 I believe that this is the correct behavior.我相信这是正确的行为。 Disclosure: I am the author of
mcp
.披露:我是
mcp
的作者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.