R - 自举置信区间 - 获取上界和下界的参数 [英] R - Bootstrapped Confidence Interval - Obtain Parameters of Upper and Lower Bounds
本文介绍了R - 自举置信区间 - 获取上界和下界的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码如下:
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])$ (bs =white,las = 1,cex = 1.2)
plot(xs,boot.pdf [,1],type =l,col = rgb(.6,.6,.6,.1),ylim =范围(boot.pdf),
xlab =注释寿命(月),ylab =概率密度,main =概率分布)
for(i in 2:ncol(boot.pdf))lines(xs,boot.pdf [,i],col = rgb(.6,。 (boot.pdf,1,quantile,c(0.025,0.5,0.975))
min.point< - apply(引导。 pdf,1,min,na.rm = TRUE)
max.point< - apply(boot.pdf,1,max,na.rm = TRUE)
lines(xs,q unts [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)
- 我想用R的ggplot软件包绘制相同的图表。任何想法如何我可以用ggplot语法来做到这一点? 我不是
- I would like to plot the same chart with R's ggplot package. Any ideas as to how i might do this with ggplot syntax?
虽然这里是问题2的一些代码。
library(fitdistrplus)#你忘了提及这个
库(ggplot2)
库(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 ='gray')+
geom_line(data = dat2 ,aes(group = quantile,col = quantile),size = 1)+
scale_color_manual(values = c('2.5%'='red','50%'='darkred','97 .5%'= '')+
theme_bw()+ xlab(Note Life(months))+ ylab(Probability density)+
ggtitle(概率分布)
I used bootstrapping to obtain confidence intervals of a Weibull distribution. Then I plotted the Confidence Bands in a plot.
Code is below:
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)
Two questions: 1. How do i obtain the Shape and Scale Parameter values from the lower and upper bounds of the confidence interval plotted (i.e. the two red lines)?
lines(xs, quants[1, ], col="red", lwd=1.5, lty=2)
lines(xs, quants[3, ], col="red", lwd=1.5, lty=2)
解决方案
I'm not sure about question 1.
Here is some code for question 2 though.
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")
这篇关于R - 自举置信区间 - 获取上界和下界的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文