【问题标题】:R - Bootstrapped Confidence Interval - Obtain Parameters of Upper and Lower BoundsR - 自举置信区间 - 获取上下界的参数
【发布时间】:2015-12-13 11:21:47
【问题描述】:

我使用 bootstrapping 来获得 Weibull 分布的置信区间。然后我在一个情节中绘制了置信带。

代码如下:

set.seed(123)
rw.small<-rweibull(100,shape=1.781096,scale=33.669511)
xs <- seq(0,100, len=500)

boot.pdf <- sapply(1:100, function(i) {
     xi <- sample(rw.small, size=length(rw.small), replace=TRUE)
     MLE.est <- suppressWarnings(fitdist(xi, distr="weibull",lower=0))  
     dweibull(xs, shape=MLE.est$estimate["shape"],  scale = MLE.est$estimate["scale"])
 })

par(bg="white",las=1,cex=1.2)

plot(xs, boot.pdf[, 1], type="l", col=rgb(.6, .6, .6, .1), ylim=range(boot.pdf),
      xlab="Note Life (months)", ylab="Probability density",main= "Probability Distribution")

for(i in 2:ncol(boot.pdf)) lines(xs, boot.pdf[, i], col=rgb(.6, .6, .6, .1))
quants <- apply(boot.pdf, 1, quantile, c(0.025, 0.5, 0.975))
min.point <- apply(boot.pdf, 1, min, na.rm=TRUE)
max.point <- apply(boot.pdf, 1, max, na.rm=TRUE)
lines(xs, quants[1, ], col="red", lwd=1.5, lty=2)
lines(xs, quants[3, ], col="red", lwd=1.5, lty=2)
lines(xs, quants[2, ], col="darkred", lwd=2)

两个问题: 1. 如何从绘制的置信区间的上下界(即两条红线)中获取形状和比例参数值?

lines(xs, quants[1, ], col="red", lwd=1.5, lty=2)
lines(xs, quants[3, ], col="red", lwd=1.5, lty=2)
  1. 我想用 R 的 ggplot 包绘制相同的图表。关于如何使用 ggplot 语法做到这一点的任何想法?

【问题讨论】:

    标签: r ggplot2 confidence-interval statistics-bootstrap probability-density


    【解决方案1】:

    我不确定问题 1。

    这里是问题 2 的一些代码。

    library (fitdistrplus) #you forgot to mention this
    library (ggplot2)
    library (tidyr)
    
    dat <- gather(as.data.frame(boot.pdf), i, y, -1)
    dat2 <- gather(as.data.frame(t(quants)), quantile, y)
    dat$x <- dat2$x <- xs
    
    ggplot(dat, aes(x, y, group = i)) +
      geom_line(col = 'grey') +
      geom_line(data = dat2, aes(group = quantile, col = quantile), size = 1) +
      scale_color_manual(values  = c('2.5%' = 'red', '50%' = 'darkred', '97.5%' = 'red')) +
      theme_bw() + xlab("Note Life (months)") + ylab("Probability density") + 
      ggtitle("Probability Distribution")
    

    【讨论】:

    • 感谢 Axeman。对于问题 1,我真的想找到 2.5% 和 97.5% 曲线的 Weibull 形状和比例参数值。例如,原始模型的 shape=1.781096,scale=33.669511。
    • 是的,我明白,但我不知道该怎么做。你可以看看MASS 包中的fitdistr 函数。
    • fits &lt;- apply(quants[, -1], 1, fitdist, 'weibull') 这会运行,但估计与您的预期有很大不同。我猜你的代码有问题,但我不确定是什么。
    猜你喜欢
    • 2012-03-07
    • 1970-01-01
    • 2019-02-28
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多