<?xml version="1.0" encoding="UTF-8" ?>
<!-- RSS generated by PHPBoost on Thu, 16 Apr 2026 08:54:36 +0200 -->

<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Easy Guides]]></title>
		<atom:link href="https://www.sthda.com/english/syndication/rss/wiki/18" rel="self" type="application/rss+xml"/>
		<link>https://www.sthda.com</link>
		<description><![CDATA[Last articles of the category: Correlation Analyses in R]]></description>
		<copyright>(C) 2005-2026 PHPBoost</copyright>
		<language>en</language>
		<generator>PHPBoost</generator>
		
		
		<item>
			<title><![CDATA[Visualize correlation matrix using symnum function]]></title>
			<link>https://www.sthda.com/english/wiki/visualize-correlation-matrix-using-symnum-function</link>
			<guid>https://www.sthda.com/english/wiki/visualize-correlation-matrix-using-symnum-function</guid>
			<description><![CDATA[[html]<br />
          <br />
  <!--====================== start from here when you copy to sthda================--><br />
  <div id="rdoc"><br />
<div id="TOC"><br />
<ul><br />
<li><a href="#graph-of-correlation-matrix-using-symnum-function">Graph of correlation matrix using symnum function</a></li><br />
<li><a href="#conclusions">Conclusions</a></li><br />
<li><a href="#infos">Infos</a></li><br />
</ul><br />
</div><br />
<br />
<br />
<p>This article describes how to make a <strong>graph of correlation matrix</strong> in <strong>R</strong>. The R <strong>symnum()</strong> function is used. It takes the <strong>correlation table</strong> as an argument. The result is a table in which <strong>correlation coefficients</strong> are replaced by symbols according to the <strong>degree of correlation</strong>.</p><br />
<div class="block"><br />
<i class="fa - fa-cogs fa-4x valign_middle"></i> Note that online software is also available <a href="https://www.sthda.com/english/english/rsthda/correlation-matrix.php">here</a> to compute <strong>correlation matrix</strong> and to plot a <strong>correlogram</strong> without any installation.<br />
</div><br />
<div id="graph-of-correlation-matrix-using-symnum-function" class="section level1"><br />
<h1>Graph of correlation matrix using symnum function</h1><br />
<p>The R function <strong>symnum</strong> can be used to easily highlight the highly correlated variables. It replaces correlation coefficients by symbols according to the value.</p><br />
<p>The simplified format of the function is :</p><br />
<pre class="r"><code>symnum(x, cutpoints = c(0.3, 0.6, 0.8, 0.9, 0.95),<br />
       symbols = c(" ", ".", ",", "+", "*", "B"))</code>[/pre]<br />
<p><span class="warning"> - <strong>x</strong> is the correlation matrix to visualize<br />
- <strong>cutpoints</strong> : <strong>correlation coefficient</strong> cutpoints. The <strong>correlation coefficients</strong> between 0 and 0.3 are replaced by a space (" &amp;#8220<img src="https://www.sthda.com/english/english/images/smileys/wink.png" alt=";)" class="smiley" />; <strong>correlation coefficients</strong> between 0.3 and 0.6 are replace by&amp;#8221;.&amp;#8220;; etc &amp;#8230;<br />
- <strong>symbols</strong> : the symbols to use. </span></p><br />
<p>The following R code performs a <strong>correlation analysis</strong> and displays a <strong>graph of the correlation matrix</strong> :</p><br />
<pre class="r"><code>## Correlation matrix<br />
corMat<-cor(mtcars)<br />
head(round(corMat,2))</code>[/pre]<br />
<pre><code>       mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb<br />
mpg   1.00 -0.85 -0.85 -0.78  0.68 -0.87  0.42  0.66  0.60  0.48 -0.55<br />
cyl  -0.85  1.00  0.90  0.83 -0.70  0.78 -0.59 -0.81 -0.52 -0.49  0.53<br />
disp -0.85  0.90  1.00  0.79 -0.71  0.89 -0.43 -0.71 -0.59 -0.56  0.39<br />
hp   -0.78  0.83  0.79  1.00 -0.45  0.66 -0.71 -0.72 -0.24 -0.13  0.75<br />
drat  0.68 -0.70 -0.71 -0.45  1.00 -0.71  0.09  0.44  0.71  0.70 -0.09<br />
wt   -0.87  0.78  0.89  0.66 -0.71  1.00 -0.17 -0.55 -0.69 -0.58  0.43</code></pre><br />
<pre class="r"><code>## Correlation graph for visualization<br />
## abbr.colnames=FALSE to avoid abbreviation of column names<br />
symnum(corMat, abbr.colnames=FALSE)</code>[/pre]<br />
<pre><code>     mpg cyl disp hp drat wt qsec vs am gear carb<br />
mpg  1                                         <br />
cyl  +   1                                     <br />
disp +   *   1                                 <br />
hp   ,   +   ,    1                            <br />
drat ,   ,   ,    .  1                         <br />
wt   +   ,   +    ,  ,    1                    <br />
qsec .   .   .    ,          1                 <br />
vs   ,   +   ,    ,  .    .  ,    1            <br />
am   .   .   .       ,    ,          1         <br />
gear .   .   .       ,    .          ,  1      <br />
carb .   .   .    ,       .  ,    .          1 <br />
attr(,"legend")<br />
[1] 0 ' ' 0.3 '.' 0.6 ',' 0.8 '+' 0.9 '*' 0.95 'B' 1</code></pre><br />
</div><br />
<div id="conclusions" class="section level1"><br />
<h1>Conclusions</h1><br />
<div class="block"><br />
One of the easy way to visualize <strong>correlation matrix</strong> in R is to use the <strong>symnum()</strong> R function.<br />
</div><br />
</div><br />
<div id="infos" class="section level1"><br />
<h1>Infos</h1><br />
<pre class="warning"><code>This analysis was performed using R (ver. 3.1.0).</code>[/pre]<br />
</div><br />
<script>jQuery(document).ready(function () {<br />
    jQuery('h1').addClass('wiki_paragraph1');<br />
    jQuery('h2').addClass('wiki_paragraph2');<br />
    jQuery('h3').addClass('wiki_paragraph3');<br />
    jQuery('h4').addClass('wiki_paragraph4');<br />
    });//add phpboost class to header</script><br />
<style>.content{padding:0px;}</style><br />
</div><!--end rdoc--><br />
<!--====================== stop here when you copy to sthda================--><br />
[/html]]]></description>
			<pubDate>Wed, 21 May 2025 11:49:27 +0200</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Elegant correlation table using xtable R package]]></title>
			<link>https://www.sthda.com/english/wiki/elegant-correlation-table-using-xtable-r-package</link>
			<guid>https://www.sthda.com/english/wiki/elegant-correlation-table-using-xtable-r-package</guid>
			<description><![CDATA[<!-- START HTML -->

  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">
<p><br/> <a href="correlation-matrix">Correlation matrix analysis</a> is an important method to find <strong>dependence</strong> between variables. Computing <strong>correlation matrix</strong> and drawing <strong>correlogram</strong> is explained <a href="correlation-matrix">here</a>. The aim of this article is to show you how to get the <strong>lower and the upper triangular part of a correlation matrix</strong>. We will also use the <strong>xtable R package</strong> to display a nice <strong>correlation table</strong> in html or latex formats.</p>
<p><br/><br/> <img src="https://www.sthda.com/english/sthda/RDoc/images/correlation-matrix-visualization-xtable.png" alt="Elegant correlation table using xtable R package" /></p>
<br/><br/>
<div class="block">
<p><i class="fa - fa-cogs fa-4x valign_middle"></i> Note that online software is also available <a href="../rsthda/correlation-matrix.php">here</a> to compute <strong>correlation matrix</strong> and to plot a <strong>correlogram</strong> without any installation.</p>
</div>
<p><strong>Contents:</strong></p>
<div id="TOC">
<ul>
<li><a href="#correlation-matrix-analysis">Correlation matrix analysis</a></li>
<li><a href="#lower-and-upper-triangular-part-of-a-correlation-matrix">Lower and upper triangular part of a correlation matrix</a></li>
<li><a href="#use-xtable-r-package-to-display-nice-correlation-table-in-html-format">Use xtable R package to display nice correlation table in html format</a></li>
<li><a href="#combine-matrix-of-correlation-coefficients-and-significance-levels">Combine matrix of correlation coefficients and significance levels</a></li>
<li><a href="#conclusions">Conclusions</a></li>
</ul>
</div>
<div id="correlation-matrix-analysis" class="section level2">
<h2>Correlation matrix analysis</h2>
<p>The following R code computes a <strong>correlation matrix</strong> using mtcars data. <a href="correlation-matrix">Click here</a> to read more.</p>
<pre class="r"><code>mcor<-round(cor(mtcars),2)
mcor</code></pre>
<pre><code>       mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
mpg   1.00 -0.85 -0.85 -0.78  0.68 -0.87  0.42  0.66  0.60  0.48 -0.55
cyl  -0.85  1.00  0.90  0.83 -0.70  0.78 -0.59 -0.81 -0.52 -0.49  0.53
disp -0.85  0.90  1.00  0.79 -0.71  0.89 -0.43 -0.71 -0.59 -0.56  0.39
hp   -0.78  0.83  0.79  1.00 -0.45  0.66 -0.71 -0.72 -0.24 -0.13  0.75
drat  0.68 -0.70 -0.71 -0.45  1.00 -0.71  0.09  0.44  0.71  0.70 -0.09
wt   -0.87  0.78  0.89  0.66 -0.71  1.00 -0.17 -0.55 -0.69 -0.58  0.43
qsec  0.42 -0.59 -0.43 -0.71  0.09 -0.17  1.00  0.74 -0.23 -0.21 -0.66
vs    0.66 -0.81 -0.71 -0.72  0.44 -0.55  0.74  1.00  0.17  0.21 -0.57
am    0.60 -0.52 -0.59 -0.24  0.71 -0.69 -0.23  0.17  1.00  0.79  0.06
gear  0.48 -0.49 -0.56 -0.13  0.70 -0.58 -0.21  0.21  0.79  1.00  0.27
carb -0.55  0.53  0.39  0.75 -0.09  0.43 -0.66 -0.57  0.06  0.27  1.00</code></pre>
<p>The result is a table of <strong>correlation coefficients</strong> between all possible pairs of variables.</p>
</div>
<div id="lower-and-upper-triangular-part-of-a-correlation-matrix" class="section level2">
<h2>Lower and upper triangular part of a correlation matrix</h2>
<p>To get the lower or the upper part of a <strong>correlation matrix</strong>, the R function <strong>lower.tri()</strong> or <strong>upper.tri()</strong> can be used. The formats of the functions are :</p>
<pre class="r"><code>lower.tri(x, diag = FALSE)
upper.tri(x, diag = FALSE)</code></pre>
<p><span class="notice"> - <strong>x</strong> : is the <strong>correlation matrix</strong> - <strong>diag</strong> : if TRUE the diagonal are not included in the result. </span></p>
<p>The two functions above, return a matrix of logicals which has the same size of a the <strong>correlation matrix</strong>. The entries is TRUE in the lower or upper triangle :</p>
<pre class="r"><code>upper.tri(mcor)</code></pre>
<pre><code>       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10] [,11]
 [1,] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 [2,] FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 [3,] FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 [4,] FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 [5,] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
 [6,] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
 [7,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE
 [8,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE
 [9,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE
[10,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
[11,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE</code></pre>
<pre class="r"><code># Hide upper triangle
upper<-mcor
upper[upper.tri(mcor)]<-""
upper<-as.data.frame(upper)
upper</code></pre>
<pre><code>       mpg   cyl  disp    hp  drat    wt  qsec    vs   am gear carb
mpg      1                                                         
cyl  -0.85     1                                                   
disp -0.85   0.9     1                                             
hp   -0.78  0.83  0.79     1                                       
drat  0.68  -0.7 -0.71 -0.45     1                                 
wt   -0.87  0.78  0.89  0.66 -0.71     1                           
qsec  0.42 -0.59 -0.43 -0.71  0.09 -0.17     1                     
vs    0.66 -0.81 -0.71 -0.72  0.44 -0.55  0.74     1               
am     0.6 -0.52 -0.59 -0.24  0.71 -0.69 -0.23  0.17    1          
gear  0.48 -0.49 -0.56 -0.13   0.7 -0.58 -0.21  0.21 0.79    1     
carb -0.55  0.53  0.39  0.75 -0.09  0.43 -0.66 -0.57 0.06 0.27    1</code></pre>
<pre class="r"><code>#Hide lower triangle
lower<-mcor
lower[lower.tri(mcor, diag=TRUE)]<-""
lower<-as.data.frame(lower)
lower</code></pre>
<pre><code>     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
mpg      -0.85 -0.85 -0.78  0.68 -0.87  0.42  0.66   0.6  0.48 -0.55
cyl              0.9  0.83  -0.7  0.78 -0.59 -0.81 -0.52 -0.49  0.53
disp                  0.79 -0.71  0.89 -0.43 -0.71 -0.59 -0.56  0.39
hp                         -0.45  0.66 -0.71 -0.72 -0.24 -0.13  0.75
drat                             -0.71  0.09  0.44  0.71   0.7 -0.09
wt                                     -0.17 -0.55 -0.69 -0.58  0.43
qsec                                          0.74 -0.23 -0.21 -0.66
vs                                                  0.17  0.21 -0.57
am                                                        0.79  0.06
gear                                                            0.27
carb                                                                </code></pre>
</div>
<div id="use-xtable-r-package-to-display-nice-correlation-table-in-html-format" class="section level2">
<h2>Use xtable R package to display nice correlation table in html format</h2>
<pre class="r"><code>library(xtable)
print(xtable(upper), type="html")</code></pre>
<!-- html table generated in R 3.3.2 by xtable 1.8-2 package -->
<!-- Mon Aug  7 11:46:55 2017 -->
<table border="1">
<tr>
<th>
</th>
<th>
mpg
</th>
<th>
cyl
</th>
<th>
disp
</th>
<th>
hp
</th>
<th>
drat
</th>
<th>
wt
</th>
<th>
qsec
</th>
<th>
vs
</th>
<th>
am
</th>
<th>
gear
</th>
<th>
carb
</th>
</tr>
<tr>
<td align="right">
mpg
</td>
<td>
1
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
cyl
</td>
<td>
-0.85
</td>
<td>
1
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
disp
</td>
<td>
-0.85
</td>
<td>
0.9
</td>
<td>
1
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
hp
</td>
<td>
-0.78
</td>
<td>
0.83
</td>
<td>
0.79
</td>
<td>
1
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
drat
</td>
<td>
0.68
</td>
<td>
-0.7
</td>
<td>
-0.71
</td>
<td>
-0.45
</td>
<td>
1
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
wt
</td>
<td>
-0.87
</td>
<td>
0.78
</td>
<td>
0.89
</td>
<td>
0.66
</td>
<td>
-0.71
</td>
<td>
1
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
qsec
</td>
<td>
0.42
</td>
<td>
-0.59
</td>
<td>
-0.43
</td>
<td>
-0.71
</td>
<td>
0.09
</td>
<td>
-0.17
</td>
<td>
1
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
vs
</td>
<td>
0.66
</td>
<td>
-0.81
</td>
<td>
-0.71
</td>
<td>
-0.72
</td>
<td>
0.44
</td>
<td>
-0.55
</td>
<td>
0.74
</td>
<td>
1
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
am
</td>
<td>
0.6
</td>
<td>
-0.52
</td>
<td>
-0.59
</td>
<td>
-0.24
</td>
<td>
0.71
</td>
<td>
-0.69
</td>
<td>
-0.23
</td>
<td>
0.17
</td>
<td>
1
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
gear
</td>
<td>
0.48
</td>
<td>
-0.49
</td>
<td>
-0.56
</td>
<td>
-0.13
</td>
<td>
0.7
</td>
<td>
-0.58
</td>
<td>
-0.21
</td>
<td>
0.21
</td>
<td>
0.79
</td>
<td>
1
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
carb
</td>
<td>
-0.55
</td>
<td>
0.53
</td>
<td>
0.39
</td>
<td>
0.75
</td>
<td>
-0.09
</td>
<td>
0.43
</td>
<td>
-0.66
</td>
<td>
-0.57
</td>
<td>
0.06
</td>
<td>
0.27
</td>
<td>
1
</td>
</tr>
</table>
</div>
<div id="combine-matrix-of-correlation-coefficients-and-significance-levels" class="section level2">
<h2>Combine matrix of correlation coefficients and significance levels</h2>
<p>Custom function <strong>corstars()</strong> is used to combine the <strong>correlation coefficients</strong> and the level of <strong>significance</strong>. The R code of the function is provided at the end of this article. It requires 2 packages :</p>
<ul>
<li>The <strong>Hmisc R package</strong> to compute the <a href="correlation-matrix"><strong>matrix of correlation coefficients</strong></a> and the corresponding <strong>p-values</strong>.
</li>
<li>The <strong>xtable R package</strong> for displaying in HTML or Latex format.</li>
</ul>
<div class="warning">
<p>
Before continuing with the following exercises, you should first copy and paste the source code the function <strong>corstars</strong>(), which you can find at the bottom of this article.
</p>
</div>
<pre class="r"><code>corstars(mtcars[,1:7], result="html")</code></pre>
<!-- html table generated in R 3.3.2 by xtable 1.8-2 package -->
<!-- Mon Aug  7 11:46:56 2017 -->
<table border="1">
<tr>
<th>
</th>
<th>
mpg
</th>
<th>
cyl
</th>
<th>
disp
</th>
<th>
hp
</th>
<th>
drat
</th>
<th>
wt
</th>
</tr>
<tr>
<td align="right">
mpg
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
cyl
</td>
<td>
-0.85****
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
disp
</td>
<td>
-0.85****
</td>
<td>
0.90****
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
hp
</td>
<td>
-0.78****
</td>
<td>
0.83****
</td>
<td>
0.79****
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
drat
</td>
<td>
0.68****
</td>
<td>
-0.70****
</td>
<td>
-0.71****
</td>
<td>
-0.45**
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
wt
</td>
<td>
-0.87****
</td>
<td>
0.78****
</td>
<td>
0.89****
</td>
<td>
0.66****
</td>
<td>
-0.71****
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
qsec
</td>
<td>
0.42*
</td>
<td>
-0.59***
</td>
<td>
-0.43*
</td>
<td>
-0.71****
</td>
<td>
0.09
</td>
<td>
-0.17
</td>
</tr>
</table>
<p>p < .0001 ‘****’; p < .001 ‘***’, p < .01 ‘**’, p < .05 ‘*’</p>
<p><strong>The code of corstars function</strong> (The code is adapted from the one posted on this <a href="https://stat.ethz.ch/pipermail/r-help/2008-March/156583.html">forum</a> and on this <a href="http://myowelt.blogspot.fr/2008/04/beautiful-correlation-tables-in-r.html">blog</a> ):</p>
<pre class="r"><code># x is a matrix containing the data
# method : correlation method. "pearson"" or "spearman"" is supported
# removeTriangle : remove upper or lower triangle
# results :  if "html" or "latex"
  # the results will be displayed in html or latex format
corstars <-function(x, method=c("pearson", "spearman"), removeTriangle=c("upper", "lower"),
                     result=c("none", "html", "latex")){
    #Compute correlation matrix
    require(Hmisc)
    x <- as.matrix(x)
    correlation_matrix<-rcorr(x, type=method[1])
    R <- correlation_matrix$r # Matrix of correlation coeficients
    p <- correlation_matrix$P # Matrix of p-value 
    
    ## Define notions for significance levels; spacing is important.
    mystars <- ifelse(p < .0001, "****", ifelse(p < .001, "*** ", ifelse(p < .01, "**  ", ifelse(p < .05, "*   ", "    "))))
    
    ## trunctuate the correlation matrix to two decimal
    R <- format(round(cbind(rep(-1.11, ncol(x)), R), 2))[,-1]
    
    ## build a new matrix that includes the correlations with their apropriate stars
    Rnew <- matrix(paste(R, mystars, sep=""), ncol=ncol(x))
    diag(Rnew) <- paste(diag(R), " ", sep="")
    rownames(Rnew) <- colnames(x)
    colnames(Rnew) <- paste(colnames(x), "", sep="")
    
    ## remove upper triangle of correlation matrix
    if(removeTriangle[1]=="upper"){
      Rnew <- as.matrix(Rnew)
      Rnew[upper.tri(Rnew, diag = TRUE)] <- ""
      Rnew <- as.data.frame(Rnew)
    }
    
    ## remove lower triangle of correlation matrix
    else if(removeTriangle[1]=="lower"){
      Rnew <- as.matrix(Rnew)
      Rnew[lower.tri(Rnew, diag = TRUE)] <- ""
      Rnew <- as.data.frame(Rnew)
    }
    
    ## remove last column and return the correlation matrix
    Rnew <- cbind(Rnew[1:length(Rnew)-1])
    if (result[1]=="none") return(Rnew)
    else{
      if(result[1]=="html") print(xtable(Rnew), type="html")
      else print(xtable(Rnew), type="latex") 
    }
} </code></pre>
</div>
<div id="conclusions" class="section level2">
<h2>Conclusions</h2>
<div class="block">
<ul>
<li>Use <strong>cor()</strong> function to compute <strong>correlation matrix</strong>.</li>
<li>Use <strong>lower.tri()</strong> and <strong>upper.tri()</strong> functions to get the lower or upper part of the <strong>correlation matrix</strong></li>
<li>Use <strong>xtable</strong> R function to display a nice correlation matrix in latex or html format.</li>
</ul>
</div>
</div>
<div id="infos" class="section level2">
<h2>Infos</h2>
<pre class="warning"><code>This analysis was performed using R (ver. 3.3.2).</code></pre>
</div>
<script>jQuery(document).ready(function () {
    jQuery('#rdoc h1').addClass('wiki_paragraph1');
    jQuery('#rdoc h2').addClass('wiki_paragraph2');
    jQuery('#rdoc h3').addClass('wiki_paragraph3');
    jQuery('#rdoc h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->

<!-- END HTML -->]]></description>
			<pubDate>Mon, 07 Aug 2017 12:07:05 +0200</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Correlation matrix : An R function to do all you need]]></title>
			<link>https://www.sthda.com/english/wiki/correlation-matrix-an-r-function-to-do-all-you-need</link>
			<guid>https://www.sthda.com/english/wiki/correlation-matrix-an-r-function-to-do-all-you-need</guid>
			<description><![CDATA[<!-- START HTML -->

  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">

<div id="TOC">
<ul>
<li><a href="#prerequisites">Prerequisites</a></li>
<li><a href="#example-of-data">Example of data</a></li>
<li><a href="#computing-the-correlation-matrix">Computing the correlation matrix</a><ul>
<li><a href="#upper-triangle-of-the-correlation-matrix">Upper triangle of the correlation matrix</a></li>
<li><a href="#full-correlation-matrix">Full correlation matrix</a></li>
<li><a href="#change-the-colors-of-the-correlogram">Change the colors of the correlogram</a></li>
<li><a href="#draw-a-heatmap">Draw a heatmap</a></li>
</ul></li>
<li><a href="#format-the-correlation-table">Format the correlation table</a></li>
<li><a href="#description-of-rquery.cormat-function">Description of rquery.cormat function</a></li>
<li><a href="#infos">Infos</a></li>
</ul>
</div>

<p><br/></p>
<p><a href="https://www.sthda.com/english/english/wiki/correlation-matrix-analisis-and-visualization"><strong>Correlation matrix</strong></a> analysis is very useful to study <strong>dependences</strong> or <strong>associations</strong> between variables. This article provides a custom <strong>R function</strong>, <strong>rquery.cormat</strong>(), for <strong>calculating</strong> and <strong>visualizing</strong> easily a<strong>correlation matrix</strong>.The result is a list containing, the <strong>correlation coefficient tables</strong> and the <strong>p-values</strong> of the <strong>correlations</strong>. In the result, the variables are reordered according to the level of the <strong>correlation</strong> which can help to quickly identify the most associated variables. A graph is also generated to visualize the <strong>correlation matrix</strong> using a <a href="https://www.sthda.com/english/english/wiki/visualize-correlation-matrix-using-correlogram"><strong>correlogram</strong></a> or a <a href="https://www.sthda.com/english/english/wiki/correlation-matrix-how-to-make-a-heatmap"><strong>heatmap</strong></a>.</p>
<div id="prerequisites" class="section level1">
<h1>Prerequisites</h1>
<p>The <strong>rquery.cormat</strong> function requires the installation of <strong>corrplot</strong> package. Before proceeding, install it using he following R code :</p>
<pre class="r"><code>install.packages("corrplot")</code></pre>
<p>To use the <strong>rquery.cormat</strong> function, you can source it as follow :</p>
<pre class="r"><code>source("https://www.sthda.com/upload/rquery_cormat.r")</code></pre>
<p><span class="warning">The R code of <strong>rquery.cormat</strong> function is provided at the end of this document.</span></p>
</div>
<div id="example-of-data" class="section level1">
<h1>Example of data</h1>
<p>The <code>mtcars</code> data is used in the following examples :</p>
<pre class="r"><code>mydata <- mtcars[, c(1,3,4,5,6,7)]
head(mydata)</code></pre>
<pre><code>                   mpg disp  hp drat    wt  qsec
Mazda RX4         21.0  160 110 3.90 2.620 16.46
Mazda RX4 Wag     21.0  160 110 3.90 2.875 17.02
Datsun 710        22.8  108  93 3.85 2.320 18.61
Hornet 4 Drive    21.4  258 110 3.08 3.215 19.44
Hornet Sportabout 18.7  360 175 3.15 3.440 17.02
Valiant           18.1  225 105 2.76 3.460 20.22</code></pre>
</div>
<div id="computing-the-correlation-matrix" class="section level1">
<h1>Computing the correlation matrix</h1>
<pre class="r"><code>rquery.cormat(mydata)</code></pre>
<pre><code>$r
        hp  disp    wt  qsec  mpg drat
hp       1                            
disp  0.79     1                      
wt    0.66  0.89     1                
qsec -0.71 -0.43 -0.17     1          
mpg  -0.78 -0.85 -0.87  0.42    1     
drat -0.45 -0.71 -0.71 0.091 0.68    1

$p
          hp    disp      wt  qsec     mpg drat
hp         0                                   
disp 7.1e-08       0                           
wt   4.1e-05 1.2e-11       0                   
qsec 5.8e-06   0.013    0.34     0             
mpg  1.8e-07 9.4e-10 1.3e-10 0.017       0     
drat    0.01 5.3e-06 4.8e-06  0.62 1.8e-05    0

$sym
     hp disp wt qsec mpg drat
hp   1                       
disp ,  1                    
wt   ,  +    1               
qsec ,  .       1            
mpg  ,  +    +  .    1       
drat .  ,    ,       ,   1   
attr(,"legend")
[1] 0 &amp;#39; &amp;#39; 0.3 &amp;#39;.&amp;#39; 0.6 &amp;#39;,&amp;#39; 0.8 &amp;#39;+&amp;#39; 0.9 &amp;#39;*&amp;#39; 0.95 &amp;#39;B&amp;#39; 1</code></pre>
<div class="figure">
<img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-matrix-rquery-cormat-graph-1.png" alt="Correlogram, Heatmap" width="288" style="margin-bottom:10px;" />
<p class="caption">
Correlogram, Heatmap
</p>
</div>
<br/>
<div class="block">
<p>The result of <strong>rquery.cormat</strong> function is a list containing the following components :</p>
<ul>
<li><strong>r</strong> : The table of correlation coefficients</li>
<li><strong>p</strong> : Table of p-values corresponding to the significance levels of the correlations</li>
<li><p><strong>sym</strong> : A representation of the correlation matrix in which coefficients are replaced by symbols according to the strength of the dependence. For more description, see this article: <a href="https://www.sthda.com/english/english/wiki/visualize-correlation-matrix-using-symnum-function">Visualize correlation matrix using symnum function</a></p></li>
<li>In the generated graph, negative correlations are in blue and positive ones in red color.</li>
</ul>
</div>
<p><br/></p>
<p><span class="warning">Note that in the result above, only the lower triangle of the correlation matrix is shown by default. You can use the following R script to get the upper triangle or the full correlation matrix.</span></p>
<div id="upper-triangle-of-the-correlation-matrix" class="section level2">
<h2>Upper triangle of the correlation matrix</h2>
<pre class="r"><code>rquery.cormat(mydata, type="upper")</code></pre>
<pre><code>$r
     hp disp   wt  qsec   mpg  drat
hp    1 0.79 0.66 -0.71 -0.78 -0.45
disp       1 0.89 -0.43 -0.85 -0.71
wt              1 -0.17 -0.87 -0.71
qsec                  1  0.42 0.091
mpg                         1  0.68
drat                              1

$p
     hp    disp      wt    qsec     mpg    drat
hp    0 7.1e-08 4.1e-05 5.8e-06 1.8e-07    0.01
disp          0 1.2e-11   0.013 9.4e-10 5.3e-06
wt                    0    0.34 1.3e-10 4.8e-06
qsec                          0   0.017    0.62
mpg                                   0 1.8e-05
drat                                          0

$sym
     hp disp wt qsec mpg drat
hp   1  ,    ,  ,    ,   .   
disp    1    +  .    +   ,   
wt           1       +   ,   
qsec            1    .       
mpg                  1   ,   
drat                     1   
attr(,"legend")
[1] 0 &amp;#39; &amp;#39; 0.3 &amp;#39;.&amp;#39; 0.6 &amp;#39;,&amp;#39; 0.8 &amp;#39;+&amp;#39; 0.9 &amp;#39;*&amp;#39; 0.95 &amp;#39;B&amp;#39; 1</code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-matrix-rquery-cormat-upper-triangle-1.png" width="288" style="margin-bottom:10px;" /></p>
</div>
<div id="full-correlation-matrix" class="section level2">
<h2>Full correlation matrix</h2>
<pre class="r"><code>rquery.cormat(mydata, type="full")</code></pre>
<pre><code>$r
        hp  disp    wt   qsec   mpg   drat
hp    1.00  0.79  0.66 -0.710 -0.78 -0.450
disp  0.79  1.00  0.89 -0.430 -0.85 -0.710
wt    0.66  0.89  1.00 -0.170 -0.87 -0.710
qsec -0.71 -0.43 -0.17  1.000  0.42  0.091
mpg  -0.78 -0.85 -0.87  0.420  1.00  0.680
drat -0.45 -0.71 -0.71  0.091  0.68  1.000

$p
          hp    disp      wt    qsec     mpg    drat
hp   0.0e+00 7.1e-08 4.1e-05 5.8e-06 1.8e-07 1.0e-02
disp 7.1e-08 0.0e+00 1.2e-11 1.3e-02 9.4e-10 5.3e-06
wt   4.1e-05 1.2e-11 0.0e+00 3.4e-01 1.3e-10 4.8e-06
qsec 5.8e-06 1.3e-02 3.4e-01 0.0e+00 1.7e-02 6.2e-01
mpg  1.8e-07 9.4e-10 1.3e-10 1.7e-02 0.0e+00 1.8e-05
drat 1.0e-02 5.3e-06 4.8e-06 6.2e-01 1.8e-05 0.0e+00

$sym
     hp disp wt qsec mpg drat
hp   1                       
disp ,  1                    
wt   ,  +    1               
qsec ,  .       1            
mpg  ,  +    +  .    1       
drat .  ,    ,       ,   1   
attr(,"legend")
[1] 0 &amp;#39; &amp;#39; 0.3 &amp;#39;.&amp;#39; 0.6 &amp;#39;,&amp;#39; 0.8 &amp;#39;+&amp;#39; 0.9 &amp;#39;*&amp;#39; 0.95 &amp;#39;B&amp;#39; 1</code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-matrix-rquery-cormat-full-1.png" width="288" style="margin-bottom:10px;" /></p>
</div>
<div id="change-the-colors-of-the-correlogram" class="section level2">
<h2>Change the colors of the correlogram</h2>
<pre class="r"><code>col<- colorRampPalette(c("blue", "white", "red"))(20)
cormat<-rquery.cormat(mydata, type="full", col=col)</code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-matrix-rquery-cormat-change-correlogram-color-1.png" width="288" style="margin-bottom:10px;" /></p>
</div>
<div id="draw-a-heatmap" class="section level2">
<h2>Draw a heatmap</h2>
<pre class="r"><code>cormat<-rquery.cormat(mydata, graphType="heatmap")</code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-matrix-rquery-cormat-heatmap-1.png" width="288" style="margin-bottom:10px;" /></p>
<br/>
<div class="warning">
<p>To calculate the <strong>correlation matrix</strong> without plotting the <strong>graph</strong>, you can use the following <strong>R</strong> script :</p>
<pre class="r"><code>rquery.cormat(mydata, graph=FALSE)</code></pre>
</div>
<p><br/></p>
</div>
</div>
<div id="format-the-correlation-table" class="section level1">
<h1>Format the correlation table</h1>
<p>The <strong>R code</strong> below can be used to format the <strong>correlation matrix</strong> into a table of four columns containing :</p>
<ul>
<li>The names of rows/columns</li>
<li>The correlation coefficients</li>
<li>The p-values</li>
</ul>
<p>For this end, use the argument : <strong>type=“flatten”</strong></p>
<pre class="r"><code>rquery.cormat(mydata, type="flatten", graph=FALSE)</code></pre>
<pre><code>$r
    row column    cor       p
1    hp   disp  0.790 7.1e-08
2    hp     wt  0.660 4.1e-05
3  disp     wt  0.890 1.2e-11
4    hp   qsec -0.710 5.8e-06
5  disp   qsec -0.430 1.3e-02
6    wt   qsec -0.170 3.4e-01
7    hp    mpg -0.780 1.8e-07
8  disp    mpg -0.850 9.4e-10
9    wt    mpg -0.870 1.3e-10
10 qsec    mpg  0.420 1.7e-02
11   hp   drat -0.450 1.0e-02
12 disp   drat -0.710 5.3e-06
13   wt   drat -0.710 4.8e-06
14 qsec   drat  0.091 6.2e-01
15  mpg   drat  0.680 1.8e-05

$p
NULL

$sym
NULL</code></pre>
</div>
<div id="description-of-rquery.cormat-function" class="section level1">
<h1>Description of rquery.cormat function</h1>
<p><strong>A simplified format of the function is</strong> :</p>
<pre class="r"><code>rquery.cormat(x, type=c(&amp;#39;lower&amp;#39;, &amp;#39;upper&amp;#39;, &amp;#39;full&amp;#39;, &amp;#39;flatten&amp;#39;),
              graph=TRUE, graphType=c("correlogram", "heatmap"),
              col=NULL, ...)</code></pre>
<p><strong>Description of the arguments</strong>:</p>
<ul>
<li><strong>x</strong> : <strong>matrix</strong> of data values</li>
<li><strong>type</strong> : Possible values are “lower” (default), “upper”, “full” or “flatten”. It displays the lower or upper triangular of the matrix, full or flatten matrix.</li>
<li><strong>graph</strong> : if TRUE, a correlogram or heatmap is generated to visualize the correlation matrix.</li>
<li><strong>graphType</strong> : Type of graphs. Possible values are “correlogram” or “heatmap”.</li>
<li><strong>col</strong>: colors to use for the correlogram or the heatmap.</li>
<li><strong>…</strong> : Further arguments to be passed to <strong>cor()</strong> or <strong>cor.test()</strong> function.</li>
</ul>
<p><strong>R code of the rquery.cormat function</strong>:</p>
<pre class="r"><code>#+++++++++++++++++++++++++
# Computing of correlation matrix
#+++++++++++++++++++++++++
# Required package : corrplot

# x : matrix
# type: possible values are "lower" (default), "upper", "full" or "flatten";
  #display lower or upper triangular of the matrix, full  or flatten matrix.
# graph : if TRUE, a correlogram or heatmap is plotted
# graphType : possible values are "correlogram" or "heatmap"
# col: colors to use for the correlogram
# ... : Further arguments to be passed to cor or cor.test function

# Result is a list including the following components :
  # r : correlation matrix, p :  p-values
  # sym : Symbolic number coding of the correlation matrix
rquery.cormat<-function(x,
                        type=c(&amp;#39;lower&amp;#39;, &amp;#39;upper&amp;#39;, &amp;#39;full&amp;#39;, &amp;#39;flatten&amp;#39;),
                        graph=TRUE,
                        graphType=c("correlogram", "heatmap"),
                        col=NULL, ...)
{
  library(corrplot)
  # Helper functions
  #+++++++++++++++++
  # Compute the matrix of correlation p-values
  cor.pmat <- function(x, ...) {
    mat <- as.matrix(x)
    n <- ncol(mat)
    p.mat<- matrix(NA, n, n)
    diag(p.mat) <- 0
    for (i in 1:(n - 1)) {
      for (j in (i + 1):n) {
        tmp <- cor.test(mat[, i], mat[, j], ...)
        p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
      }
    }
    colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
    p.mat
  }
  # Get lower triangle of the matrix
  getLower.tri<-function(mat){
    upper<-mat
    upper[upper.tri(mat)]<-""
    mat<-as.data.frame(upper)
    mat
  }
  # Get upper triangle of the matrix
  getUpper.tri<-function(mat){
    lt<-mat
    lt[lower.tri(mat)]<-""
    mat<-as.data.frame(lt)
    mat
  }
  # Get flatten matrix
  flattenCorrMatrix <- function(cormat, pmat) {
    ut <- upper.tri(cormat)
    data.frame(
      row = rownames(cormat)[row(cormat)[ut]],
      column = rownames(cormat)[col(cormat)[ut]],
      cor  =(cormat)[ut],
      p = pmat[ut]
    )
  }
  # Define color
  if (is.null(col)) {
    col <- colorRampPalette(
            c("#67001F", "#B2182B", "#D6604D", "#F4A582",
              "#FDDBC7", "#FFFFFF", "#D1E5F0", "#92C5DE", 
             "#4393C3", "#2166AC", "#053061"))(200)
    col<-rev(col)
  }
  
  # Correlation matrix
  cormat<-signif(cor(x, use = "complete.obs", ...),2)
  pmat<-signif(cor.pmat(x, ...),2)
  # Reorder correlation matrix
  ord<-corrMatOrder(cormat, order="hclust")
  cormat<-cormat[ord, ord]
  pmat<-pmat[ord, ord]
  # Replace correlation coeff by symbols
  sym<-symnum(cormat, abbr.colnames=FALSE)
  # Correlogram
  if(graph &amp; graphType[1]=="correlogram"){
    corrplot(cormat, type=ifelse(type[1]=="flatten", "lower", type[1]),
             tl.col="black", tl.srt=45,col=col,...)
  }
  else if(graphType[1]=="heatmap")
    heatmap(cormat, col=col, symm=TRUE)
  # Get lower/upper triangle
  if(type[1]=="lower"){
    cormat<-getLower.tri(cormat)
    pmat<-getLower.tri(pmat)
  }
  else if(type[1]=="upper"){
    cormat<-getUpper.tri(cormat)
    pmat<-getUpper.tri(pmat)
    sym=t(sym)
  }
  else if(type[1]=="flatten"){
    cormat<-flattenCorrMatrix(cormat, pmat)
    pmat=NULL
    sym=NULL
  }
  list(r=cormat, p=pmat, sym=sym)
}</code></pre>
</div>
<div id="infos" class="section level1">
<h1>Infos</h1>
<p><span class="warning"> This analysis has been performed using R (ver. 3.2.4). </span></p>
<p><br/></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('#rdoc h1').addClass('wiki_paragraph1');
    jQuery('#rdoc h2').addClass('wiki_paragraph2');
    jQuery('#rdoc h3').addClass('wiki_paragraph3');
    jQuery('#rdoc h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->


<!-- END HTML -->]]></description>
			<pubDate>Wed, 05 Oct 2016 16:31:30 +0200</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Correlation Test Between Two Variables in R]]></title>
			<link>https://www.sthda.com/english/wiki/correlation-test-between-two-variables-in-r</link>
			<guid>https://www.sthda.com/english/wiki/correlation-test-between-two-variables-in-r</guid>
			<description><![CDATA[<!-- START HTML -->

  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">

<div id="TOC">
<ul>
<li><a href="#what-is-correlation-test">What is correlation test?</a></li>
<li><a href="#install-and-load-required-r-packages">Install and load required R packages</a></li>
<li><a href="#methods-for-correlation-analyses">Methods for correlation analyses</a></li>
<li><a href="#correlation-formula">Correlation formula</a><ul>
<li><a href="#pearson-correlation-formula">Pearson correlation formula</a></li>
<li><a href="#spearman-correlation-formula">Spearman correlation formula</a></li>
<li><a href="#kendall-correlation-formula">Kendall correlation formula</a></li>
</ul></li>
<li><a href="#compute-correlation-in-r">Compute correlation in R</a><ul>
<li><a href="#r-functions">R functions</a></li>
<li><a href="#import-your-data-into-r">Import your data into R</a></li>
<li><a href="#visualize-your-data-using-scatter-plots">Visualize your data using scatter plots</a></li>
<li><a href="#preleminary-test-to-check-the-test-assumptions">Preleminary test to check the test assumptions</a></li>
<li><a href="#pearson-correlation-test">Pearson correlation test</a><ul>
<li><a href="#interpretation-of-the-result">Interpretation of the result</a></li>
<li><a href="#access-to-the-values-returned-by-cor.test-function">Access to the values returned by cor.test() function</a></li>
</ul></li>
<li><a href="#kendall-rank-correlation-test">Kendall rank correlation test</a></li>
<li><a href="#spearman-rank-correlation-coefficient">Spearman rank correlation coefficient</a></li>
</ul></li>
<li><a href="#interpret-correlation-coefficient">Interpret correlation coefficient</a></li>
<li><a href="#online-correlation-coefficient-calculator">Online correlation coefficient calculator</a></li>
<li><a href="#summary">Summary</a></li>
<li><a href="#infos">Infos</a></li>
</ul>
</div>

<p><br/></p>
<div id="what-is-correlation-test" class="section level1">
<h1>What is correlation test?</h1>
<br/>
<div class="block">
<strong>Correlation test</strong> is used to evaluate the association between two or more variables.
</div>
<p><br/></p>
<p>For instance, if we are interested to know whether there is a relationship between the heights of fathers and sons, a <strong>correlation coefficient</strong> can be calculated to answer this question.</p>
<p><span class="notice">If there is no relationship between the two variables (father and son heights), the average height of son should be the same regardless of the height of the fathers and vice versa.</span></p>
<p><span class="success">Here, we’ll describe the different correlation methods and we’ll provide pratical examples using <strong>R</strong> software.</span></p>
</div>
<div id="install-and-load-required-r-packages" class="section level1">
<h1>Install and load required R packages</h1>
<p>We’ll use the <a href="https://www.sthda.com/english/english/wiki/ggpubr-r-package-ggplot2-based-publication-ready-plots"><strong>ggpubr</strong> R package</a> for an easy ggplot2-based data visualization</p>
<ul>
<li>Install the latest version from GitHub as follow (recommended):</li>
</ul>
<pre class="r"><code>if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/ggpubr")</code></pre>
<ul>
<li>Or, install from CRAN as follow:</li>
</ul>
<pre class="r"><code>install.packages("ggpubr")</code></pre>
<ul>
<li>Load ggpubr as follow:</li>
</ul>
<pre class="r"><code>library("ggpubr")</code></pre>
</div>
<div id="methods-for-correlation-analyses" class="section level1">
<h1>Methods for correlation analyses</h1>
<p>There are different methods to perform <strong>correlation analysis</strong>:</p>
<ul>
<li><p><strong>Pearson correlation (r)</strong>, which measures a linear dependence between two variables (x and y). It’s also known as a <strong>parametric correlation</strong> test because it depends to the distribution of the data. It can be used only when x and y are from normal distribution. The plot of y = f(x) is named the <strong>linear regression</strong> curve.</p></li>
<li><p><strong>Kendall tau</strong> and <strong>Spearman rho</strong>, which are rank-based correlation coefficients (non-parametric)</p></li>
</ul>
<p><span class="success">The most commonly used method is the <strong>Pearson correlation</strong> method.</span></p>
</div>
<div id="correlation-formula" class="section level1">
<h1>Correlation formula</h1>
<p>In the formula below,</p>
<ul>
<li><strong>x</strong> and <strong>y</strong> are two vectors of length <strong>n</strong></li>
<li><span class="math">\(m_x\)</span> and <span class="math">\(m_y\)</span> corresponds to the means of x and y, respectively.</li>
</ul>
<div id="pearson-correlation-formula" class="section level2">
<h2>Pearson correlation formula</h2>
<p><span class="math">\[ 
r = \frac{\sum{(x-m_x)(y-m_y)}}{\sqrt{\sum{(x-m_x)^2}\sum{(y-m_y)^2}}}
\]</span></p>
<p><span class="math">\(m_x\)</span> and <span class="math">\(m_y\)</span> are the means of x and y variables.</p>
<p>The p-value (significance level) of the correlation can be determined :</p>
<ol style="list-style-type: decimal">
<li><p>by using the correlation coefficient table for the degrees of freedom : <span class="math">\(df = n-2\)</span>, where <span class="math">\(n\)</span> is the number of observation in x and y variables.</p></li>
<li><p>or by calculating the <strong>t value</strong> as follow:</p></li>
</ol>
<p><span class="math">\[
t = \frac{r}{\sqrt{1-r^2}}\sqrt{n-2}
\]</span></p>
<p>In the case 2) the corresponding p-value is determined using <a href="https://www.sthda.com/english/english/wiki/t-distribution-table"><strong>t distribution table</strong></a> for <span class="math">\(df = n-2\)</span></p>
<p><span class="success">If the p-value is < 5%, then the correlation between x and y is significant.</span></p>
</div>
<div id="spearman-correlation-formula" class="section level2">
<h2>Spearman correlation formula</h2>
<p>The <strong>Spearman correlation</strong> method computes the correlation between the rank of x and the rank of y variables.</p>
<p><span class="math">\[
rho = \frac{\sum(x&amp;#39; - m_{x&amp;#39;})(y&amp;#39;_i - m_{y&amp;#39;})}{\sqrt{\sum(x&amp;#39; - m_{x&amp;#39;})^2 \sum(y&amp;#39; - m_{y&amp;#39;})^2}}
\]</span></p>
<p>Where <span class="math">\(x&amp;#39; = rank(x_)\)</span> and <span class="math">\(y&amp;#39; = rank(y)\)</span>.</p>
</div>
<div id="kendall-correlation-formula" class="section level2">
<h2>Kendall correlation formula</h2>
<p>The <strong>Kendall correlation</strong> method measures the correspondence between the ranking of x and y variables. The total number of possible pairings of x with y observations is <span class="math">\(n(n-1)/2\)</span>, where n is the size of x and y.</p>
<p>The procedure is as follow:</p>
<ul>
<li><p>Begin by ordering the pairs by the x values. If x and y are correlated, then they would have the same relative rank orders.</p></li>
<li><p>Now, for each <span class="math">\(y_i\)</span>, count the number of <span class="math">\(y_j > y_i\)</span> (<strong>concordant pairs (c)</strong>) and the number of <span class="math">\(y_j < y_i\)</span> (<strong>discordant pairs (d)</strong>).</p></li>
</ul>
<p><strong>Kendall correlation distance</strong> is defined as follow:</p>
<p><span class="math">\[
tau = \frac{n_c - n_d}{\frac{1}{2}n(n-1)}
\]</span></p>
<p>Where,</p>
<ul>
<li><span class="math">\(n_c\)</span>: total number of concordant pairs</li>
<li><span class="math">\(n_d\)</span>: total number of discordant pairs</li>
<li><span class="math">\(n\)</span>: size of x and y</li>
</ul>
</div>
</div>
<div id="compute-correlation-in-r" class="section level1">
<h1>Compute correlation in R</h1>
<div id="r-functions" class="section level2">
<h2>R functions</h2>
<p>Correlation coefficient can be computed using the functions <strong>cor()</strong> or <strong>cor.test()</strong>:</p>
<br/>
<div class="block">
<ul>
<li><strong>cor()</strong> computes the <strong>correlation coefficient</strong></li>
<li><strong>cor.test()</strong> test for association/correlation between paired samples. It returns both the <strong>correlation coefficient</strong> and the <strong>significance level</strong>(or p-value) of the correlation .</li>
</ul>
</div>
<p><br/></p>
<p>The simplified formats are:</p>
<pre class="r"><code>cor(x, y, method = c("pearson", "kendall", "spearman"))

cor.test(x, y, method=c("pearson", "kendall", "spearman"))</code></pre>
<br/>
<div class="block">
<ul>
<li><strong>x, y</strong>: numeric vectors with the same length</li>
<li><strong>method</strong>: correlation method</li>
</ul>
</div>
<p><br></p>
<p><span class="warning">If your data contain missing values, use the following R code to handle missing values by case-wise deletion.</span></p>
<pre class="r"><code>cor(x, y,  method = "pearson", use = "complete.obs")</code></pre>
</div>
<div id="import-your-data-into-r" class="section level2">
<h2>Import your data into R</h2>
<ol style="list-style-type: decimal">
<li><p><strong>Prepare your data</strong> as specified here: <a href="https://www.sthda.com/english/english/wiki/best-practices-for-preparing-your-data-set-for-r">Best practices for preparing your data set for R</a></p></li>
<li><p><strong>Save your data</strong> in an external .txt tab or .csv files</p></li>
<li><p><strong>Import your data into R</strong> as follow:</p></li>
</ol>
<pre class="r"><code># If .txt tab file, use this
my_data <- read.delim(file.choose())

# Or, if .csv file, use this
my_data <- read.csv(file.choose())</code></pre>
<p>Here, we’ll use the built-in R data set <em>mtcars</em> as an example.</p>
<p>The R code below computes the correlation between mpg and wt variables in mtcars data set:</p>
<pre class="r"><code>my_data <- mtcars
head(my_data, 6)</code></pre>
<pre><code>                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1</code></pre>
<p><span class="question">We want to compute the correlation between mpg and wt variables.</span></p>
</div>
<div id="visualize-your-data-using-scatter-plots" class="section level2">
<h2>Visualize your data using scatter plots</h2>
<p>To use R base graphs, click this link: <a href="https://www.sthda.com/english/english/wiki/scatter-plots-r-base-graphs">scatter plot - R base graphs</a>. Here, we’ll use the <a href="https://www.sthda.com/english/english/wiki/ggpubr-r-package-ggplot2-based-publication-ready-plots"><strong>ggpubr</strong> R package</a>.</p>
<pre class="r"><code>library("ggpubr")
ggscatter(my_data, x = "mpg", y = "wt", 
          add = "reg.line", conf.int = TRUE, 
          cor.coef = TRUE, cor.method = "pearson",
          xlab = "Miles/(US) gallon", ylab = "Weight (1000 lbs)")</code></pre>
<div class="figure">
<img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-test-between-two-variables-correlation-test-scatter-plot-1.png" alt="Correlation Test Between Two Variables in R software" width="480" style="margin-bottom:10px;" />
<p class="caption">
Correlation Test Between Two Variables in R software
</p>
</div>
</div>
<div id="preleminary-test-to-check-the-test-assumptions" class="section level2">
<h2>Preleminary test to check the test assumptions</h2>
<ol style="list-style-type: decimal">
<li><p><strong>Is the covariation linear</strong>? Yes, form the plot above, the relationship is linear. In the situation where the scatter plots show curved patterns, we are dealing with nonlinear association between the two variables.</p></li>
<li><strong>Are the data from each of the 2 variables (x, y) follow a normal distribution</strong>?
<ul>
<li>Use Shapiro-Wilk normality test –> R function: <strong>shapiro.test</strong>()</li>
<li>and look at the normality plot —> R function: <strong>ggpubr::ggqqplot</strong>()</li>
</ul></li>
</ol>
<ul>
<li><strong>Shapiro-Wilk test</strong> can be performed as follow:
<ul>
<li>Null hypothesis: the data are normally distributed</li>
<li>Alternative hypothesis: the data are not normally distributed</li>
</ul></li>
</ul>
<pre class="r"><code># Shapiro-Wilk normality test for mpg
shapiro.test(my_data$mpg) # => p = 0.1229

# Shapiro-Wilk normality test for wt
shapiro.test(my_data$wt) # => p = 0.09</code></pre>
<p><span class="success">From the output, the two p-values are greater than the significance level 0.05 implying that the distribution of the data are not significantly different from normal distribution. In other words, we can assume the normality.</span></p>
<ul>
<li><strong>Visual inspection</strong> of the data normality using <strong>Q-Q plots</strong> (quantile-quantile plots). Q-Q plot draws the correlation between a given sample and the normal distribution.</li>
</ul>
<pre class="r"><code>library("ggpubr")
# mpg
ggqqplot(my_data$mpg, ylab = "MPG")
# wt
ggqqplot(my_data$wt, ylab = "WT")</code></pre>
<div class="figure">
<img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-test-between-two-variables-qqplot-1.png" alt="Correlation Test Between Two Variables in R software" width="259.2" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-test-between-two-variables-qqplot-2.png" alt="Correlation Test Between Two Variables in R software" width="259.2" style="margin-bottom:10px;" />
<p class="caption">
Correlation Test Between Two Variables in R software
</p>
</div>
<p><span class="success">From the normality plots, we conclude that both populations may come from normal distributions.</span></p>
<p><span class="warning">Note that, if the data are not normally distributed, it’s recommended to use the non-parametric correlation, including Spearman and Kendall rank-based correlation tests.</span></p>
</div>
<div id="pearson-correlation-test" class="section level2">
<h2>Pearson correlation test</h2>
<p>Correlation test between mpg and wt variables:</p>
<pre class="r"><code>res <- cor.test(my_data$wt, my_data$mpg, 
                    method = "pearson")
res</code></pre>
<pre><code>
    Pearson&amp;#39;s product-moment correlation

data:  my_data$wt and my_data$mpg
t = -9.559, df = 30, p-value = 1.294e-10
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.9338264 -0.7440872
sample estimates:
       cor 
-0.8676594 </code></pre>
<br/>
<div class="block">
<p>In the result above :</p>
<ul>
<li><strong>t</strong> is the <strong>t-test statistic</strong> value (t = -9.559),</li>
<li><strong>df</strong> is the degrees of freedom (df= 30),</li>
<li><strong>p-value</strong> is the significance level of the <strong>t-test</strong> (p-value = 1.29410^{-10}).</li>
<li><strong>conf.int</strong> is the <strong>confidence interval</strong> of the correlation coefficient at 95% (conf.int = [-0.9338, -0.7441]);</li>
<li><strong>sample estimates</strong> is the correlation coefficient (Cor.coeff = -0.87).</li>
</ul>
</div>
<p><br/></p>
<div id="interpretation-of-the-result" class="section level3">
<h3>Interpretation of the result</h3>
<p><span class="success">The <strong>p-value</strong> of the test is 1.29410^{-10}, which is less than the significance level alpha = 0.05. We can conclude that wt and mpg are significantly correlated with a correlation coefficient of -0.87 and p-value of 1.29410^{-10} .</span></p>
</div>
<div id="access-to-the-values-returned-by-cor.test-function" class="section level3">
<h3>Access to the values returned by cor.test() function</h3>
<p>The function <strong>cor.test</strong>() returns a list containing the following components:</p>
<ul>
<li><strong>p.value</strong>: the p-value of the test</li>
<li><strong>estimate</strong>: the correlation coefficient</li>
</ul>
<pre class="r"><code># Extract the p.value
res$p.value</code></pre>
<pre><code>[1] 1.293959e-10</code></pre>
<pre class="r"><code># Extract the correlation coefficient
res$estimate</code></pre>
<pre><code>       cor 
-0.8676594 </code></pre>
</div>
</div>
<div id="kendall-rank-correlation-test" class="section level2">
<h2>Kendall rank correlation test</h2>
<p>The <strong>Kendall rank correlation coefficient</strong> or <strong>Kendall’s tau</strong> statistic is used to estimate a rank-based measure of association. This test may be used if the data do not necessarily come from a bivariate normal distribution.</p>
<pre class="r"><code>res2 <- cor.test(my_data$wt, my_data$mpg,  method="kendall")
res2</code></pre>
<pre><code>
    Kendall&amp;#39;s rank correlation tau

data:  my_data$wt and my_data$mpg
z = -5.7981, p-value = 6.706e-09
alternative hypothesis: true tau is not equal to 0
sample estimates:
       tau 
-0.7278321 </code></pre>
<p><strong>tau</strong> is the <strong>Kendall correlation coefficient</strong>.</p>
<p><span class="success"> The <strong>correlation coefficient</strong> between x and y are -0.7278 and the <em>p-value</em> is 6.70610^{-9}. </span></p>
</div>
<div id="spearman-rank-correlation-coefficient" class="section level2">
<h2>Spearman rank correlation coefficient</h2>
<p>Spearman’s <strong>rho</strong> statistic is also used to estimate a rank-based measure of association. This test may be used if the data do not come from a bivariate normal distribution.</p>
<pre class="r"><code>res2 <-cor.test(my_data$wt, my_data$mpg,  method = "spearman")
res2</code></pre>
<pre><code>
    Spearman&amp;#39;s rank correlation rho

data:  my_data$wt and my_data$mpg
S = 10292, p-value = 1.488e-11
alternative hypothesis: true rho is not equal to 0
sample estimates:
      rho 
-0.886422 </code></pre>
<p><strong>rho</strong> is the <strong>Spearman’s correlation coefficient</strong>.</p>
<p><span class="success"> The <strong>correlation coefficient</strong> between x and y are -0.8864 and the <em>p-value</em> is 1.48810^{-11}. </span></p>
</div>
</div>
<div id="interpret-correlation-coefficient" class="section level1">
<h1>Interpret correlation coefficient</h1>
<p>Correlation coefficient is comprised between <strong>-1</strong> and <strong>1</strong>:</p>
<br/>
<div class="block">
<ul>
<li><strong>-1</strong> indicates a strong <strong>negative correlation</strong> : this means that every time <strong>x increases</strong>, <strong>y decreases</strong> (left panel figure)</li>
<li><strong>0</strong> means that there is no <strong>association</strong> between the two variables (x and y) (middle panel figure)</li>
<li><strong>1</strong> indicates a strong <strong>positive correlation</strong> : this means that <strong>y increases</strong> with <strong>x</strong> (right panel figure)</li>
</ul>
</div>
<p><br/></p>
<div class="figure">
<img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-test-between-two-variables-correlation-coefficient-1.png" alt="Correlation Test Between Two Variables in R software" width="165.6" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-test-between-two-variables-correlation-coefficient-2.png" alt="Correlation Test Between Two Variables in R software" width="165.6" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-test-between-two-variables-correlation-coefficient-3.png" alt="Correlation Test Between Two Variables in R software" width="165.6" style="margin-bottom:10px;" />
<p class="caption">
Correlation Test Between Two Variables in R software
</p>
</div>
</div>
<div id="online-correlation-coefficient-calculator" class="section level1">
<h1>Online correlation coefficient calculator</h1>
<p>You can compute correlation test between two variables, online, without any installation by clicking the following link:</p>
<br/>
<div class="block">
<i class="fa - fa-cogs fa-4x valign_middle"></i> <a href="https://www.sthda.com/english/english/rsthda/correlation.php">Correlation coefficient calculator</a>
</div>
<p><br/></p>
</div>
<div id="summary" class="section level1">
<h1>Summary</h1>
<br/>
<div class="block">
<ul>
<li>Use the function <strong>cor.test</strong>(x,y) to analyze the correlation coefficient between two variables and to get significance level of the correlation.</li>
<li>Three possible correlation methods using the function <strong>cor.test</strong>(x,y): pearson, kendall, spearman</li>
</ul>
</div>
<p><br/></p>
</div>
<div id="infos" class="section level1">
<h1>Infos</h1>
<p><span class="warning"> This analysis has been performed using <strong>R software</strong> (ver. 3.2.4). </span></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('#rdoc h1').addClass('wiki_paragraph1');
    jQuery('#rdoc h2').addClass('wiki_paragraph2');
    jQuery('#rdoc h3').addClass('wiki_paragraph3');
    jQuery('#rdoc h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src  = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>
<!--====================== stop here when you copy to sthda================-->


<!-- END HTML -->]]></description>
			<pubDate>Fri, 30 Sep 2016 19:45:17 +0200</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Correlation Analyses in R]]></title>
			<link>https://www.sthda.com/english/wiki/correlation-analyses-in-r</link>
			<guid>https://www.sthda.com/english/wiki/correlation-analyses-in-r</guid>
			<description><![CDATA[<!-- START HTML -->

  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">

<p><br/></p>
<p>Previously, we described the <a href="https://www.sthda.com/english/english/wiki/r-basics-quick-and-easy">essentials of R programming</a> and provided quick start guides for <a href="https://www.sthda.com/english/english/wiki/importing-data-into-r">importing data</a> into <strong>R</strong>. Additionally, we described how to compute <a href="https://www.sthda.com/english/english/wiki/descriptive-statistics-and-graphics">descriptive or summary statistics</a> using R software.</p>
<br/>
<div class="block">
This chapter contains articles for computing and visualizing <strong>correlation analyses</strong> in R. Recall that, <strong>correlation analysis</strong> is used to investigate the association between two or more variables. A simple example, is to evaluate whether there is a link between maternal age and child’s weight at birth.

</div>
<p><br/></p>
<div id="how-this-chapter-is-organized" class="section level1">
<h1>How this chapter is organized?</h1>
<br/>
<div class="block">
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/correlation-test-between-two-variables-in-r">Correlation Test Between Two Variables in R</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/correlation-matrix-a-quick-start-guide-to-analyze-format-and-visualize-a-correlation-matrix-using-r-software">Correlation Matrix: Analyze, Format and Visualize</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/visualize-correlation-matrix-using-correlogram">Visualize Correlation Matrix using Correlogram</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/elegant-correlation-table-using-xtable-r-package">Elegant correlation table using xtable R package</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/correlation-matrix-an-r-function-to-do-all-you-need">Correlation Matrix : An R Function to Do All You Need</a></li>
</ul>
</div>
<p><br/></p>
<p>
 <img src="https://www.sthda.com/english/sthda/RDoc/images/correlation-analysis-in-r.png" alt="Correlation Analysis in R" /> <br/></p>
<hr/>
</div>
<div id="correlation-test-between-two-variables-in-r" class="section level1">
<h1>Correlation Test Between Two Variables in R</h1>
<p>Brief outline:</p>
<ul>
<li>What is correlation test?</li>
<li>Methods for correlation analyses</li>
<li>Correlation formula
<ul>
<li>Pearson correlation formula</li>
<li>Spearman correlation formula</li>
<li>Kendall correlation formula</li>
</ul></li>
<li>Compute correlation in R
<ul>
<li>R functions</li>
<li>Import your data into R</li>
<li>Visualize your data using scatter plots</li>
<li>Preliminary test to check the test assumptions</li>
<li>Pearson correlation test</li>
<li>Kendall rank correlation test</li>
<li>Spearman rank correlation coefficient</li>
</ul></li>
<li>Interpret correlation coefficient</li>
</ul>
<p><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-analysis-in-r-correlation-test-scatter-plot-1.png" width="480" style="margin-bottom:10px;" /></p>
<p><span class="success">Read more: —> <a href="https://www.sthda.com/english/english/wiki/correlation-test-between-two-variables-in-r">Correlation Test Between Two Variables in R</a>.</span></p>
</div>
<div id="correlation-matrix-analyze-format-and-visualize" class="section level1">
<h1>Correlation Matrix: Analyze, Format and Visualize</h1>
<p><strong>Correlation matrix</strong> is used to analyze the correlation between multiple variables at the same time.</p>
<p>Brief outline:</p>
<ul>
<li>What is correlation matrix?</li>
<li>Compute correlation matrix in R
<ul>
<li>R functions</li>
<li>Compute correlation matrix</li>
<li>Correlation matrix with significance levels (p-value)</li>
<li>A simple function to format the correlation matrix</li>
<li>Visualize correlation matrix
<ul>
<li>Use symnum() function: Symbolic number coding</li>
<li>Use corrplot() function: Draw a correlogram</li>
<li>Use chart.Correlation(): Draw scatter plots</li>
<li>Use heatmap()</li>
</ul></li>
</ul></li>
</ul>
<div class="figure">
<img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-analysis-in-r-graph-1.png" alt="scatter plot, chart" width="528" style="margin-bottom:10px;" />
<p class="caption">
scatter plot, chart
</p>
</div>
<p><span class="success">Read more: —> <a href="https://www.sthda.com/english/english/wiki/correlation-matrix-a-quick-start-guide-to-analyze-format-and-visualize-a-correlation-matrix-using-r-software">Correlation Matrix: Analyze, Format and Visualize</a>.</span></p>
</div>
<div id="visualize-correlation-matrix-using-correlogram" class="section level1">
<h1>Visualize Correlation Matrix using Correlogram</h1>
<p><strong>Correlogram</strong> is a <strong>graph of correlation matrix</strong>. Useful to highlight the most correlated variables in a data table. In this plot, <strong>correlation coefficients</strong> are colored according to the value. <strong>Correlation matrix</strong> can be also reordered according to the degree of association between variables.</p>
<p>Brief outline:</p>
<ul>
<li>Install R corrplot package</li>
<li>Data for correlation analysis</li>
<li>Computing correlation matrix</li>
<li>Correlogram : Visualizing the correlation matrix
<ul>
<li>Visualization methods</li>
<li>Types of correlogram layout</li>
<li>Reordering the correlation matrix</li>
<li>Changing the color of the correlogram</li>
<li>Changing the color and the rotation of text labels</li>
<li>Combining correlogram with the significance test</li>
<li>Customize the correlogram</li>
</ul></li>
</ul>
<pre class="r"><code>library(corrplot)
library(RColorBrewer)
M <-cor(mtcars)
corrplot(M, type="upper", order="hclust",
         col=brewer.pal(n=8, name="RdYlBu"))</code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-analysis-in-r-correlation-matrix-correlogram-1.png" width="480" style="margin-bottom:10px;" /></p>
<p><span class="success">Read more: —> <a href="https://www.sthda.com/english/english/wiki/visualize-correlation-matrix-using-correlogram">Visualize Correlation Matrix using Correlogram</a>.</span></p>
</div>
<div id="elegant-correlation-table-using-xtable-r-package" class="section level1">
<h1>Elegant Correlation Table using xtable R Package</h1>
<p>The aim of this article is to show you how to get the <strong>lower and the upper triangular part of a correlation matrix</strong>. We will use also <strong>xtable R package</strong> to display a nice <strong>correlation table</strong>.</p>
<p>Brief outline:</p>
<ul>
<li>Correlation matrix analysis</li>
<li>Lower and upper triangular part of a correlation matrix</li>
<li>Use xtable R package to display nice correlation table in html format</li>
<li>Combine matrix of correlation coefficients and significance levels</li>
</ul>
<p>
 <img src="https://www.sthda.com/english/sthda/RDoc/images/correlation-matrix-visualization-xtable.png" alt="Elegant correlation table using xtable R package" /> 
</p>
<p><span class="success">Read more: —> <a href="https://www.sthda.com/english/english/wiki/elegant-correlation-table-using-xtable-r-package">Elegant correlation table using xtable R package</a>.</span></p>
</div>
<div id="correlation-matrix-an-r-function-to-do-all-you-need" class="section level1">
<h1>Correlation Matrix : An R Function to Do All You Need</h1>
<p>The goal of this article is to provide you a custom <strong>R function</strong>, named <strong>rquery.cormat</strong>(), for <strong>calculating</strong> and <strong>visualizing</strong> easily a <strong>correlation matrix</strong> in a single line R code.</p>
<p>Brief outline:</p>
<ul>
<li>Computing the correlation matrix using rquery.cormat()
<ul>
<li>Upper triangle of the correlation matrix</li>
<li>Full correlation matrix</li>
<li>Change the colors of the correlogram</li>
<li>Draw a heatmap</li>
</ul></li>
<li>Format the correlation table</li>
<li>Description of rquery.cormat() function</li>
</ul>
<pre class="r"><code>source("https://www.sthda.com/upload/rquery_cormat.r")
mydata <- mtcars[, c(1,3,4,5,6,7)]
require("corrplot")
rquery.cormat(mydata)</code></pre>
<pre><code>$r
        hp  disp    wt  qsec  mpg drat
hp       1                            
disp  0.79     1                      
wt    0.66  0.89     1                
qsec -0.71 -0.43 -0.17     1          
mpg  -0.78 -0.85 -0.87  0.42    1     
drat -0.45 -0.71 -0.71 0.091 0.68    1

$p
          hp    disp      wt  qsec     mpg drat
hp         0                                   
disp 7.1e-08       0                           
wt   4.1e-05 1.2e-11       0                   
qsec 5.8e-06   0.013    0.34     0             
mpg  1.8e-07 9.4e-10 1.3e-10 0.017       0     
drat    0.01 5.3e-06 4.8e-06  0.62 1.8e-05    0

$sym
     hp disp wt qsec mpg drat
hp   1                       
disp ,  1                    
wt   ,  +    1               
qsec ,  .       1            
mpg  ,  +    +  .    1       
drat .  ,    ,       ,   1   
attr(,"legend")
[1] 0 &amp;#39; &amp;#39; 0.3 &amp;#39;.&amp;#39; 0.6 &amp;#39;,&amp;#39; 0.8 &amp;#39;+&amp;#39; 0.9 &amp;#39;*&amp;#39; 0.95 &amp;#39;B&amp;#39; 1</code></pre>
<p><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-analysis-in-r-rquery-cormat-1.png" width="480" style="margin-bottom:10px;" /></p>
<p><span class="success">Read more: —> <a href="https://www.sthda.com/english/english/wiki/correlation-matrix-an-r-function-to-do-all-you-need">Correlation Matrix : An R Function to Do All You Need</a>.</span></p>
</div>
<div id="see-also" class="section level1">
<h1>See also</h1>
<ul>
<li><a href="https://www.sthda.com/english/english/wiki/r-basics-quick-and-easy">R Basics</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/import-and-export-data-using-r">Import and Export Data using R</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/preparing-and-reshaping-data-in-r-for-easier-analyses">Preparing and Reshaping Data in R for Easier Analyses</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/data-manipulation-in-r">Data Manipulation in R</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/data-visualization">Data visualization</a></li>
<li><a href="https://www.sthda.com/english/english/wiki/descriptive-statistics-and-graphics">Descriptive Statistics and Graphics</a></li>
</ul>
</div>
<div id="infos" class="section level1">
<h1>Infos</h1>
<p><span class="warning"> This analysis has been performed using <strong>R statistical software</strong> (ver. 3.2.4). </span></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('#rdoc h1').addClass('wiki_paragraph1');
    jQuery('#rdoc h2').addClass('wiki_paragraph2');
    jQuery('#rdoc h3').addClass('wiki_paragraph3');
    jQuery('#rdoc h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->

<!-- END HTML -->]]></description>
			<pubDate>Tue, 27 Sep 2016 15:28:15 +0200</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Correlation matrix : A quick start guide to analyze, format and visualize a correlation matrix using R software]]></title>
			<link>https://www.sthda.com/english/wiki/correlation-matrix-a-quick-start-guide-to-analyze-format-and-visualize-a-correlation-matrix-using-r-software</link>
			<guid>https://www.sthda.com/english/wiki/correlation-matrix-a-quick-start-guide-to-analyze-format-and-visualize-a-correlation-matrix-using-r-software</guid>
			<description><![CDATA[<!-- START HTML -->

  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">

<div id="TOC">
<ul>
<li><a href="#what-is-correlation-matrix">What is correlation matrix?</a></li>
<li><a href="#compute-correlation-matrix-in-r">Compute correlation matrix in R</a><ul>
<li><a href="#r-functions">R functions</a></li>
<li><a href="#import-your-data-into-r">Import your data into R</a></li>
<li><a href="#compute-correlation-matrix">Compute correlation matrix</a></li>
<li><a href="#correlation-matrix-with-significance-levels-p-value">Correlation matrix with significance levels (p-value)</a></li>
<li><a href="#a-simple-function-to-format-the-correlation-matrix">A simple function to format the correlation matrix</a></li>
<li><a href="#visualize-correlation-matrix">Visualize correlation matrix</a><ul>
<li><a href="#use-symnum-function-symbolic-number-coding">Use symnum() function: Symbolic number coding</a></li>
<li><a href="#use-corrplot-function-draw-a-correlogram">Use corrplot() function: Draw a correlogram</a></li>
<li><a href="#use-chart.correlation-draw-scatter-plots">Use chart.Correlation(): Draw scatter plots</a></li>
<li><a href="#use-heatmap">Use heatmap()</a></li>
</ul></li>
</ul></li>
<li><a href="#online-software-to-analyze-and-visualize-a-correlation-matrix">Online software to analyze and visualize a correlation matrix</a></li>
<li><a href="#summarry">Summarry</a></li>
<li><a href="#infos">Infos</a></li>
</ul>
</div>

<p><br/></p>
<div id="what-is-correlation-matrix" class="section level1">
<h1>What is correlation matrix?</h1>
<br/>
<div class="block">
Previously, we described how to perform <a href="https://www.sthda.com/english/english/wiki/correlation-test-between-two-variables-in-r"><strong>correlation test</strong> between two variables</a>. In this article, you’ll learn how to compute a <strong>correlation matrix</strong>, which is used to investigate the dependence between multiple variables at the same time. The result is a table containing the <strong>correlation coefficients</strong> between each variable and the others.
</div>
<p><br/></p>
<p>There are different methods for <strong>correlation analysis</strong> : <strong>Pearson parametric correlation test</strong>, <strong>Spearman</strong> and <strong>Kendall</strong> rank-based <strong>correlation analysis</strong>. These methods are discussed in the next sections.</p>
<p><span class="success">The aim of this <strong>R tutorial</strong> is to show you how to compute and visualize a <strong>correlation matrix in R</strong>. We provide also an <a href="https://www.sthda.com/english/wiki/(../rsthda/correlation-matrix.php)">online software</a> for computing and visualizing a correlation matrix.</span></p>
</div>
<div id="compute-correlation-matrix-in-r" class="section level1">
<h1>Compute correlation matrix in R</h1>
<div id="r-functions" class="section level2">
<h2>R functions</h2>
<p>As you may know, The <strong>R</strong> function <strong>cor()</strong> can be used to compute a <strong>correlation matrix</strong>. A simplified format of the function is :</p>
<pre class="r"><code>cor(x, method = c("pearson", "kendall", "spearman"))</code></pre>
<br/>
<div class="block">
<ul>
<li><strong>x</strong>: numeric matrix or a data frame.</li>
<li><strong>method</strong>: indicates the <strong>correlation coefficient</strong> to be computed. The default is <a href="https://www.sthda.com/english/english/wiki/correlation-test-between-two-variables">pearson correlation coefficient</a> which measures the linear <strong>dependence</strong> between two variables. <a href="https://www.sthda.com/english/english/wiki/correlation-test-between-two-variables">kendall and spearman</a> correlation methods are non-parametric <strong>rank-based correlation test</strong>.</li>
</ul>
</div>
<p><br></p>
<p><span class="warning">If your data contain missing values, use the following R code to handle missing values by case-wise deletion.</span></p>
<pre class="r"><code>cor(x, method = "pearson", use = "complete.obs")</code></pre>
</div>
<div id="import-your-data-into-r" class="section level2">
<h2>Import your data into R</h2>
<ol style="list-style-type: decimal">
<li><p><strong>Prepare your data</strong> as specified here: <a href="https://www.sthda.com/english/english/wiki/best-practices-for-preparing-your-data-set-for-r">Best practices for preparing your data set for R</a></p></li>
<li><p><strong>Save your data</strong> in an external .txt tab or .csv files</p></li>
<li><p><strong>Import your data into R</strong> as follow:</p></li>
</ol>
<pre class="r"><code># If .txt tab file, use this
my_data <- read.delim(file.choose())

# Or, if .csv file, use this
my_data <- read.csv(file.choose())</code></pre>
<p>Here, we’ll use a data derived from the built-in R data set <em>mtcars</em> as an example:</p>
<pre class="r"><code># Load data
data("mtcars")
my_data <- mtcars[, c(1,3,4,5,6,7)]
# print the first 6 rows
head(my_data, 6)</code></pre>
<pre><code>                   mpg disp  hp drat    wt  qsec
Mazda RX4         21.0  160 110 3.90 2.620 16.46
Mazda RX4 Wag     21.0  160 110 3.90 2.875 17.02
Datsun 710        22.8  108  93 3.85 2.320 18.61
Hornet 4 Drive    21.4  258 110 3.08 3.215 19.44
Hornet Sportabout 18.7  360 175 3.15 3.440 17.02
Valiant           18.1  225 105 2.76 3.460 20.22</code></pre>
</div>
<div id="compute-correlation-matrix" class="section level2">
<h2>Compute correlation matrix</h2>
<pre class="r"><code>res <- cor(my_data)
round(res, 2)</code></pre>
<pre><code>       mpg  disp    hp  drat    wt  qsec
mpg   1.00 -0.85 -0.78  0.68 -0.87  0.42
disp -0.85  1.00  0.79 -0.71  0.89 -0.43
hp   -0.78  0.79  1.00 -0.45  0.66 -0.71
drat  0.68 -0.71 -0.45  1.00 -0.71  0.09
wt   -0.87  0.89  0.66 -0.71  1.00 -0.17
qsec  0.42 -0.43 -0.71  0.09 -0.17  1.00</code></pre>
<p><span class="success>In the table above **correlations coefficients** between the possible pairs of variables are shown.</span> <span class=" warning"="">In the table above <strong>correlations coefficients</strong> between the possible pairs of variables are shown.</span></p>
<p><span class="warning">Note that, if your data contain missing values, use the following R code to handle missing values by case-wise deletion.</span></p>
<pre class="r"><code>cor(my_data, use = "complete.obs")</code></pre>
<p><span class="warning"> Unfortunately, the function <strong>cor()</strong> returns only the <strong>correlation coefficients</strong> between variables. In the next section, we will use <strong>Hmisc R package</strong> to calculate the <strong>correlation p-values</strong>.</span></p>
</div>
<div id="correlation-matrix-with-significance-levels-p-value" class="section level2">
<h2>Correlation matrix with significance levels (p-value)</h2>
<p>The function <strong>rcorr()</strong> [in <strong>Hmisc</strong> package] can be used to compute the <strong>significance levels</strong> for <strong>pearson</strong> and <strong>spearman correlations</strong>. It returns both the correlation coefficients and the p-value of the correlation for all possible pairs of columns in the data table.</p>
<ul>
<li>Simplified format:</li>
</ul>
<pre class="r"><code>rcorr(x, type = c("pearson","spearman"))</code></pre>
<p><span class="warning"><strong>x</strong> should be a matrix. The <strong>correlation type</strong> can be either <strong>pearson</strong> or <strong>spearman</strong>.</span></p>
<ul>
<li>Install <strong>Hmisc</strong> package:</li>
</ul>
<pre class="r"><code>install.packages("Hmisc")</code></pre>
<ul>
<li>Use <strong>rcorr</strong>() function</li>
</ul>
<pre class="r"><code>library("Hmisc")
res2 <- rcorr(as.matrix(my_data))
res2</code></pre>
<pre><code>       mpg  disp    hp  drat    wt  qsec
mpg   1.00 -0.85 -0.78  0.68 -0.87  0.42
disp -0.85  1.00  0.79 -0.71  0.89 -0.43
hp   -0.78  0.79  1.00 -0.45  0.66 -0.71
drat  0.68 -0.71 -0.45  1.00 -0.71  0.09
wt   -0.87  0.89  0.66 -0.71  1.00 -0.17
qsec  0.42 -0.43 -0.71  0.09 -0.17  1.00

n= 32 


P
     mpg    disp   hp     drat   wt     qsec  
mpg         0.0000 0.0000 0.0000 0.0000 0.0171
disp 0.0000        0.0000 0.0000 0.0000 0.0131
hp   0.0000 0.0000        0.0100 0.0000 0.0000
drat 0.0000 0.0000 0.0100        0.0000 0.6196
wt   0.0000 0.0000 0.0000 0.0000        0.3389
qsec 0.0171 0.0131 0.0000 0.6196 0.3389       </code></pre>
<p><span class="success"> The output of the function <strong>rcorr()</strong> is a list containing the following elements :
- <strong>r</strong> : the <strong>correlation matrix</strong>
- <strong>n</strong> : the matrix of the number of observations used in analyzing each pair of variables
- <strong>P</strong> : the <strong>p-values</strong> corresponding to the <strong>significance levels</strong> of <strong>correlations</strong>. </span></p>
<p>If you want to extract the p-values or the correlation coefficients from the output, use this:</p>
<pre class="r"><code># Extract the correlation coefficients
res2$r

# Extract p-values
res2$P</code></pre>
</div>
<div id="a-simple-function-to-format-the-correlation-matrix" class="section level2">
<h2>A simple function to format the correlation matrix</h2>
<p>This section provides a simple function for formatting a <strong>correlation matrix</strong> into a table with 4 columns containing :</p>
<ul>
<li>Column 1 : row names (variable 1 for the correlation test)</li>
<li>Column 2 : column names (variable 2 for the correlation test)</li>
<li>Column 3 : the correlation coefficients</li>
<li>Column 4 : the p-values of the correlations</li>
</ul>
<p>The custom function below can be used :</p>
<pre class="r"><code># ++++++++++++++++++++++++++++
# flattenCorrMatrix
# ++++++++++++++++++++++++++++
# cormat : matrix of the correlation coefficients
# pmat : matrix of the correlation p-values
flattenCorrMatrix <- function(cormat, pmat) {
  ut <- upper.tri(cormat)
  data.frame(
    row = rownames(cormat)[row(cormat)[ut]],
    column = rownames(cormat)[col(cormat)[ut]],
    cor  =(cormat)[ut],
    p = pmat[ut]
    )
}</code></pre>
<p>Example of usage :</p>
<pre class="r"><code>library(Hmisc)
res2<-rcorr(as.matrix(mtcars[,1:7]))
flattenCorrMatrix(res2$r, res2$P)</code></pre>
<pre><code>    row column         cor            p
1   mpg    cyl -0.85216194 6.112697e-10
2   mpg   disp -0.84755135 9.380354e-10
3   cyl   disp  0.90203285 1.803002e-12
4   mpg     hp -0.77616835 1.787838e-07
5   cyl     hp  0.83244747 3.477856e-09
6  disp     hp  0.79094857 7.142686e-08
7   mpg   drat  0.68117189 1.776241e-05
8   cyl   drat -0.69993812 8.244635e-06
9  disp   drat -0.71021390 5.282028e-06
10   hp   drat -0.44875914 9.988768e-03
11  mpg     wt -0.86765939 1.293956e-10
12  cyl     wt  0.78249580 1.217567e-07
13 disp     wt  0.88797992 1.222311e-11
14   hp     wt  0.65874785 4.145833e-05
15 drat     wt -0.71244061 4.784268e-06
16  mpg   qsec  0.41868404 1.708199e-02
17  cyl   qsec -0.59124213 3.660527e-04
18 disp   qsec -0.43369791 1.314403e-02
19   hp   qsec -0.70822340 5.766250e-06
20 drat   qsec  0.09120482 6.195823e-01
21   wt   qsec -0.17471591 3.388682e-01</code></pre>
</div>
<div id="visualize-correlation-matrix" class="section level2">
<h2>Visualize correlation matrix</h2>
<p>There are different ways for visualizing a <strong>correlation matrix</strong> in R software :</p>
<ul>
<li>symnum() function</li>
<li>corrplot() function to plot a <strong>correlogram</strong></li>
<li>scatter plots</li>
<li>heatmap</li>
</ul>
<div id="use-symnum-function-symbolic-number-coding" class="section level3">
<h3>Use symnum() function: Symbolic number coding</h3>
<p>The R function <a href="https://www.sthda.com/english/english/wiki/visualize-correlation-matrix-using-symnum-function">symnum()</a> replaces <strong>correlation coefficients</strong> by symbols according to the level of the correlation. It takes the <strong>correlation matrix</strong> as an argument :</p>
<ul>
<li><strong>Simplified format</strong>:</li>
</ul>
<pre class="r"><code>symnum(x, cutpoints = c(0.3, 0.6, 0.8, 0.9, 0.95),
       symbols = c(" ", ".", ",", "+", "*", "B"),
       abbr.colnames = TRUE)</code></pre>
<br/>
<div class="block">
<ul>
<li><strong>x</strong>: the correlation matrix to visualize
</li>
<li><strong>cutpoints</strong>: <strong>correlation coefficient</strong> cutpoints. The <strong>correlation coefficients</strong> between 0 and 0.3 are replaced by a space (" “); <strong>correlation coefficients</strong> between 0.3 and 0.6 are replace by”.“; etc …
</li>
<li><strong>symbols</strong> : the symbols to use.</li>
<li><strong>abbr.colnames</strong>: logical value. If TRUE, colnames are abbreviated.</li>
</ul>
</div>
<p><br/></p>
<ul>
<li><strong>Example of usage</strong>:</li>
</ul>
<pre class="r"><code>symnum(res, abbr.colnames = FALSE)</code></pre>
<pre><code>     mpg disp hp drat wt qsec
mpg  1                       
disp +   1                   
hp   ,   ,    1              
drat ,   ,    .  1           
wt   +   +    ,  ,    1      
qsec .   .    ,          1   
attr(,"legend")
[1] 0 &amp;#39; &amp;#39; 0.3 &amp;#39;.&amp;#39; 0.6 &amp;#39;,&amp;#39; 0.8 &amp;#39;+&amp;#39; 0.9 &amp;#39;*&amp;#39; 0.95 &amp;#39;B&amp;#39; 1</code></pre>
<p><span class="success"> As indicated in the legend, the <strong>correlation coefficients</strong> between <strong>0</strong> and <strong>0.3</strong> are replaced by a space (" “); <strong>correlation coefficients</strong> between 0.3 and 0.6 are replace by”.“; etc … </span></p>
</div>
<div id="use-corrplot-function-draw-a-correlogram" class="section level3">
<h3>Use corrplot() function: Draw a correlogram</h3>
<p>The function <strong>corrplot</strong>(), in the package of the same name, creates a <strong>graphical</strong> display of a correlation matrix, highlighting the most correlated variables in a data table.</p>
<p>In this plot, correlation coefficients are colored according to the value. Correlation matrix can be also reordered according to the degree of association between variables.</p>
<ul>
<li><strong>Install corrplot</strong>:</li>
</ul>
<pre class="r"><code>install.packages("corrplot")</code></pre>
<ul>
<li><strong>Use corrplot</strong>() to create a <strong>correlogram</strong>:</li>
</ul>
<p>The function <strong>corrplot()</strong> takes the <strong>correlation matrix</strong> as the first argument. The second argument (type=“upper”) is used to display only the upper triangular of the <strong>correlation matrix</strong>.</p>
<pre class="r"><code>library(corrplot)
corrplot(res, type = "upper", order = "hclust", 
         tl.col = "black", tl.srt = 45)</code></pre>
<div class="figure">
<img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-matrix-correlogram-1.png" alt="Correlation matrix - R software and statistics" width="384" style="margin-bottom:10px;" />
<p class="caption">
Correlation matrix - R software and statistics
</p>
</div>
<p><span class="success"> <strong>Positive correlations</strong> are displayed in blue and <strong>negative correlations</strong> in red color. Color intensity and the size of the circle are proportional to the <strong>correlation coefficients</strong>. In the right side of the <strong>correlogram</strong>, the legend color shows the <strong>correlation coefficients</strong> and the corresponding colors. </span></p>
<br/>

<div class="block">
<ul>
<li>The <strong>correlation matrix</strong> is reordered according to the <strong>correlation coefficient</strong> using <strong>“hclust”</strong> method.
</li>
<li><strong>tl.col</strong> (for text label color) and <strong>tl.srt</strong> (for text label string rotation) are used to change text colors and rotations.</li>
<li>Possible values for the argument <strong>type</strong> are : “upper”, “lower”, “full”</li>
</ul>
</div>
<p><br/></p>
<p>Read more : <a href="https://www.sthda.com/english/english/wiki/visualize-correlation-matrix-using-correlogram">visualize a correlation matrix using corrplot</a>.</p>
<p>It’s also possible to <strong>combine correlogram with the significance test</strong>. We’ll use the result <em>res.cor2</em> generated in the previous section with <strong>rcorr</strong>() function [in <strong>Hmisc</strong> package]:</p>
<pre class="r"><code># Insignificant correlation are crossed
corrplot(res2$r, type="upper", order="hclust", 
         p.mat = res2$P, sig.level = 0.01, insig = "blank")
# Insignificant correlations are leaved blank
corrplot(res2$r, type="upper", order="hclust", 
         p.mat = res2$P, sig.level = 0.01, insig = "blank")</code></pre>
<div class="figure">
<img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-matrix-correlogram-significance-1.png" alt="Correlation matrix - R software and statistics" width="384" style="margin-bottom:10px;" />
<p class="caption">
Correlation matrix - R software and statistics
</p>
</div>
<p><span class="success"> In the above plot, correlations with p-value > 0.01 are considered as insignificant. In this case the correlation coefficient values are leaved blank or crosses are added.</span></p>
</div>
<div id="use-chart.correlation-draw-scatter-plots" class="section level3">
<h3>Use chart.Correlation(): Draw scatter plots</h3>
<p>The function <em>chart.Correlation()</em>[ in the package <strong>PerformanceAnalytics</strong>], can be used to display a chart of a correlation matrix.</p>
<ul>
<li><strong>Install PerformanceAnalytics</strong>:</li>
</ul>
<pre class="r"><code>install.packages("PerformanceAnalytics")</code></pre>
<ul>
<li><strong>Use chart.Correlation()</strong>:</li>
</ul>
<pre class="r"><code>library("PerformanceAnalytics")
my_data <- mtcars[, c(1,3,4,5,6,7)]
chart.Correlation(my_data, histogram=TRUE, pch=19)</code></pre>
<div class="figure">
<img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-matrix-graph-1.png" alt="scatter plot, chart" width="528" style="margin-bottom:10px;" />
<p class="caption">
scatter plot, chart
</p>
</div>
<br>
<div class="block">
<p>In the above plot:</p>
<ul>
<li>The distribution of each variable is shown on the diagonal.</li>
<li>On the bottom of the diagonal : the bivariate scatter plots with a fitted line are displayed</li>
<li>On the top of the diagonal : the value of the correlation plus the significance level as stars</li>
<li>Each significance level is associated to a symbol : p-values(0, 0.001, 0.01, 0.05, 0.1, 1) <=> symbols(“***”, “**”, “*”, “.”, " “)</li>
</ul>
</div>
<p><br/></p>
</div>
<div id="use-heatmap" class="section level3">
<h3>Use heatmap()</h3>
<pre class="r"><code># Get some colors
col<- colorRampPalette(c("blue", "white", "red"))(20)
heatmap(x = res, col = col, symm = TRUE)</code></pre>
<div class="figure">
<img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-matrix-heatmap-1.png" alt="Heatmap of correlation matrix" width="384" style="margin-bottom:10px;" />
<p class="caption">
Heatmap of correlation matrix
</p>
</div>
<br/>
<div class="block">
<ul>
<li><strong>x</strong> : the correlation matrix to be plotted</li>
<li><strong>col</strong> : color palettes</li>
<li><strong>symm</strong> : logical indicating if x should be treated symmetrically; can only be true when x is a square matrix.</li>
</ul>
</div>
<p><br/></p>
</div>
</div>
</div>
<div id="online-software-to-analyze-and-visualize-a-correlation-matrix" class="section level1">
<h1>Online software to analyze and visualize a correlation matrix</h1>
<br/>
<div class="block">
<i class="fa - fa-cogs fa-4x valign_middle"></i> A web application for computing and visualizing a correlation matrix is available here without any installation : <a href="https://www.sthda.com/english/english/rsthda/correlation-matrix.php">online software for correlation matrix</a>.

</div>
<p><br/></p>
<p><a href="https://www.sthda.com/english/english/rsthda/correlation-matrix.php"><img src="https://www.sthda.com/english/sthda/RDoc/images/cloud-computing.jpg" title="Click to go to the online software", alt="online software for correlation matrix, R software"/></a></p>
<p><a href="http://www.freepik.com" rel="nofollow" style="font-size:9px;">(Designed by Freepik)</a></p>
<p><span class="success">Take me to the <a href="https://www.sthda.com/english/english/rsthda/correlation-matrix.php">correlation matrix calculator</a></span></p>
<p>The software can be used as follow :</p>
<ol style="list-style-type: decimal">
<li><strong>Go to the web application</strong> : <a href="https://www.sthda.com/english/english/rsthda/correlation-matrix.php">correlation matrix calculator</a></li>
<li><strong>Upload a .txt tab or a CSV file</strong> containing your data (columns are variables). The supported file formats are described <a href="https://www.sthda.com/english/english/wiki/rsthda">here</a>. You can use the demo data available on the calculator web page by clicking on the corresponding link.</li>
<li>After uploading, an <strong>overview of a part of your file</strong> is shown to check that the data are correctly imported. If the data are not correctly displayed, please make sure that the format of your file is OK <a href="https://www.sthda.com/english/english/wiki/rsthda">here</a>.</li>
<li><strong>Click on the ‘Analyze’ button</strong> and <strong>select at least 2 variables</strong> to calculate the correlation matrix. By default, all variables are selected. <strong>Please, deselect the columns containing texts</strong>. You can also <strong>select the correlation methods</strong> (Pearson, Spearman or Kendall). Default is the Pearson method.</li>
<li>Click the <strong>OK</strong> button</li>
<li>Results : the output of the software includes :
<ul>
<li>The correlation matrix</li>
<li>The visualization of the correlation matrix as a correlogram</li>
<li>A web link to export the results as .txt tab file</li>
</ul></li>
</ol>
<br/>
<div class="warning">
<p>Note that, you can specify the alternative hypothesis to use for the correlation test by clicking on the button “Advanced options”.</p>
<p>Choose one of the 3 options :</p>
<ul>
<li>Two-sided</li>
<li>Correlation < 0 for “less”</li>
<li>Correlation > 0 for “greater”</li>
</ul>
Default value is <strong>Two-sided</strong>.
</div>
<p><br/></p>
</div>
<div id="summarry" class="section level1">
<h1>Summarry</h1>
<br/>
<div class="block">
<ul>
<li>Use <strong>cor</strong>() function for simple <strong>correlation analysis</strong></li>
<li>Use <strong>rcorr</strong>() function from <strong>Hmisc</strong> package to compute <strong>matrix of correlation coefficients</strong> and <strong>matrix of p-values</strong> in single step.</li>
<li>Use <strong>symnum</strong>(), <strong>corrplot</strong>()[from <strong>corrplot</strong> package], <strong>chart.Correlation</strong>() [from <strong>PerformanceAnalytics</strong> package], or <strong>heatmap</strong>() functions to visualize a <strong>correlation matrix</strong>.</li>
</ul>
</div>
</div>
<div id="infos" class="section level1">
<h1>Infos</h1>
<p><span class="warning"> This analysis has been performed using <strong>R software</strong> (ver. 3.2.4). </span></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('#rdoc h1').addClass('wiki_paragraph1');
    jQuery('#rdoc h2').addClass('wiki_paragraph2');
    jQuery('#rdoc h3').addClass('wiki_paragraph3');
    jQuery('#rdoc h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->


<!-- END HTML -->]]></description>
			<pubDate>Tue, 27 Sep 2016 06:57:30 +0200</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Correlation coefficient calculator]]></title>
			<link>https://www.sthda.com/english/wiki/correlation-coefficient-calculator</link>
			<guid>https://www.sthda.com/english/wiki/correlation-coefficient-calculator</guid>
			<description><![CDATA[<!-- START HTML -->

  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">

<div id="TOC">
<ul>
<li><a href="#the-different-types-of-correlation-methods">The different types of correlation methods</a></li>
<li><a href="#pearson-correlation-coefficient-formula">Pearson correlation coefficient formula</a></li>
<li><a href="#calculate-the-correlation-coefficient-using-r-software">Calculate the correlation coefficient using R software</a></li>
<li><a href="#interpretation-of-the-correlation-coefficient">Interpretation of the correlation coefficient</a></li>
<li><a href="#online-correlation-coefficient-calculator">Online correlation coefficient calculator</a></li>
</ul>
</div>

<p><br/></p>
<p><strong>Correlation test</strong> is used to study the dependence between two or more variables. The goal of this article is to describe briefly the different correlation methods and to provide an online <strong>correlation coefficient calculator</strong>.</p>
<div id="the-different-types-of-correlation-methods" class="section level1">
<h1>The different types of correlation methods</h1>
<p>There are :</p>
<ul>
<li>the <strong>Pearson correlation</strong> method which is a parametric correlation test as it depends on the distribution of the data. This method measures the linear dependence between two variables.</li>
<li>the <strong>Kendall</strong> and <strong>Spearman</strong> rank-based correlation analysis (non-parametric methods). These two methods are recommended if the data do not come from a bivariate normal distribution.</li>
</ul>
<p><span class="success">Pearson correlation test is the most commonly used method to calculate the <strong>correlation coefficient</strong> between two variables. The formula is shown in the next section.</span></p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/images/correlation-calculator.png" alt="correlation coefficient calculator, R software" /></p>
<p><a href="http://www.freepik.com" rel="nofollow" style="font-size:9px;">(Designed by Freepik)</a></p>
</div>
<div id="pearson-correlation-coefficient-formula" class="section level1">
<h1>Pearson correlation coefficient formula</h1>
<p>The Pearson correlation coefficient between two variables, x and y, can be calculated using the formula below :</p>
<p><span class="math">\[ 
r = \frac{\sum{(x-m_x)(y-m_y)}}{\sqrt{\sum{(x-mx)^2}\sum{(y-my)^2}}}
\]</span></p>
<p><span class="math">\(m_x\)</span> and <span class="math">\(m_y\)</span> are the means of x and y variables.</p>
<p><span class="warning">The significance level of the correlation can be determined by reading the table of critical values for the degrees of freedom : <span class="math">\(df = n-2\)</span></span></p>
<p>Read more : <a href="https://www.sthda.com/english/english/wiki/correlation-formula">correlation formula</a></p>
</div>
<div id="calculate-the-correlation-coefficient-using-r-software" class="section level1">
<h1>Calculate the correlation coefficient using R software</h1>
<p>Correlation coefficient can be easily computed in R using the function <strong>cor()</strong> or <strong>cor.test()</strong>. The simplified formats are :</p>
<pre class="r"><code>cor(x, y, method = c("pearson", "kendall", "spearman"))

cor.test(x, y, method=c("pearson", "kendall", "spearman"))</code></pre>
<p>In the R code above x and y are two numeric vectors with the same length.</p>
<br/>
<div class="warning">
<p>The main difference between the two correlation functions is that :</p>
<ul>
<li>the function <strong>cor()</strong> returns only the <strong>correlation coefficient</strong></li>
<li>the function <strong>cor.test()</strong> returns both the <strong>correlation coefficient</strong> and the <strong>significance level</strong>(or p-value) of the correlation</li>
</ul>
</div>
<p><br/></p>
<p>These functions can be used as follow :</p>
<pre class="r"><code># Define two numeric vectors
x <- c(44.4, 45.9, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1)
y <- c( 2.6,  3.1,  2.5,  5.0,  3.6,  4.0,  5.2,  2.8,  3.8)

# Pearson correlation coefficient between x and y
cor(x, y)</code></pre>
<pre><code>[1] 0.5712</code></pre>
<pre class="r"><code># Pearson correlation test 
cor.test(x, y)</code></pre>
<pre><code>
    Pearson&amp;#39;s product-moment correlation

data:  x and y
t = 1.841, df = 7, p-value = 0.1082
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.1497  0.8956
sample estimates:
   cor 
0.5712 </code></pre>
<p><span class="success"> The <strong>correlation coefficient</strong> is 0.5712 and the <em>p-value</em> is 0.1082. </span></p>
<p>Read more : <a href="https://www.sthda.com/english/english/wiki/correlation-analysis">correlation analysis using R software</a></p>
<p>Spearman and Kendall non-parametric correlation methods can be used as follow :</p>
<pre class="r"><code># Spearman non-parametric correlation test
cor.test(x, y, method="spearman")

# Kendall non-parametric correlation test
cor.test(x, y, method="kendall")</code></pre>
</div>
<div id="interpretation-of-the-correlation-coefficient" class="section level1">
<h1>Interpretation of the correlation coefficient</h1>
<p><span class="success">The value of the correlation coefficient is comprised between <strong>-1</strong> and <strong>1</strong></span></p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-coefficient-calculator-plot1.png" title="correlation coefficient calculator, R software" alt="correlation coefficient calculator, R software" width="165.6" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-coefficient-calculator-plot2.png" title="correlation coefficient calculator, R software" alt="correlation coefficient calculator, R software" width="165.6" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-coefficient-calculator-plot3.png" title="correlation coefficient calculator, R software" alt="correlation coefficient calculator, R software" width="165.6" style="margin-bottom:10px;" /></p>
<ul>
<li><strong>-1</strong> corresponds to a strong <strong>negative correlation</strong> : this means that every time <strong>x increases</strong>, <strong>y decreases</strong> (left panel figure)</li>
<li><strong>0</strong> means that there is no <strong>association</strong> between the two variables (x and y) (middle panel figure)</li>
<li><strong>1</strong> corresponds to a strong <strong>positive correlation</strong> : this means that <strong>y increases</strong> with <strong>x</strong> (right panel figure)</li>
</ul>
</div>
<div id="online-correlation-coefficient-calculator" class="section level1">
<h1>Online correlation coefficient calculator</h1>
<p>A web application, for computing the different correlation coefficients, is available at this link : <a href="https://www.sthda.com/english/english/rsthda/correlation.php">correlation coefficient calculator</a>.</p>
<p>It can be used online without any installation to calculate Pearson, Kendall or Spearman correlation coefficient.</p>
<p><a href="https://www.sthda.com/english/english/rsthda/correlation.php"><img src="https://www.sthda.com/english/sthda/RDoc/images/cloud-computing.jpg" title="Click to go to the calculator!", alt="correlation coefficient calculator, R software"/></a></p>
<p><a href="http://www.freepik.com" rel="nofollow" style="font-size:9px;">(Designed by Freepik)</a></p>
<p><span class="success">Go to the <a href="https://www.sthda.com/english/english/rsthda/correlation.php">correlation coefficient calculator</a></span></p>
<p>The correlation coefficient calculator can be used as follow :</p>
<ol style="list-style-type: decimal">
<li><strong>Go to the application</strong> available at this link : <a href="https://www.sthda.com/english/english/rsthda/correlation.php">correlation coefficient calculator</a></li>
<li><strong>Copy and paste your data</strong> from Excel to the calculator. You can use the demo data available in the calculator web page by clicking on the link.</li>
<li><strong>Select the correlation methods</strong> (Pearson, Spearman or Kendall). Default is the Pearson method.</li>
<li>Click the <strong>OK</strong> button</li>
</ol>
<br/>
<div class="warning">
<p>Note that, you can specify the alternative hypothesis to use for the correlation test by clicking on the button “Advanced options”.</p>
<p>Choose one of the 3 options :</p>
<ul>
<li>Two-sided</li>
<li>Correlation < 0 for “less”</li>
<li>Correlation > 0 for “greater”</li>
</ul>
Default value is <strong>Two-sided</strong>.
</div>
<p><br/></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src  = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>

</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->

<!-- END HTML -->]]></description>
			<pubDate>Wed, 21 Jan 2015 16:46:47 +0100</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Correlation coefficient]]></title>
			<link>https://www.sthda.com/english/wiki/correlation-coefficient</link>
			<guid>https://www.sthda.com/english/wiki/correlation-coefficient</guid>
			<description><![CDATA[<!-- START HTML -->

  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">

<div id="TOC">
<ul>
<li><a href="#correlation-coefficient-definition">correlation coefficient definition</a></li>
<li><a href="#negative-and-positive-correlation">Negative and positive correlation</a></li>
<li><a href="#correlation-coefficient-calculator">Correlation coefficient calculator</a></li>
</ul>
</div>

<p><br/></p>
<div id="correlation-coefficient-definition" class="section level1">
<h1>correlation coefficient definition</h1>
<p><strong>Correlation coefficient</strong> is a quantity that measures the strength of the <strong>association</strong> (or <strong>dependence</strong>) between two variables (x and y). For example if we are interested to know whether there is a <strong>relationship</strong> between the heights of fathers and son, a <strong>correlation coefficient</strong> can be calculated.</p>
<p>There are different correlation coefficients :</p>
<ul>
<li><strong>Pearson r</strong> : linear dependence</li>
<li><strong>Kendall tau</strong> : rank-based correlation coefficient</li>
<li><strong>Spearman rho</strong> : rank-base correlation coefficient</li>
</ul>
<p><span class="warning"><a href="https://www.sthda.com/english/english/wiki/correlation-formula"><strong>Pearson correlation</strong></a> is the most used one.</span></p>
<p><strong>correlation formula</strong> is described <a href="https://www.sthda.com/english/english/wiki/correlation-formula">here</a>.</p>
</div>
<div id="negative-and-positive-correlation" class="section level1">
<h1>Negative and positive correlation</h1>
<p>The value of <strong>correlation coefficient</strong> can be <strong>negative </strong>or <strong>positive</strong> and it is comprised between <strong>-1</strong> and <strong>1</strong> (see the plots below).</p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-coefficient-plot1.png" title="correlation coefficient -1" alt="correlation coefficient -1" width="165.6" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-coefficient-plot2.png" title="correlation coefficient -1" alt="correlation coefficient -1" width="165.6" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-coefficient-plot3.png" title="correlation coefficient -1" alt="correlation coefficient -1" width="165.6" style="margin-bottom:10px;" /></p>
<ul>
<li><strong>-1</strong> means strong <strong>negative correlation</strong> : In this case y decreases when x increases (left panel figure)</li>
<li><strong>0</strong> means that there is no <strong>relationship</strong> between the two variables (x and y) (middle panel figure)</li>
<li><strong>1</strong> means strong <strong>positive correlation</strong> : In this case y increases when y increases (right panel figure)</li>
</ul>
</div>
<div id="correlation-coefficient-calculator" class="section level1">
<h1>Correlation coefficient calculator</h1>
<div class="block">
A <a href="https://www.sthda.com/english/english/rsthda/correlation.php"><strong>correlation coefficient calculator</strong></a> can be found <a href="https://www.sthda.com/english/english/rsthda/correlation.php">here</a>.
</div>
<p><br/></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->
<!--====================== stop here when you copy to sthda================-->

<!-- END HTML -->]]></description>
			<pubDate>Tue, 18 Nov 2014 21:47:51 +0100</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Correlation coefficient calculator : the top 3 you should know]]></title>
			<link>https://www.sthda.com/english/wiki/correlation-coefficient-calculator-the-top-3-you-should-know</link>
			<guid>https://www.sthda.com/english/wiki/correlation-coefficient-calculator-the-top-3-you-should-know</guid>
			<description><![CDATA[<!-- START HTML -->
<style>#whatis img{background-color:#fff;background-color:#FFF;border-radius:3px;border:1px solid #CCC;box-shadow:2px 2px 12px -5px #999;margin:0 5px;margin-bottom:5px;padding:5px;text-align:center}</style>
<!-- END HTML --><br />
<br />
<!-- START HTML -->
<div id="whatis">
<!-- END HTML --><br />
<span style="font-size: 20px;"><span style="color:#0000FF;">The top 3 online <strong>correlation coefficient calculator</strong></span></span><br />
<br />
<br />
<br />
<h2 class="formatter-title">1. RSTHDA correlation coefficient calculator</h2><br />
<br />
<a href="https://www.sthda.com/english/english/rsthda/correlation.php"><img src="https://www.sthda.com/english/upload/rsthda_correlation_coefficient_calculator.png" alt="" /></a><br />
<br />
<span class="success">It can be used to compute <strong>pearson, spearman and kendall </strong>correlation coefficients.</span><br />
<a href="https://www.sthda.com/english/english/rsthda/correlation.php">Use it here!</a><br />
<br />
 <br />
<br />
<h2 class="formatter-title">2. Correlan calculator from Social Science Statistics </h2><br />
   <br />
<span class="success">It computes only pearson correlation coefficient.</span><br />
<a href="http://www.socscistatistics.com/tests/pearson/Default2.aspx">Use it here</a><br />
<br />
<!-- START HTML -->
</div>
<!-- END HTML --><br />
<br />
<h2 class="formatter-title">3. Calculator from easycalculation.com</h2><br />
<br />
<a href="https://www.easycalculation.com/statistics/correlation.php"><img src="https://www.sthda.com/english/upload/easycalculation.png" alt="" /></a><br />
<a href="https://www.easycalculation.com/statistics/correlation.php">Demo</a>]]></description>
			<pubDate>Tue, 18 Nov 2014 08:11:39 +0100</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[correlation formula]]></title>
			<link>https://www.sthda.com/english/wiki/correlation-formula</link>
			<guid>https://www.sthda.com/english/wiki/correlation-formula</guid>
			<description><![CDATA[<!-- START HTML -->

  <!--====================== start from here when you copy to sthda================-->  
  <div id="rdoc">


<div id="TOC">
<ul>
<li><a href="#define-correlation">Define correlation</a></li>
<li><a href="#pearson-correlation">Pearson correlation</a></li>
<li><a href="#interpretation-of-correlation-coefficient">Interpretation of correlation coefficient</a></li>
<li><a href="#correlation-coefficient-calculator">Correlation coefficient calculator</a></li>
</ul>
</div>

<p><br/></p>
<div id="define-correlation" class="section level1">
<h1>Define correlation</h1>
<p><strong>Correlation</strong> is very helpful to investigate the <strong>dependence</strong> between two or more variables. As an example we are interested to know whether there is an association between the weights of fathers and son. <strong>correlation coefficient</strong> can be calculated to answer this question.</p>
<p>If there is no relationship between the two variables (father and son weights), the average weight of son should be the same regardless of the weight of the fathers and vice versa.</p>
<p>There are different methods to perform <strong>correlation analysis</strong> : <strong>Pearson</strong>, <strong>Kendall</strong> and <strong>Spearman</strong> correlation tests.</p>
<p><span class="warning">The most commonly used is <strong>Pearson correlation</strong>. The aim of this article is to describe Pearson <strong>correlation formula</strong>.</span></p>
</div>
<div id="pearson-correlation" class="section level1">
<h1>Pearson correlation</h1>
<p><strong>Pearson correlation</strong> measures a linear dependence between two variables (x and y). It’s also known as a <strong>parametric correlation</strong> test because it depends to the distribution of the data. The plot of y = f(x) is named <strong>linear regression</strong> curve.</p>
<p>The <strong>pearson correlation</strong> formula is :</p>
<p><span class="math">\[ 
r = \frac{\sum{(x-m_x)(y-m_y)}}{\sqrt{\sum{(x-mx)^2}\sum{(y-my)^2}}}
\]</span></p>
<p><span class="math">\(m_x\)</span> and <span class="math">\(m_y\)</span> are the means of x and y variables.</p>
<p>the p-value (significance level) of the correlation can be determined :</p>
<ol style="list-style-type: decimal">
<li><p>by using the correlation coefficient table for the degrees of freedom : <span class="math">\(df = n-2\)</span></p></li>
<li><p>or by calculating the <strong>t value</strong> :
<span class="math">\[
t=\frac{r}{\sqrt{1-r^2}}\sqrt{n-2}
\]</span></p></li>
</ol>
<p>In this case the corresponding p-value is determined using <a href="https://www.sthda.com/english/english/wiki/t-distribution-table"><strong>t distribution table</strong></a> for <span class="math">\(df = n-2\)</span></p>
<p><span class="warning">If the p-value is less than 5%, then the correlation is significant.</span></p>
</div>
<div id="interpretation-of-correlation-coefficient" class="section level1">
<h1>Interpretation of correlation coefficient</h1>
<p><span class="success"> Correlation coefficient is between -1 (strong <strong>negative correlation</strong>) and 1 (strong <strong>positive correlation</strong>) </span></p>
<p><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-formula-plot1.png" title="plot of chunk plot" alt="plot of chunk plot" width="165.6" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-formula-plot2.png" title="plot of chunk plot" alt="plot of chunk plot" width="165.6" style="margin-bottom:10px;" /><img src="https://www.sthda.com/english/sthda/RDoc/figure/statistics/correlation-formula-plot3.png" title="plot of chunk plot" alt="plot of chunk plot" width="165.6" style="margin-bottom:10px;" /></p>
</div>
<div id="correlation-coefficient-calculator" class="section level1">
<h1>Correlation coefficient calculator</h1>
<div class="block">
Note that online <strong>correlation coefficient calculator</strong> is also available by following this links : <a href="https://www.sthda.com/english/english/rsthda/correlation.php"><strong>Correlation coefficient calculator</strong></a>.
</div>
<p><br/></p>
</div>

<script>jQuery(document).ready(function () {
    jQuery('h1').addClass('wiki_paragraph1');
    jQuery('h2').addClass('wiki_paragraph2');
    jQuery('h3').addClass('wiki_paragraph3');
    jQuery('h4').addClass('wiki_paragraph4');
    });//add phpboost class to header</script>
<style>.content{padding:0px;}</style>
</div><!--end rdoc-->


<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src  = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>
<!--====================== stop here when you copy to sthda================-->

<!-- END HTML -->]]></description>
			<pubDate>Tue, 18 Nov 2014 07:15:23 +0100</pubDate>
			
		</item>
		
	</channel>
</rss>
