Description Usage Arguments Details Value Note See Also Examples
Tests the association/correlation for continuous paired samples using corrected versions of the Pearson's correlation test, Kendall's tau test and Spearman's rho test. These three tests are asymptotically calibrated.
1 2 3 4 5 6 7 8 | cortest(
x,
y,
alternative = "two.sided",
method = "pearson",
ties.break = "none",
conf.level = 0.95
)
|
x, y |
the two continuous variables. Must be of same length. |
alternative |
indicates the alternative hypothesis and must be one of "two.sided", "greater" or "less". |
method |
a character string indicating which test to implement. Can be |
ties.break |
the method used to break ties in case there are ties in the x or y vectors. Can be |
conf.level |
confidence level for the confidence interval of the correlation coefficient. It is used only for the Pearson's correlation test. |
Three tests are implemented. The null hypothesis for the corrected Pearson test is: H0 Cor(X,Y)=0 where Cor represents the Pearson correlation coefficient.
The alternative is specified by the alternative
argument. The null hypothesis for the corrected Kendall test is: H0 tau=0 where tau represents the Kendall's tau coefficient.
The null hypothesis for the corrected Spearman test is: H0 rho=0 where rho represents the Spearman's rho coefficient.
All tests are asymptotically calibrated in the sense that the rejection probability under the null hypothesis is asymptotically equal to the level of the test.
When the Pearson's correlation test is used, a confidence interval for the Pearson correlation coefficient is also returned. This confidence interval has been implemented from the delta-method. It should be noted that this method is asymptotic and can display very narrow intervals for small sample sizes and thus can suffer from low coverage probabilities. We therefore advocate to use confidence intervals for the Pearson coefficient only when n is at least larger than 100.
Returns the result of the test with its corresponding p-value, the value of the test statistic and the estimated value of the Pearson correlation coefficient, Kendall's tau or Spearman's rho. For the Pearson's correlation test an asymptotic confidence interval for the correlation coefficient is also returned.
The option ties.break
handles ties in the Kendall and Spearman test. If ties.break="none"
the ties are ignored, if ties.break="random"
they are randomly broken.
Note that only the ties inside each vector are broken (but not ties between vectors).
vartest
, indeptest
, mediantest
, wilcoxtest
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | #Application on the Evans dataset
#Description of this dataset is available in the lbreg package
data(Evans)
with(Evans,cor.test(CHL[CDH==1],DBP[CDH==1]))
with(Evans,cortest(CHL[CDH==1],DBP[CDH==1]))
#The pvalues are very different!
with(Evans,cortest(CHL[CDH==1],DBP[CDH==1],method="kendall",ties.break="random"))
with(Evans,cortest(CHL[CDH==1],DBP[CDH==1],method="spearman",ties.break="random"))
#We use the function tiebreak to remove ties and compare the results from cor.test with cortest
X=tiebreak(Evans$CHL[Evans$CDH==1])
Y=tiebreak(Evans$DBP[Evans$CDH==1])
cor.test(X,Y,method="kendall")
cortest(X,Y,method="kendall")
cor.test(X,Y,method="spearman")
cortest(X,Y,method="spearman")
#Simulated data
n=100
M=10000 #number of replications
testone=function(n){
X=rnorm(n,0,1)
epsi=rnorm(n,0,1)
Y=X^2+0.3*epsi
list(test1=cortest(X,Y)$p.value,test2=cor.test(X,Y)$p.value) #cor.test is the standard Pearson test
}
res1=res2=rep(NA,M)
# Replications to check if the the corrected Pearson test and the standard test are well calibrated
for (i in 1:M)
{
result=testone(n)
res1[i]=result$test1
res2[i]=result$test2
}
mean(res1<0.05) #0.0495
mean(res2<0.05) #0.3674
#Replications with Kendall test (takes long time to run)
M=1000
testone=function(n){
X=rnorm(n,0,1)
epsi=rnorm(n,0,1)
Y=X^2+0.3*epsi
list(test1=cortest(X,Y)$p.value,test2=cor.test(X,Y)$p.value,
test3=cortest(X,Y,method="kendall")$p.value,
test4=cor.test(X,Y,method="kendall")$p.value)
#cor.test is the standard Pearson or Kendall correlation test
}
res1=res2=res3=res4=rep(NA,M)
# Replications to check if the the tests are well calibrated
for (i in 1:M)
{
result=testone(n)
res1[i]=result$test1
res2[i]=result$test2
#res3[i]=result$test3
#res4[i]=result$test4
}
mean(res1<0.05) #0.039
mean(res2<0.05) #0.346
#mean(res3<0.05) #0.044
#mean(res4<0.05) #0.154
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.