D Cornacchia
2010-Jun-09 23:11 UTC
[R] Finding the bootstrapped coefficient of variation and the stderr on the CV(boot)
Dear R-Helpers, I am trying to bootstrap the coefficient of variation on a suite of vectors, here I provide an example using one of the vectors in my study. When I ran this script with the vector x <-c(0.625, 0.071428571, 0.133333333, 0.125, 0), it returned CV(boot) [the second one], and stderr(boot) [the second one] without problem. However, when I ran it with the vector in the following example, I received NA for the latter two statistics. I cannot figure out what is missing, why it won't work with this vector, and how I can rectify this situation. Your input would be greatly appreciated! x <-c(1, 0, 0.153846154, 0, 0.142857143) mean (x) sample(x,replace=T) mean(sample(x,replace=T)) boot <-numeric(991) for (i in 1:991) boot[i] <- mean(sample(x,replace=T)) mean(boot) var(boot) sd(boot) stderr <- function(boot) sqrt(var(boot)/length(boot)) stderr(boot) median(boot) min(boot) max(boot) range(boot) IQR(boot) quantile(boot,0.975) quantile(boot,0.025) bias <- mean(boot) - mean(x) mean(x) - bias mean(x) - bias - 1.96*sqrt(var(boot)) mean(x) - bias + 1.96*sqrt(var(boot)) CV <- function(x) sqrt(var(x))/mean(x) CV(x) CV(boot) N <- boot CV(x) CV(sample(x,replace=T)) boot <-numeric(991) for (i in 1:991) boot[i] <- CV(sample(x,replace=T)) CV(boot) stderr(boot) P <- boot hist(N) t.test (x, N) ks.test(x, N) Many thanks, D. p.s. I am very new to R. [[alternative HTML version deleted]]
Jorge Ivan Velez
2010-Jun-09 23:38 UTC
[R] Finding the bootstrapped coefficient of variation and the stderr on the CV(boot)
Hi D.,
One option would be to use the boot library:
require(boot)
x <-c(1, 0, 0.153846154, 0, 0.142857143)
mycv <- function(x, id){
xs <- x[id]
sd(xs)/mean(xs)
}
my.boot <- boot(x, mycv, R = 999)
my.boot
plot(my.boot)
boot.ci(my.boot)
Also, take a look at [1], [2] and [3].
HTH,
Jorge
[1] http://www.statmethods.net/advstats/bootstrapping.html
[2] http://www.ats.ucla.edu/stat/r/library/bootstrap.htm
[3] http://lib.stat.cmu.edu/S/Spoetry/Tutor/bootstrap_resampling.html
On Wed, Jun 9, 2010 at 7:11 PM, D Cornacchia <> wrote:
> Dear R-Helpers,
>
> I am trying to bootstrap the coefficient of variation on a suite of
> vectors, here I provide an example using one of the vectors in my
> study. When I ran this script with the vector x <-c(0.625,
> 0.071428571, 0.133333333, 0.125, 0), it returned CV(boot) [the second
> one], and stderr(boot) [the second one] without problem. However, when
> I ran it with the vector in the following example, I received NA for
> the latter two statistics. I cannot figure out what is missing, why it
> won't work with this vector, and how I can rectify this situation.
> Your input would be greatly appreciated!
>
> x <-c(1, 0, 0.153846154, 0, 0.142857143)
> mean (x)
> sample(x,replace=T)
> mean(sample(x,replace=T))
> boot <-numeric(991)
> for (i in 1:991) boot[i] <- mean(sample(x,replace=T))
> mean(boot)
> var(boot)
> sd(boot)
> stderr <- function(boot) sqrt(var(boot)/length(boot))
> stderr(boot)
> median(boot)
> min(boot)
> max(boot)
> range(boot)
> IQR(boot)
> quantile(boot,0.975)
> quantile(boot,0.025)
> bias <- mean(boot) - mean(x)
> mean(x) - bias
> mean(x) - bias - 1.96*sqrt(var(boot))
> mean(x) - bias + 1.96*sqrt(var(boot))
> CV <- function(x) sqrt(var(x))/mean(x)
> CV(x)
> CV(boot)
> N <- boot
> CV(x)
> CV(sample(x,replace=T))
> boot <-numeric(991)
> for (i in 1:991) boot[i] <- CV(sample(x,replace=T))
> CV(boot)
> stderr(boot)
> P <- boot
> hist(N)
> t.test (x, N)
> ks.test(x, N)
>
> Many thanks,
> D.
>
> p.s. I am very new to R.
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]