简体   繁体   中英

A Boxplot in R to compare the results of 95% confidence Intervals of different sample size

I am trying to show the effect of sample size of a gamma distribution on the bounds of a 95% confidence interval by using bootstrapping. Now, I need to put together the results of 4 different sample sizes in a single boxplot. The R code follows:

y <- rgamma(30,1,1) + rnorm(30,0,0.01)
y60 <- rgamma(60,1,1) + rnorm(60,0,0.01)
y100 <- rgamma(100,1,1) + rnorm(100,0,0.01)
y200 <- rgamma(200,1,1) + rnorm(200,0,0.01)
minusL <- function(params, data) {
-sum(log(dgamma(data, params[1], params[2])))
}
fit <- nlm(minusL, c(1,1), data=y)
fit
gammamedian<-function(data) {
fit <- nlm(minusL, c(1,1), data=data)
qgamma(.5, fit$estimate[1], fit$estimate[2])
}
gammamedian(y)
gammamedian(y60)
gammamedian(y100)
gammamedian(y200)
gengamma<- function(data, params){
rgamma(length(data), params[1], params[2])}
library(boot)
pbootresults<-boot(y, gammamedian, R=1000, sim="parametric",            
ran.gen=gengamma, mle=fit$estimate)
pbootresults
boot.ci(pbootresults, type=c("basic", "perc", "norm"))
pbootresults<-boot(y60, gammamedian, R=1000, sim="parametric",  
ran.gen=gengamma, mle=fit$estimate)
pbootresults
boot.ci(pbootresults, type=c("basic", "perc", "norm"))
pbootresults<-boot(y100, gammamedian, R=1000, sim="parametric",     
ran.gen=gengamma, mle=fit$estimate)
pbootresults
boot.ci(pbootresults, type=c("basic", "perc", "norm"))
pbootresults<-boot(y200, gammamedian, R=1000, sim="parametric",  
ran.gen=gengamma, mle=fit$estimate)
pbootresults
boot.ci(pbootresults, type=c("basic", "perc", "norm"))

 [An Excel image example ][1]


 [1]: https://i.stack.imgur.com/JXR6P.jpg

You have to store the min and max values in a datatable and then use the geom_rect()

I give a quick example for two of the cases and similar you can do for the rest and also play with the aestetics to have a better view.

library(boot)
pbootresults<-boot(y, gammamedian, R=1000, sim="parametric",            
                   ran.gen=gengamma, mle=fit$estimate)
pbootresults
temp2 <-boot.ci(pbootresults, type=c("basic", "perc", "norm")) # store teh results
pbootresults<-boot(y60, gammamedian, R=1000, sim="parametric",  
                   ran.gen=gengamma, mle=fit$estimate)
pbootresults
boot.ci(pbootresults, type=c("basic", "perc", "norm"))
pbootresults<-boot(y100, gammamedian, R=1000, sim="parametric",     
                   ran.gen=gengamma, mle=fit$estimate)
pbootresults
 boot.ci(pbootresults, type=c("basic", "perc", "norm"))
pbootresults<-boot(y200, gammamedian, R=1000, sim="parametric",  
                   ran.gen=gengamma, mle=fit$estimate)
pbootresults
temp <- boot.ci(pbootresults, type=c("basic", "perc", "norm")) # store the results

datatable <- temp$percent %>% as.data.table()
datatable <- rbind(datatable, temp2$percent %>% as.data.table())
datatable[, group :=.I]

 ggplot(data = datatable) + geom_rect(mapping =  aes(xmin = group - 0.25, xmax = group+ 0.25, ymin = V4, ymax = V5))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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