<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/beautiful-and-powerful-correlation-tables-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://neuropsychology.github.io/psycho.R//2018/05/18/correlation.html"> Dominique Makowski</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<ul>
<li><a rel="nofollow" href="https://neuropsychology.github.io/psycho.R//2018/05/18/correlation.html#another-correlation-function" target="_blank">Another <em>correlation</em> function?!</a></li>
<li><a rel="nofollow" href="https://neuropsychology.github.io/psycho.R//2018/05/18/correlation.html#a-table" target="_blank">A table</a></li>
<li><a rel="nofollow" href="https://neuropsychology.github.io/psycho.R//2018/05/18/correlation.html#a-plot" target="_blank">A Plot</a></li>
<li><a rel="nofollow" href="https://neuropsychology.github.io/psycho.R//2018/05/18/correlation.html#a-print" target="_blank">A print</a></li>
<li><a rel="nofollow" href="https://neuropsychology.github.io/psycho.R//2018/05/18/correlation.html#options" target="_blank">Options</a></li>
<li><a rel="nofollow" href="https://neuropsychology.github.io/psycho.R//2018/05/18/correlation.html#fun-with-p-hacking" target="_blank">Fun with <em>p</em>-hacking</a></li>
<li><a rel="nofollow" href="https://neuropsychology.github.io/psycho.R//2018/05/18/correlation.html#credits" target="_blank">Credits</a></li>
</ul>
<h1>Another <em>correlation</em> function?!</h1>
<p>Yes, the <code>correlation</code> function from the <a rel="nofollow" href="https://github.com/neuropsychology/psycho.R" target="_blank"><code>psycho</code></a> package.</p>
<div>
<div>
<pre><code><span>devtools</span><span>::</span><span>install_github</span><span>(</span><span>&quot;neuropsychology/psycho.R&quot;</span><span>)</span><span> </span><span># Install the newest version</span><span>
</span><span>library</span><span>(</span><span>psycho</span><span>)</span><span>
</span><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>cor</span><span> </span><span>&lt;-</span><span> </span><span>psycho</span><span>::</span><span>affective</span><span> </span><span>%>%</span><span> </span><span>correlation</span><span>()</span><span>
</span></code></pre>
</div>
</div>
<p>This function automatically select numeric variables and run a correlation analysis. It returns a <code>psychobject</code>.</p>
<h1>A table</h1>
<p>We can then extract a <strong>formatted table</strong> that can be saved and pasted into reports and manuscripts by using the <code>summary</code> function.</p>
<div>
<div>
<pre><code><span>summary</span><span>(</span><span>cor</span><span>)</span><span>
</span><span># write.csv(summary(cor), &quot;myformattedcortable.csv&quot;)</span><span>
</span></code></pre>
</div>
</div>
<table>
<thead>
<tr>
<th> </th>
<th>Age</th>
<th>Life_Satisfaction</th>
<th>Concealing</th>
<th>Adjusting</th>
</tr>
</thead>
<tbody>
<tr>
<td>Age</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Life_Satisfaction</td>
<td>0.03</td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Concealing</td>
<td>-0.05</td>
<td>-0.06</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Adjusting</td>
<td>0.03</td>
<td>0.36***</td>
<td>0.22***</td>
<td> </td>
</tr>
<tr>
<td>Tolerating</td>
<td>0.03</td>
<td>0.15***</td>
<td>0.07</td>
<td>0.29***</td>
</tr>
</tbody>
</table>
<h1>A Plot</h1>
<p>It integrates a <strong>plot</strong> done with <a rel="nofollow" href="https://github.com/kassambara/ggcorrplot" target="_blank"><code>ggcorplot</code></a>.</p>
<div>
<div>
<pre><code><span>plot</span><span>(</span><span>cor</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p><img src="https://i2.wp.com/raw.githubusercontent.com/neuropsychology/psycho.R/master/docs/_posts/2018-05-18-correlation_files/figure-markdown_github/unnamed-chunk-5-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h1>A print</h1>
<p>It also includes a <strong>pairwise correlation printing</strong> method.</p>
<div>
<div>
<pre><code><span>print</span><span>(</span><span>cor</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>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 &lt; .001***).
   - Concealing / Adjusting:   Results of the Pearson correlation showed a significant and weak negative association between Concealing and Adjusting (r(1249) = 0.22, p &lt; .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 &lt; .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 &lt; .001***).
</code></pre>
</div>
</div>
<h1>Options</h1>
<p>You can also cutomize the <strong>type</strong> (<em>pearson, spearman or kendall</em>), the <strong>p value correction method</strong> (<em>holm (default), bonferroni, fdr, none…</em>) and run <strong>partial, semi-partial</strong> or <strong>glasso</strong> correlations.</p>
<div>
<div>
<pre><code><span>psycho</span><span>::</span><span>affective</span><span> </span><span>%>%</span><span> </span><span>correlation</span><span>(</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;pearson&quot;</span><span>,</span><span> </span><span>adjust</span><span>=</span><span>&quot;bonferroni&quot;</span><span>,</span><span> </span><span>type</span><span>=</span><span>&quot;partial&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span>summary</span><span>()</span><span>
</span></code></pre>
</div>
</div>
<table>
<thead>
<tr>
<th> </th>
<th>Age</th>
<th>Life_Satisfaction</th>
<th>Concealing</th>
<th>Adjusting</th>
</tr>
</thead>
<tbody>
<tr>
<td>Age</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Life_Satisfaction</td>
<td>0.01</td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Concealing</td>
<td>-0.06</td>
<td>-0.16***</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Adjusting</td>
<td>0.02</td>
<td>0.36***</td>
<td>0.25***</td>
<td> </td>
</tr>
<tr>
<td>Tolerating</td>
<td>0.02</td>
<td>0.06</td>
<td>0.02</td>
<td>0.24***</td>
</tr>
</tbody>
</table>
<h1>Fun with <em>p</em>-hacking</h1>
<p>In order to <strong>prevent people for running many uncorrected correlation tests</strong> (promoting <em>p</em>-hacking and result-fishing), we included the <code>i_am_cheating</code> parameter. If FALSE (default), the function will help you finding interesting results!</p>
<div>
<div>
<pre><code><span>df_with_11_vars</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>replicate</span><span>(</span><span>11</span><span>,</span><span> </span><span>rnorm</span><span>(</span><span>1000</span><span>)))</span><span>
</span><span>cor</span><span> </span><span>&lt;-</span><span> </span><span>correlation</span><span>(</span><span>df_with_11_vars</span><span>,</span><span> </span><span>adjust</span><span>=</span><span>&quot;none&quot;</span><span>)</span><span> </span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## Warning in correlation(df_with_11_vars, adjust = &quot;none&quot;): 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.
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>summary</span><span>(</span><span>cor</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<table>
<thead>
<tr>
<th> </th>
<th>X1</th>
<th>X2</th>
<th>X3</th>
<th>X4</th>
<th>X5</th>
<th>X6</th>
<th>X7</th>
<th>X8</th>
<th>X9</th>
<th>X10</th>
<th>X11</th>
</tr>
</thead>
<tbody>
<tr>
<td>X1</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>X2</td>
<td>-0.04</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>X3</td>
<td>-0.04</td>
<td>-0.02</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>X4</td>
<td>0.02</td>
<td>0.05</td>
<td>-0.02</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>X5</td>
<td>-0.01</td>
<td>-0.02</td>
<td>0.05</td>
<td>-0.03</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>X6</td>
<td>-0.03</td>
<td>0.03</td>
<td>0.08*</td>
<td>0.02</td>
<td>0.02</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>X7</td>
<td>0.03</td>
<td>-0.01</td>
<td>-0.02</td>
<td>-0.04</td>
<td>-0.03</td>
<td>-0.04</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>X8</td>
<td>0.01</td>
<td>-0.07*</td>
<td>0.04</td>
<td>0.02</td>
<td>-0.01</td>
<td>-0.01</td>
<td>0.00</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>X9</td>
<td>-0.02</td>
<td>0.03</td>
<td>-0.03</td>
<td>-0.02</td>
<td>0.00</td>
<td>-0.04</td>
<td>0.03</td>
<td>-0.02</td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>X10</td>
<td>-0.03</td>
<td>0.00</td>
<td>0.00</td>
<td>0.01</td>
<td>0.01</td>
<td>-0.01</td>
<td>0.01</td>
<td>-0.02</td>
<td>0.02</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>X11</td>
<td>0.01</td>
<td>0.01</td>
<td>-0.03</td>
<td>-0.05</td>
<td>0.00</td>
<td>0.05</td>
<td>0.01</td>
<td>0.00</td>
<td>-0.01</td>
<td>0.07*</td>
<td> </td>
</tr>
<tr>
<td>Local_Air_Density</td>
<td>0.26***</td>
<td>-0.02</td>
<td>-0.44***</td>
<td>-0.15***</td>
<td>-0.25***</td>
<td>-0.50***</td>
<td>0.57***</td>
<td>-0.11***</td>
<td>0.47***</td>
<td>0.06</td>
<td>0.01</td>
</tr>
<tr>
<td>Reincarnation_Cycle</td>
<td>-0.03</td>
<td>-0.02</td>
<td>0.02</td>
<td>0.04</td>
<td>0.01</td>
<td>0.00</td>
<td>0.05</td>
<td>-0.04</td>
<td>-0.05</td>
<td>-0.01</td>
<td>0.03</td>
</tr>
<tr>
<td>Communism_Level</td>
<td>0.58***</td>
<td>-0.44***</td>
<td>0.04</td>
<td>0.06</td>
<td>-0.10**</td>
<td>-0.18***</td>
<td>0.10**</td>
<td>0.46***</td>
<td>-0.50***</td>
<td>-0.21***</td>
<td>-0.14***</td>
</tr>
<tr>
<td>Alien_Mothership_Distance</td>
<td>0.00</td>
<td>-0.03</td>
<td>0.01</td>
<td>0.00</td>
<td>-0.01</td>
<td>-0.03</td>
<td>-0.04</td>
<td>0.01</td>
<td>0.01</td>
<td>-0.02</td>
<td>0.00</td>
</tr>
<tr>
<td>Schopenhauers_Optimism</td>
<td>0.11***</td>
<td>0.31***</td>
<td>-0.25***</td>
<td>0.64***</td>
<td>-0.29***</td>
<td>-0.15***</td>
<td>-0.35***</td>
<td>-0.09**</td>
<td>0.08*</td>
<td>-0.22***</td>
<td>-0.47***</td>
</tr>
<tr>
<td>Hulks_Power</td>
<td>0.03</td>
<td>0.00</td>
<td>0.02</td>
<td>0.03</td>
<td>-0.02</td>
<td>-0.01</td>
<td>-0.05</td>
<td>-0.01</td>
<td>0.00</td>
<td>0.01</td>
<td>0.03</td>
</tr>
</tbody>
</table>
<p><strong>As we can see, Schopenhauer’s Optimism is strongly related to many variables!!!</strong></p>
<h1>Credits</h1>
<p>This package was useful? You can cite <a rel="nofollow" href="https://github.com/neuropsychology/psycho.R" target="_blank"><code>psycho</code></a> as follows:</p>
<ul>
<li>Makowski, (2018). <em>The psycho Package: an Efficient and Publishing-Oriented Workflow for Psychological Science</em>. Journal of Open Source Software, 3(22), 470. <a rel="nofollow" href="https://doi.org/10.21105/joss.00470" target="_blank">https://doi.org/10.21105/joss.00470</a></li>
</ul>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://neuropsychology.github.io/psycho.R//2018/05/18/correlation.html"> Dominique Makowski</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/d7DP_9idprg" height="1"></a></div>