Beautiful and Powerful Correlation Tables in R
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Another correlation function?!
Yes, the correlation
function from the psycho
package.
<span class="n">devtools</span><span class="o">::</span><span class="n">install_github</span><span class="p">(</span><span class="s2">"neuropsychology/psycho.R"</span><span class="p">)</span><span class="w"> </span><span class="c1"># Install the newest version</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">psycho</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">tidyverse</span><span class="p">)</span><span class="w">
</span><span class="n">cor</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">psycho</span><span class="o">::</span><span class="n">affective</span><span class="w"> </span><span class="o">%>%</span><span class="w">
</span><span class="n">correlation</span><span class="p">()</span><span class="w">
</span>
This function automatically select numeric variables and run a correlation analysis. It returns a psychobject
.
A table
We can then extract a formatted table that can be saved and pasted into reports and manuscripts by using the summary
function.
<span class="n">summary</span><span class="p">(</span><span class="n">cor</span><span class="p">)</span><span class="w">
</span><span class="c1"># write.csv(summary(cor), "myformattedcortable.csv")</span><span class="w">
</span>
Age | Life_Satisfaction | Concealing | Adjusting | |
---|---|---|---|---|
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
.
<span class="n">plot</span><span class="p">(</span><span class="n">cor</span><span class="p">)</span><span class="w">
</span>
A print
It also includes a pairwise correlation printing method.
<span class="n">print</span><span class="p">(</span><span class="n">cor</span><span class="p">)</span><span class="w">
</span>
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.
<span class="n">psycho</span><span class="o">::</span><span class="n">affective</span><span class="w"> </span><span class="o">%>%</span><span class="w">
</span><span class="n">correlation</span><span class="p">(</span><span class="n">method</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"pearson"</span><span class="p">,</span><span class="w"> </span><span class="n">adjust</span><span class="o">=</span><span class="s2">"bonferroni"</span><span class="p">,</span><span class="w"> </span><span class="n">type</span><span class="o">=</span><span class="s2">"partial"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
</span><span class="n">summary</span><span class="p">()</span><span class="w">
</span>
Age | Life_Satisfaction | Concealing | Adjusting | |
---|---|---|---|---|
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!
<span class="n">df_with_11_vars</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">data.frame</span><span class="p">(</span><span class="n">replicate</span><span class="p">(</span><span class="m">11</span><span class="p">,</span><span class="w"> </span><span class="n">rnorm</span><span class="p">(</span><span class="m">1000</span><span class="p">)))</span><span class="w">
</span><span class="n">cor</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">correlation</span><span class="p">(</span><span class="n">df_with_11_vars</span><span class="p">,</span><span class="w"> </span><span class="n">adjust</span><span class="o">=</span><span class="s2">"none"</span><span class="p">)</span><span class="w">
</span>
## 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.
<span class="n">summary</span><span class="p">(</span><span class="n">cor</span><span class="p">)</span><span class="w">
</span>
X1 | X2 | X3 | X4 | X5 | X6 | X7 | X8 | X9 | X10 | X11 | |
---|---|---|---|---|---|---|---|---|---|---|---|
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
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.