[R] Estimate correlation with bootstrap

Greg Snow Greg.Snow at intermountainmail.org
Fri Sep 21 17:58:41 CEST 2007


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.
> 



More information about the R-help mailing list