Hello. I would like to estimate the correlation coefficient from two samples with Bootstrapping using the R-function sample(). The problem is, that I have to sample pairwise. For example if I have got two time series and I draw from the first series the value from 1912 I need the value from 1912 from the second sample, too. Example: Imagine that a and b are two time series with returns for example: a <- c(1,2,3,4,5,6,7,8,9,10) b <- c(1,1,56,3,6,6,6,7,2,10) a.sample <- numeric(10) b.sample <- numeric(10) boot.cor.a.b <- numeric(1000) for (i in 1:1000) { for (j in 1:10) { a.sample[j] <- sample(a,1,replace=TRUE) b.sample[j] <- sample(b,1,replace=TRUE) } boot.cor.a.b[i] <- cor(a,b) } The problem here is, that the sampling is independent from each other. So how do I have to change the R-code to get the pairwise sampling mentioned above? I hope you can help me. Sincerely Klein. ________
Try this: a <- c(1,2,3,4,5,6,7,8,9,10) b <- c(1,1,56,3,6,6,6,7,2,10) n <- length(a) boot.cor.a.b <- replicate( 1000, {tmp <- sample(n, replace=TRUE); cor(a[tmp],b[tmp]) } ) hist(boot.cor.a.b) abline( v=c( mean(boot.cor.a.b), median(boot.cor.a.b) ), col=c('blue','green')) Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Andreas Klein > Sent: Thursday, September 20, 2007 11:59 PM > To: r-help at r-project.org > Subject: [R] Estimate correlation with bootstrap > > Hello. > > I would like to estimate the correlation coefficient from two > samples with Bootstrapping using the R-function sample(). > > The problem is, that I have to sample pairwise. For example > if I have got two time series and I draw from the first > series the value from 1912 I need the value from 1912 from > the second sample, too. > > Example: > > Imagine that a and b are two time series with returns for example: > > a <- c(1,2,3,4,5,6,7,8,9,10) > b <- c(1,1,56,3,6,6,6,7,2,10) > > a.sample <- numeric(10) > b.sample <- numeric(10) > boot.cor.a.b <- numeric(1000) > > for (i in 1:1000) > > { > > for (j in 1:10) > > { > > a.sample[j] <- sample(a,1,replace=TRUE) > b.sample[j] <- sample(b,1,replace=TRUE) > > } > > boot.cor.a.b[i] <- cor(a,b) > > } > > The problem here is, that the sampling is independent from each other. > > So how do I have to change the R-code to get the pairwise > sampling mentioned above? > > I hope you can help me. > > Sincerely > Klein. > > > ________ > > ______________________________________________ > 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. >
Hi Andreas, Simply use one call to the sample function. Try this: a <- c(1,2,3,4,5,6,7,8,9,10) b <- c(1,1,56,3,6,6,6,7,2,10) boot.cor.a.b <- numeric(1000) for (i in 1:1000){ x=sample(10,replace=T) boot.cor.a.b[i]=cor(a[x],b[x]) } Note that in R you don't need to initialize the variables like in other languages. Another option is to use the boot() function directly. See ?boot. Julian Andreas Klein wrote:> Hello. > > I would like to estimate the correlation coefficient > from two samples with Bootstrapping using the > R-function sample(). > > The problem is, that I have to sample pairwise. For > example if I have got two time series and I draw from > the first series the value from 1912 I need the value > from 1912 from the second sample, too. > > Example: > > Imagine that a and b are two time series with returns > for example: > > a <- c(1,2,3,4,5,6,7,8,9,10) > b <- c(1,1,56,3,6,6,6,7,2,10) > > a.sample <- numeric(10) > b.sample <- numeric(10) > boot.cor.a.b <- numeric(1000) > > for (i in 1:1000) > > { > > for (j in 1:10) > > { > > a.sample[j] <- sample(a,1,replace=TRUE) > b.sample[j] <- sample(b,1,replace=TRUE) > > } > > boot.cor.a.b[i] <- cor(a,b) > > } > > The problem here is, that the sampling is independent > from each other. > > So how do I have to change the R-code to get the > pairwise sampling mentioned above? > > I hope you can help me. > > Sincerely > Klein. > > > ________ > > ______________________________________________ > 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. > >-- Julian M. Burgos Fisheries Acoustics Research Lab School of Aquatic and Fishery Science University of Washington 1122 NE Boat Street Seattle, WA 98105 Phone: 206-221-6864
Andreas, To do pairwise bootstrap sampling, try for (i in 1:1000) { ind<-sample(1:10,10,replace=TRUE) boot.com.ab[i]<-cor(a[ind],b[ind]) } Regards, -Cody Cody Hamilton Edwards Lifesciences -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Andreas Klein Sent: Thursday, September 20, 2007 10:59 PM To: r-help at r-project.org Subject: [R] Estimate correlation with bootstrap Hello. I would like to estimate the correlation coefficient from two samples with Bootstrapping using the R-function sample(). The problem is, that I have to sample pairwise. For example if I have got two time series and I draw from the first series the value from 1912 I need the value from 1912 from the second sample, too. Example: Imagine that a and b are two time series with returns for example: a <- c(1,2,3,4,5,6,7,8,9,10) b <- c(1,1,56,3,6,6,6,7,2,10) a.sample <- numeric(10) b.sample <- numeric(10) boot.cor.a.b <- numeric(1000) for (i in 1:1000) { for (j in 1:10) { a.sample[j] <- sample(a,1,replace=TRUE) b.sample[j] <- sample(b,1,replace=TRUE) } boot.cor.a.b[i] <- cor(a,b) } The problem here is, that the sampling is independent from each other. So how do I have to change the R-code to get the pairwise sampling mentioned above? I hope you can help me. Sincerely Klein. ________ ______________________________________________ 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.