[R] confidential interval of correlation coefficient using bootstrap

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Apr 10 22:35:56 CEST 2004


You are not using boot() correctly.  Please do try reading the help page, 
which says

statistic: ...

  In all other cases 'statistic' must take at least two arguments.  The
  first argument passed will always be the original data. The second will
  be a vector of indices ...

so you need a function of two args like

> f <- function(data, i) cor(data[i, 1], data[i, 2])
> boot(cbind(x,y), f, 200)

ORDINARY NONPARAMETRIC BOOTSTRAP


Call:
boot(data = cbind(x, y), statistic = f, R = 200)


Bootstrap Statistics :
     original     bias    std. error
t1* 0.5711816 0.05765583   0.2178101


However, once you have done that you need a way to get a confidence 
interval, and you will find ?abc.ci has an example for you, using 
correlations.


On Sat, 10 Apr 2004, XIAO LIU wrote:

> I tried 2 methods to estimate C.I. of correlation coefficient of variables x and y:
> 
> > x <- c(44.4, 45.9, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1)
> > y <- c( 2.6,  3.1,  2.5,  5.0,  3.6,  4.0,  5.2,  2.8,  3.8)
> 
> #METHOD 1: Pearson's
> **********************************************************
> > cor.test(x, y, method = "pearson", conf.level = 0.95)
> 
>         Pearson's product-moment correlation
> 
> data:  x and y 
> t = 1.8411, df = 7, p-value = 0.1082
> alternative hypothesis: true correlation is not equal to 0 
> 95 percent confidence interval:
>  -0.1497426  0.8955795 
> sample estimates:
>       cor 
> 0.5711816 
> ***********************************************************
> 
> #METHOD 2: bootstrap
> ***********************************************************
> > boot(cbind(x,y), cor, 200)
> 
> ORDINARY NONPARAMETRIC BOOTSTRAP
> 
> 
> Call:
> boot(data = cbind(x, y), statistic = cor, R = 200)
> 
> 
> Bootstrap Statistics :
>      original     bias    std. error
> t1* 0.5429120 -0.5232893   0.3799117
> t2* 0.3832856 -0.3779026   0.3876666
> ************************************************************
> 
> 'cor.test' gave the value of cor as 0.5711816.  Why did 'boot' give two original cors: t1 and t2.  And why does none of t1 and t2 equal to what 'cor.test' gave.
> 
> Thank you very much
> 
> Xiao

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list