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?
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With