Tomas Zelinsky
2010-Jan-21 12:45 UTC
[R] Estimation of S.E. based on bootstrapping (functions with two or more arguments)
Hi all, I need to estimate S.E. of a certain indicator. The function to compute the value of indicator contains two arguments. Can anybody tell me how to do it? Example: We have data: a <- c(1:10) b <- c(11:20) data <- data.frame(a, b) Function to compute value of the indicator: indicator <- function(X, Y) sum(X)/(sum(Y)*2) Next I need to do the bootstrapping and estimate mean value of indicator and its standard error. If the function (indicator in my case) contained only one argument, there would not be a problem, the code would look like: resamples <- lapply(1:1000, function(i) sample(data, replace = T)) r.indicator <- sapply(resamples, indicator) mean(r.indicator) sqrt(var(r.indicator)) But in case of function with two arguments it doesn???t work. I tried to do it like: resamples <- lapply(1:1000, function(i) data[sample(1:nrow(data), replace TRUE),]) r.indicator <- sapply(resamples, indicator) but it didn't work. Can anybody help? Thanks a lot. Tomas __________ Informacia od ESET NOD32 Antivirus, verzia databazy 4792 (20100121) __________ Tuto spravu preveril ESET NOD32 Antivirus. [1]http://www.eset.sk References 1. http://www.eset.sk/
Chuck Cleland
2010-Jan-21 13:11 UTC
[R] Estimation of S.E. based on bootstrapping (functions with two or more arguments)
On 1/21/2010 7:45 AM, Tomas Zelinsky wrote:> Hi all, > > I need to estimate S.E. of a certain indicator. The function to compute the > value of indicator contains two arguments. Can anybody tell me how to do it? > > Example: > > We have data: > a <- c(1:10) > b <- c(11:20) > data <- data.frame(a, b) > > Function to compute value of the indicator: > indicator <- function(X, Y) sum(X)/(sum(Y)*2) > > Next I need to do the bootstrapping and estimate mean value of indicator and > its standard error. > > If the function (indicator in my case) contained only one argument, there > would not be a problem, the code would look like: > > resamples <- lapply(1:1000, function(i) sample(data, replace = T)) > r.indicator <- sapply(resamples, indicator) > mean(r.indicator) > sqrt(var(r.indicator)) > > > But in case of function with two arguments it doesn???t work. I tried to do it > like: > resamples <- lapply(1:1000, function(i) data[sample(1:nrow(data), replace > TRUE),]) > r.indicator <- sapply(resamples, indicator) > > but it didn't work. > > > Can anybody help?How about using boot() in package boot? Using your example: a <- c(1:10) b <- c(11:20) DF <- data.frame(a,b) library(boot) boot(DF, statistic = function(d, ind){sum(d$a[ind])/sum(d$b[ind]*2)}, R = 1000) ORDINARY NONPARAMETRIC BOOTSTRAP Call: boot(data = DF, statistic = function(d, ind) { sum(d$a[ind])/sum(d$b[ind] * 2) }, R = 1000) Bootstrap Statistics : original bias std. error t1* 0.1774194 -0.001594390 0.01902264> Thanks a lot. > > Tomas > > __________ Informacia od ESET NOD32 Antivirus, verzia databazy 4792 > (20100121) __________ > Tuto spravu preveril ESET NOD32 Antivirus. > [1]http://www.eset.sk > > References > > 1. http://www.eset.sk/ > ______________________________________________ > R-help at 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.-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894