[R] Estimate correlation with bootstrap

Cody Hamilton Cody_Hamilton at Edwards.com
Fri Sep 21 23:01:50 CEST 2007


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.



More information about the R-help mailing list