https://github.com/cran/coin
Raw File
Tip revision: 1d1da53d71a24a7cd371709574597ca3332c7d69 authored by Torsten Hothorn on 14 March 2005, 00:00:00 UTC
version 0.2-9
Tip revision: 1d1da53
regtest_correlation.R

### Regression tests for the correlation problem, i.e.,
### testing the independence of two numeric variables 
### `x' and `y' (possibly blocked)

set.seed(290875)
library(coin)
isequal <- coin:::isequal

### generate data
dat <- data.frame(x = rnorm(100), y = rnorm(100), block = gl(10, 10))

### not really the same, T = (rank(x) - rank(y))^2 is used here
cor.test(~ x + y, data = dat, method = "spearman")$p.value
cor.test(~ x + y, data = dat, alternative = "less", method = "spearman")$p.value
cor.test(~ x + y, data = dat, alternative = "greater", method = "spearman")$p.value

### without blocks
pvalue(spearman_test(y ~ x, data = dat))
pvalue(spearman_test(x ~ y, data = dat))
pvalue(spearman_test( ~ y + x, data = dat))
pvalue(spearman_test( ~ x + y, data = dat))

### with blocks
pvalue(spearman_test(y ~ x | block, data = dat))
pvalue(spearman_test(x ~ y | block, data = dat))
pvalue(spearman_test( ~ y + x | block, data = dat))
pvalue(spearman_test( ~ x + y | block, data = dat))

### sanity checks, those should be errors
dat <- data.frame(x = gl(2, 50), y = rnorm(100), block = rnorm(100))
try(spearman_test(y ~ x, data = dat))
try(spearman_test(y ~ x | block, data = dat))
back to top