zero null hypothesis using R

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?


It doesn't say in the question, but if you can live with Pearson assumptions (bivariate normal), you can just look to the upper bound of the confidence interval. Any null hypothesis like yours that is greater than that would be rejected at p<0.05.

> cor.test(x, y, method = c("pearson"))$conf
[1] 0.7757901 0.9629837

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)
链接地址: http://www.djcxy.com/p/9906.html

上一篇: 如何将一个原始指针传递给Boost.Python?

下一篇: 使用R的零零假设