# Bootstrap the Correlation Coefficient without boot() in R [closed]

I want to understand the bootstrap-method for correlations. Therefore i tried to bootstrap the correlation without the boot() function of R. The problem of bootstrapping correlations are the bivariate data. I found on the internet the following statement:

There is one point to note: because each observation consists of [two features, variables], we resample the observations [and not each feature]. Resampling [the variables] separately would lose the tie between them.

(Bootstrap Methods and Permutation Tests, Hesterberg 2003). Let's have a look to my example now:

I have the following bivariate data (amount of proline and amount of kollagen):

prolin<-read.csv2("Data/Prolin.csv",header=TRUE)

prolin

Prolin Kollagen

1. 7.1  2.8
2. 7.1   2.9
3. 7.2   2.8
4. 8.3   2.6
5. 9.4   3.5
6. 10.5   4.6
7. 11.4   5.0


I first created the bootstrap resamples (for example 2000 of them) with the following R-Code. I sample not only a single observation but a whole row (Is this correct?):

n=2000
resamples <- lapply(1:n,function(i)prolin[sample(nrow(prolin),7,replace=TRUE),])

resamples[ 1 ]

[[ 1 ]]

Prolin Kollagen

1. 8.3  2.6
2. 10.5   4.6
3. 10.5   4.6
4. 7.1   2.9
5. 11.4   5.0
6. 7.1   2.8
7. 8.3   2.6


Then I tried to compute the spearman correlation coefficient to all resamples. Because I had problems with the sapply-function I used a for-loop. Previously I summarized all resamples in a data.frame (to have access to each variable of one resample). Then I compute the correlation coefficient between the first and the second column, the third and the fourth column...of the data frame:

DF<-data.frame(resamples[i])

spcorr=vector(length=0)

for(i in 1:n){spcorr[i]<-cor(DF[,2*i-1],DF[,2*i],method="spearman")}


The length of spcorr is 2000. The distribution of the vector spcorr should be my sample distribution. The problem is, that the histogram of spcorr is not a normal distribution (but should be) (see picture). What did I misunderstand?

Last Qustion: how can i build a 0.95 confidence interval with these data.

## closed as too broad by Firebug, Peter Flom♦Nov 24 '18 at 11:14

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• The Spearman correlation coefficient cannot possibly have a Normal bootstrap distribution because it is limited to the interval $[-1,1]$ and--with such a small dataset--it necessarily has a wide spread. – whuber May 13 '16 at 13:31
• Thanks for your answer. Are my codes correct? Is it possible to create a confidence intervall if the bootstrap distribution is not normal? – Chris May 13 '16 at 13:41

As I understand your question, you ask how to implement bootstrap for bivariate correlation. Your text correctly suggests that rather than sampling cases for each of the variables independently (e.g. $x_i, y_j$), you should rather sample pairs of values ($x_i,y_i$). In multivariate case, if your data is stored in a matrix (observations in rows, variables in columns), you sample with replacement $N$ out of $N$ rows of the matrix to create a resampled one. This is shown on the example below.

dat <- mtcars[, c(1, 3)]
N <- nrow(dat)
R <- 2500

cor.orig <- cor(dat)[1,2]
cor.boot <- NULL

for (i in 1:R) {
idx <- sample.int(N, N, replace = TRUE)
cor.boot[i] <- cor(dat[idx, ])[1,2]
}


On the plot below you can see histogram of bootstrapped correlation estimates with the correlation estimate from the raw data marked by red dashed line.

It is not true that your bootstrap distribution should be normal. I see from your plot that the your correlation estimate is close to $1$. This means that it's bootstrap distributtion simple cannot be symmetric because correlation is bounded in $[-1, 1]$. In such case it will always be skewed and will never look normal-like.

There are multiple ways how you could obtain confidece intervals using bootstrap. The simplest one is to take appropriate quantiles from the bootstrapped sample (i.e. quantile(cor.boot, c(0.025, 0.975)) in the example). Such method will work for non-normal distributions.

• Is there a way to do this without a for loop? – crypdick Nov 17 '17 at 3:09