Beautiful and Powerful Correlation Tables in R

Posted payton数据之旅

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Beautiful and Powerful Correlation Tables in R相关的知识,希望对你有一定的参考价值。

Another correlation function?!

Yes, the correlation function from the psycho package.

devtools::install_github("neuropsychology/psycho.R")  # Install the newest version

library(psycho)
library(tidyverse)

cor <- psycho::affective %>% 
  correlation()

This function automatically select numeric variables and run a correlation analysis. It returns apsychobject.

A table

We can then extract a formatted table that can be saved and pasted into reports and manuscripts by using the summary function.

summary(cor)
# write.csv(summary(cor), "myformattedcortable.csv")
 AgeLife_SatisfactionConcealingAdjusting
Age        
Life_Satisfaction 0.03      
Concealing -0.05 -0.06    
Adjusting 0.03 0.36*** 0.22***  
Tolerating 0.03 0.15*** 0.07 0.29***

A Plot

It integrates a plot done with ggcorplot.

plot(cor)

技术分享图片

A print

It also includes a pairwise correlation printing method.

print(cor)
Pearson Full correlation (p value correction: holm):

   - Age / Life_Satisfaction:   Results of the Pearson correlation showed a non significant and weak negative association between Age and Life_Satisfaction (r(1249) = 0.030, p > .1).
   - Age / Concealing:   Results of the Pearson correlation showed a non significant and weak positive association between Age and Concealing (r(1249) = -0.050, p > .1).
   - Life_Satisfaction / Concealing:   Results of the Pearson correlation showed a non significant and weak positive association between Life_Satisfaction and Concealing (r(1249) = -0.063, p > .1).
   - Age / Adjusting:   Results of the Pearson correlation showed a non significant and weak negative association between Age and Adjusting (r(1249) = 0.027, p > .1).
   - Life_Satisfaction / Adjusting:   Results of the Pearson correlation showed a significant and moderate negative association between Life_Satisfaction and Adjusting (r(1249) = 0.36, p < .001***).
   - Concealing / Adjusting:   Results of the Pearson correlation showed a significant and weak negative association between Concealing and Adjusting (r(1249) = 0.22, p < .001***).
   - Age / Tolerating:   Results of the Pearson correlation showed a non significant and weak negative association between Age and Tolerating (r(1249) = 0.031, p > .1).
   - Life_Satisfaction / Tolerating:   Results of the Pearson correlation showed a significant and weak negative association between Life_Satisfaction and Tolerating (r(1249) = 0.15, p < .001***).
   - Concealing / Tolerating:   Results of the Pearson correlation showed a non significant and weak negative association between Concealing and Tolerating (r(1249) = 0.074, p = 0.05°).
   - Adjusting / Tolerating:   Results of the Pearson correlation showed a significant and weak negative association between Adjusting and Tolerating (r(1249) = 0.29, p < .001***).

Options

You can also cutomize the type (pearson, spearman or kendall), the p value correction method(holm (default), bonferroni, fdr, none…) and run partial, semi-partial or glasso correlations.

psycho::affective %>% 
  correlation(method = "pearson", adjust="bonferroni", type="partial") %>% 
  summary()
 AgeLife_SatisfactionConcealingAdjusting
Age        
Life_Satisfaction 0.01      
Concealing -0.06 -0.16***    
Adjusting 0.02 0.36*** 0.25***  
Tolerating 0.02 0.06 0.02 0.24***

Fun with p-hacking

In order to prevent people for running many uncorrected correlation tests (promoting p-hacking and result-fishing), we included the i_am_cheating parameter. If FALSE (default), the function will help you finding interesting results!

df_with_11_vars <- data.frame(replicate(11, rnorm(1000)))
cor <- correlation(df_with_11_vars, adjust="none")
## Warning in correlation(df_with_11_vars, adjust = "none"): We‘ve detected that you are running a lot (> 10) of correlation tests without adjusting the p values. To help you in your p-fishing, we‘ve added some interesting variables: You never know, you might find something significant!
## To deactivate this, change the ‘i_am_cheating‘ argument to TRUE.
summary(cor)
 X1X2X3X4X5X6X7X8X9X10X11
X1                      
X2 -0.04                    
X3 -0.04 -0.02                  
X4 0.02 0.05 -0.02                
X5 -0.01 -0.02 0.05 -0.03              
X6 -0.03 0.03 0.08* 0.02 0.02            
X7 0.03 -0.01 -0.02 -0.04 -0.03 -0.04          
X8 0.01 -0.07* 0.04 0.02 -0.01 -0.01 0.00        
X9 -0.02 0.03 -0.03 -0.02 0.00 -0.04 0.03 -0.02      
X10 -0.03 0.00 0.00 0.01 0.01 -0.01 0.01 -0.02 0.02    
X11 0.01 0.01 -0.03 -0.05 0.00 0.05 0.01 0.00 -0.01 0.07*  
Local_Air_Density 0.26*** -0.02 -0.44*** -0.15*** -0.25*** -0.50*** 0.57*** -0.11*** 0.47*** 0.06 0.01
Reincarnation_Cycle -0.03 -0.02 0.02 0.04 0.01 0.00 0.05 -0.04 -0.05 -0.01 0.03
Communism_Level 0.58*** -0.44*** 0.04 0.06 -0.10** -0.18*** 0.10** 0.46*** -0.50*** -0.21*** -0.14***
Alien_Mothership_Distance 0.00 -0.03 0.01 0.00 -0.01 -0.03 -0.04 0.01 0.01 -0.02 0.00
Schopenhauers_Optimism 0.11*** 0.31*** -0.25*** 0.64*** -0.29*** -0.15*** -0.35*** -0.09** 0.08* -0.22*** -0.47***
Hulks_Power 0.03 0.00 0.02 0.03 -0.02 -0.01 -0.05 -0.01 0.00 0.01 0.03

As we can see, Schopenhauer’s Optimism is strongly related to many variables!!!

Credits

This package was useful? You can cite psycho as follows:

  • Makowski, (2018). The psycho Package: an Efficient and Publishing-Oriented Workflow for Psychological Science. Journal of Open Source Software, 3(22), 470.https://doi.org/10.21105/joss.00470

 

转自:https://neuropsychology.github.io/psycho.R//2018/05/20/correlation.html

以上是关于Beautiful and Powerful Correlation Tables in R的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 970. Powerful Integers

leetcode970. Powerful Integers

Young and Beautiful 新红楼剪辑(by up)

CF354C Vasya and Beautiful Arrays

[CF1521E]Nastia and a Beautiful Matrix

Rodrigues formula is beautiful, but uneven to sine and cosine. (zz Berkeley's Page)