R language Access Menu

Title Text Both  

Testing correlation

Pearson's correlation coefficient
Pearson's correlation coefficient is the standard method to assess correlations between numeric variables. This can be easily performed in R wit cor.test function as follows: 


               > with(bwdf, cor.test(age, lwt))

               Pearson's product-moment correlation

              data:  age and lwt
              t = 2.5034, df = 187, p-value = 0.01316
              alternative hypothesis: true correlation is not equal to 0
              95 percent confidence interval:
              0.03832798 0.31471467
              sample estimates:

Non-parametric methods need to be used when the variables are not normally distributed and for small sample sizes. Spearman's and Kendall's methods are non-parametric tests of correlation used, and they can be performed in R as follows: 


                   > with(bwdf, cor.test(age, lwt, method='spearman'))

                  Spearman's rank correlation rho

                 data:  age and lwt
                 S = 915830, p-value = 0.01037
                 alternative hypothesis: true rho is not equal to 0
                 sample estimates:

                 Warning message:
                 In cor.test.default(age, lwt, method = "spearman") :
                Cannot compute exact p-value with ties
                > with(bwdf, cor.test(age, lwt, method='kendall'))

                Kendall's rank correlation tau

               data:  age and lwt
               z = 2.5909, p-value = 0.009573
               alternative hypothesis: true tau is not equal to 0
               sample estimates:
               non-parametric test

    Comments & Feedback