[R] Estimate correlation with bootstrap

Julian Burgos jmburgos at u.washington.edu
Fri Sep 21 18:24:59 CEST 2007


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



More information about the R-help mailing list