AntoineSoetewey / statsandr

A blog on statistics and R aiming at helping academics and professionals working with data to grasp important concepts in statistics and to apply them in R. See www.statsandr.com
http://statsandr.com/
35 stars 15 forks source link

idea #8

Closed krzysiektr closed 4 years ago

krzysiektr commented 4 years ago
> RVAideMemoire::cor.multcomp(mtcars$mpg, mtcars$disp, factor(mtcars$cyl))

    Comparison of 3 Pearson's linear correlation coefficients

data:  mtcars$mpg and mtcars$disp by factor(mtcars$cyl) 
X-squared = 4.0477, df = 2, p-value = 0.1321
alternative hypothesis: true difference in coefficients is not equal to 0 
sample estimates:
coeff in group 4 coeff in group 6 coeff in group 8 
      -0.8052361        0.1030827       -0.5197670 

        Common correlation coefficient, 95% confidence interval
          and equality to given value 0 

     inf       r     sup theoretical     U Pr(>|U|)   
 -0.7831 -0.5681 -0.2318           0 3.092 0.001988 **
> correlation::correlation(dat, include_factors = TRUE, method = "auto")
Parameter1 | Parameter2 |     r |         95% CI |     t | df |      p |  Method | n_Obs
----------------------------------------------------------------------------------------
mpg        |        cyl | -0.85 | [-0.93, -0.72] | -8.92 | 30 | < .001 | Pearson |    32
mpg        |       disp | -0.85 | [-0.92, -0.71] | -8.75 | 30 | < .001 | Pearson |    32
mpg        |         hp | -0.78 | [-0.89, -0.59] | -6.74 | 30 | < .001 | Pearson |    32
mpg        |       drat |  0.68 | [ 0.44,  0.83] |  5.10 | 30 | < .001 | Pearson |    32
mpg        |         wt | -0.87 | [-0.93, -0.74] | -9.56 | 30 | < .001 | Pearson |    32
mpg        |       qsec |  0.42 | [ 0.08,  0.67] |  2.53 | 30 | 0.137  | Pearson |    32
mpg        |       gear |  0.48 | [ 0.16,  0.71] |  3.00 | 30 | 0.065  | Pearson |    32
mpg        |       carb | -0.55 | [-0.75, -0.25] | -3.62 | 30 | 0.016  | Pearson |    32
cyl        |       disp |  0.90 | [ 0.81,  0.95] | 11.45 | 30 | < .001 | Pearson |    32
cyl        |         hp |  0.83 | [ 0.68,  0.92] |  8.23 | 30 | < .001 | Pearson |    32
cyl        |       drat | -0.70 | [-0.84, -0.46] | -5.37 | 30 | < .001 | Pearson |    32
cyl        |         wt |  0.78 | [ 0.60,  0.89] |  6.88 | 30 | < .001 | Pearson |    32
cyl        |       qsec | -0.59 | [-0.78, -0.31] | -4.02 | 30 | 0.007  | Pearson |    32
cyl        |       gear | -0.49 | [-0.72, -0.17] | -3.10 | 30 | 0.054  | Pearson |    32
cyl        |       carb |  0.53 | [ 0.22,  0.74] |  3.40 | 30 | 0.027  | Pearson |    32
disp       |         hp |  0.79 | [ 0.61,  0.89] |  7.08 | 30 | < .001 | Pearson |    32
disp       |       drat | -0.71 | [-0.85, -0.48] | -5.53 | 30 | < .001 | Pearson |    32
disp       |         wt |  0.89 | [ 0.78,  0.94] | 10.58 | 30 | < .001 | Pearson |    32
disp       |       qsec | -0.43 | [-0.68, -0.10] | -2.64 | 30 | 0.131  | Pearson |    32
disp       |       gear | -0.56 | [-0.76, -0.26] | -3.66 | 30 | 0.015  | Pearson |    32
disp       |       carb |  0.39 | [ 0.05,  0.65] |  2.35 | 30 | 0.177  | Pearson |    32
hp         |       drat | -0.45 | [-0.69, -0.12] | -2.75 | 30 | 0.110  | Pearson |    32
hp         |         wt |  0.66 | [ 0.40,  0.82] |  4.80 | 30 | < .001 | Pearson |    32
hp         |       qsec | -0.71 | [-0.85, -0.48] | -5.49 | 30 | < .001 | Pearson |    32
hp         |       gear | -0.13 | [-0.45,  0.23] | -0.69 | 30 | 1.000  | Pearson |    32
hp         |       carb |  0.75 | [ 0.54,  0.87] |  6.21 | 30 | < .001 | Pearson |    32
drat       |         wt | -0.71 | [-0.85, -0.48] | -5.56 | 30 | < .001 | Pearson |    32
drat       |       qsec |  0.09 | [-0.27,  0.43] |  0.50 | 30 | 1.000  | Pearson |    32
drat       |       gear |  0.70 | [ 0.46,  0.84] |  5.36 | 30 | < .001 | Pearson |    32
drat       |       carb | -0.09 | [-0.43,  0.27] | -0.50 | 30 | 1.000  | Pearson |    32
wt         |       qsec | -0.17 | [-0.49,  0.19] | -0.97 | 30 | 1.000  | Pearson |    32
wt         |       gear | -0.58 | [-0.77, -0.29] | -3.93 | 30 | 0.008  | Pearson |    32
wt         |       carb |  0.43 | [ 0.09,  0.68] |  2.59 | 30 | 0.132  | Pearson |    32
qsec       |       gear | -0.21 | [-0.52,  0.15] | -1.19 | 30 | 1.000  | Pearson |    32
qsec       |       carb | -0.66 | [-0.82, -0.40] | -4.76 | 30 | < .001 | Pearson |    32
gear       |       carb |  0.27 | [-0.08,  0.57] |  1.56 | 30 | 0.774  | Pearson |    32
PerformanceAnalytics::chart.Correlation(as.matrix(dat), histogram=TRUE, pch="+")

corrPlot

AntoineSoetewey commented 4 years ago

Thanks @krzysiektr for pointing it out to me!

I take note of suggestions 1 and 3 for personal use, and I have included suggestion 2 in this article.

ayokem commented 10 months ago

how does it handle missing values?

AntoineSoetewey commented 10 months ago

Dear @ayokem,

Not sure which function you are referring to, but if you are referring to the cor() function:

By default, if there are some missing values, the output will be NA. See for instance:

x <- c(0.6, 0, NA, 0.5)
y <- c(0.4, 0.7, 0.3, 0.2)
cor(x, y)

Unless you use the use = "complete.obs" argument:

x <- c(0.6, 0, NA, 0.5)
y <- c(0.4, 0.7, 0.3, 0.2)
cor(x, y, use = "complete.obs")

which will give the Pearson correlation coefficient with only the complete pairs.

Hope this helps.

If you have any other questions, I suggest you ask them in the comments of the related post, so everyone can benefit from the discussion.

Regards, Antoine