Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correlation significance for non-zero null hypothesis using R

Tags:

r

correlation

I'm testing the correlation between two variables:

set.seed(123)
x <- rnorm(20)
y <- x + x * 1:20
cor.test(x, y, method = c("spearman"))

which gives:

Spearman's rank correlation rho

data:  x and y 
S = 54, p-value = 6.442e-06
alternative hypothesis: true rho is not equal to 0 
sample estimates:
   rho 
0.9594 

The p-value is testing the null hypothesis that the correlation is zero. Is there an R function that will allow me to test a different null hypothesis - say that the correlation is less than or equal to 0.3?

like image 549
Steve Avatar asked Nov 13 '11 17:11

Steve


People also ask

How do you know if a correlation is significant in R?

If r< negative critical value or r> positive critical value, then r is significant. Since r=0.801 and 0.801>0.632, r is significant and the line may be used for prediction.

How do you know if a correlation coefficient is statistically significant?

Compare r to the appropriate critical value in the table. If r is not between the positive and negative critical values, then the correlation coefficient is significant. If r is significant, then you may want to use the line for prediction. Suppose you computed r = 0.801 using n = 10 data points.


1 Answers

You can use bootstrap to calculate the confidence interval for rho:

1) Make function to extract the estimate of the cor.test (remember to put indices so the boot can sample the data):

rho <- function(x, y, indices){
  rho <- cor.test(x[indices], y[indices],  method = c("spearman"))
  return(rho$estimate)
}

2) Use the boot package to bootstrap your estimate:

library(boot)    
boot.rho <- boot(x ,y=y, rho, R=1000)

3) Take the confidence interval:

boot.ci(boot.rho)
like image 115
Carlos Cinelli Avatar answered Sep 27 '22 19:09

Carlos Cinelli