简体   繁体   中英

Plotting distribution of bootstrapped values in R

I need to fit a S-shaped distribution ( y = 0~1 ) with 95% confidence interval, and get the x values where y=5% (termed as hc5 ). The original input data file named ZnOLC50.txt :

LC50    Proportion
0.089    0.071428571
0.16    0.214285714
1.155    0.357142857
1.51    0.5
3.97    0.642857143
573.8    0.785714286
789    0.928571429


# Load data

>require(MASS)

>require(ggplot2)

>SSDZnOLC50<-read.delim("ZnOLC50.txt", header = TRUE)

>fitSSDZnOLC50<-fitdistr(SSDZnOLC50$LC50, 'lognormal')


# Extract hc5
>(hc5 <- qlnorm(0.05, meanlog = fitSSDZnOLC50$estimate[1], sdlog = fitSSDZnOLC50$estimate[2]))
[1] 0.01789181


>myboot <- function(fitSSDZnOLC50, p){

# resample from fitted distribution

>xr <- rlnorm(fitSSDZnOLC50$n, meanlog = fitSSDZnOLC50$estimate[1], sdlog = fitSSDZnOLC50$estimate[2])

# fit distribition to new data

>fitr <- fitdistr(xr, 'lognormal')
# return HCp

>hc5r <- qlnorm(p, meanlog = fitr$estimate[1], sdlog = fitr$estimate[2])
return(hc5r)
}


# Get 95% confidence interval
>set.seed(1234)
>hc5_boot <- replicate(1000, myboot(fitSSDZnOLC50, p = 0.05))

>quantile(hc5_boot, probs = c(0.025, 0.5, 0.975))

    2.5%          50%        97.5% 
0.0007278486 0.0370062459 1.4272168899 

Then I want to draw the hc5 distribution based on the generated matrix hc5_boot . First I tried this:

curve(dnorm(x,mean(hc5_boot),sd(hc5_boot)),xlim=c(-1,10),col="Red",lwd=1)

and get the curve as shown in

在此处输入图片说明

Then I also tried

> df <- as.data.frame(hc5_boot)

> ggplot(df, aes(x=hc5_boot)) + geom_density() + scale_x_continuous(limits=c(0,4))

and get the curve as shown in

在此处输入图片说明

However, based on the observation, none of those curves seem like to fit the 2.5% quantile of 0.0007278486 , as the density values are not even close to 0 when x = 0 .

So I was wondering what is wrong and how should I do it.

It sounds like you want to graph the cumulative distribution rather than the density function. To do this, use the pnorm function rather than dnorm which produces the pdf. Here is an example:

set.seed(1234)
mySample <- rnorm(100, 1, 5)
curve(pnorm(x,mean(mySample),sd(mySample)),xlim=c(-10,10),col="Red",lwd=1)

在此处输入图片说明

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