简体   繁体   English

从 R 中的包“mcp”输出中绘制 50% 分位数和可信区间

[英]Plotting both 50% quantile and credible interval from package 'mcp' output in R

I'm trying to plot both the 95% credible intervals and the line for the 50% median on the same graph using output from the "mcp" package in R. I used the mcp package to model different residual scales (ie, quasi emulate) for three segments with known breakpoints (2013 and 2017).我正在尝试使用 R 中“mcp”包的输出在同一图表上绘制 95% 可信区间和 50% 中位数的线。我使用 mcp 包来模拟不同的残差尺度(即准模拟) 用于具有已知断点的三个细分市场(2013 年和 2017 年)。 However, I can't seem to fit both the CI and a line for the 50% median on the same plot using the q_fit from the model.但是,我似乎无法使用模型中的 q_fit 在同一图上同时拟合 CI 和 50% 中位数的线。 I've included some sample code below.我在下面包含了一些示例代码。 My end goal is to have a single segmented line representing the 50% quantile from the model with an upper and lower CI.我的最终目标是用一条分割线代表模型中具有上下 CI 的 50% 分位数。 I've included some sample data and code below, along with the to separate plots that I would like to combine.我在下面包含了一些示例数据和代码,以及我想合并的单独图。

year <-c(2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020)
counts <-c(40,50,200,50,20,30,15,12,10,12,12,200,1000,4000,6000,4500,5000,6000)

df <- data.frame(year,counts)


model = list(
  counts ~ 1 + year,        #segmented 1: Intercept and Slope
  ~ 0 + year + sigma (1),   #Segement 2: Joined slope (no intercept change)
  ~ 0 + year + sigma (1)    #Segment 3: Joined slope (no intercept change)
)

fit = mcp(model, df, prior = list(cp_1 = 2013, cp_2 = 2017))

plot(fit, q_fit = TRUE, cp_dens = FALSE, lines=50) #plot 95% CI


plot(fit, q_fit = c(0.5), cp_dens = FALSE, lines=50) #plot 50% quantile

First plot with 95% CI具有 95% CI 的第一个图

50% quantile line 50% 分位数线

You can plot an arbitrary number of quantiles if you supply them as a vector to q_fit :如果将它们作为向量提供给q_fit则可以绘制任意数量的分位数:

plot(fit, q_fit = c(0.025, 0.5, 0.975), cp_dens = FALSE, lines=50)

在此处输入图片说明

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

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