# How to resample matrices to test for the robustness of their correlation?

I have several populations where I have morphology and diet for each individual. I am interested in the correlation between diet and morphological distances. However the number of individuals in each population ranges from 22 to 80 individuals. I have looked at the correlation diet-morphology for each population and (not surprisingly) the correlation coefficientis is highly correlated with the number of individuals per population.

I would like to resample (without replacement) the populations with 60-80 individuals and get random samplings of 30 individuals (1000 times). I would like to get a correlation coefficient distribution against which to test the original value of the correlation.

I guess it is possible do this in R, however I have never written a script in R and I am not familiar with resampling techniques at all. Any help with coding will be greatly appreciated

Thank you Camille

• Why is it not surprising that the correlation coefficient is highly correlated with the number of individuals per population? I see no obvious reason to expect it to be. – onestop Feb 17 '12 at 17:31
• We can answer your implicit question without any simulation: although the Pearson correlation coefficient is a biased estimator of the correlation, the bias is usually small, so the center of the sampling distribution you propose will be close to the correlation coefficient of the data from which you are sampling. – whuber Feb 17 '12 at 18:05

It would help if you could address @onestop's question, and I also agree with @whuber that using the correlation's you've found are probably fine. But I will try to provide some help with your question. Usually, when we resample, we do so with replacement, and we take a bootsample of the same size as the original sample. That is, if your sample is size 80, we resample with replacement to get 80 bootobservations in our bootsample (which will include some duplicates).

There are at least two packages in R that do bootstrapping, but I usually just do it myself. It's not that much more code and I like setting it up according to my specifications. Let's say that your data are in a matrix with 2 columns and 80 rows. You can bootstrap a correlation by sampling rows with replacement, calculating the correlation and storing it. Here's some sample R code:

set.seed(1)                            # this is for reproducability
X        = matrix(rnorm(160), ncol=2)  # a generic matix, note that I made no effort
# to correlate the data, this is just an example
rows     = c(1:80)                     # row numbers to resample over
bootDist = c()                         # a vector to store the output

for(i in 1:1000) {                                 # used a for loop, but it took 1 sec
bootRows = sample(rows, size=80, replace=T)      # this gives me the rows I will use
bootDist[i] = cor(X[bootRows,1], X[bootRows,2])  # the cor for that bootsample
}

summary(bootDist)
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-0.5573 -0.3765 -0.2997 -0.2916 -0.2128  0.2336

• Thank you for your answer. It was very helpful. I use the following code: rows = c(1:2346) bootDist = c() for(i in 1:1000) { bootRows = sample(rows, size=435, replace=F) bootDist[i] = cor(tjornres[bootRows,1], tjornres[bootRows,2]) } summary(bootDist) hist(bootDist,) I hope this script will help other student. – Camille Feb 21 '12 at 11:44