<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/the-r-qgraph-package-using-r-to-visualize-complex-relationships-among-variables-in-a-large-dataset-part-one/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://dmwiig.net/2017/03/10/the-r-qgraph-package-using-r-to-visualize-complex-relationships-among-variables-in-a-large-dataset-part-one/"> r – R Statistics and Programming</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The R <em>qgraph</em> Package: Using R to Visualize Complex Relationships Among Variables in a Large Dataset, Part One</p>
<p><u>A Tutorial by D. M. Wiig, Professor of Political Science, Grand View University</u></p>
<p>In my most recent tutorials I have discussed the use of the <em>tabplot()</em> package to visualize multivariate mixed data types in large datasets. This type of table display is a handy way to identify possible relationships among variables, but is limited in terms of interpretation and the number of variables that can be meaningfully displayed.</p>
<p>Social science research projects often start out with many potential independent predictor variables for a given dependant variable. If these variables are all measured at the interval or ratio level a correlation matrix often serves as a starting point to begin analyzing relationships among variables.</p>
<p>In this tutorial I will use the R packages <em>SemiPar, qgraph </em>and <em>Hmisc </em>in addition to the basic packages loaded when R is started. The code is as follows:</p>
<p><strong>###################################################</strong><br>
<strong> #data from package SemiPar; dataset milan.mort</strong><br>
<strong> #dataset has 3652 cases and 9 vars</strong><br>
<strong> ##################################################</strong><br>
<strong> install.packages(“SemiPar”)</strong><br>
<strong> install.packages(“Hmisc”)</strong><br>
<strong> install.packages(“qgraph”)</strong><br>
<strong> library(SemiPar)</strong><br>
<strong> ####################################################</strong></p>
<p>One of the datasets contained in the <em>SemiPar</em> packages is milan.mort. This dataset contains nine variables and data from 3652 consecutive days for the city of Milan, Italy. The nine variables in the dataset are as follows:</p>
<p>rel.humid (relative humidity)<br>
tot.mort (total number of deaths)<br>
resp.mort (total number of respiratory deaths)<br>
SO2 (measure of sulphur dioxide level in ambient air)<br>
TSP (total suspended particles in ambient air)<br>
day.num (number of days since 31st December, 1979)<br>
day.of.week (1=Monday; 2=Tuesday; 3=Wednesday; 4=Thursday; 5=Friday; 6=Saturday; 7=Sunday<br>
holiday (indicator of public holiday: 1=public holiday, 0=otherwise<br>
mean.temp (mean daily temperature in degrees celsius)</p>
<p>To look at the structure of the dataset use the following</p>
<p><strong>#########################################</strong><br>
<strong> library(SemiPar)</strong><br>
<strong> data(milan.mort)</strong><br>
<strong> str(milan.mort)</strong><br>
<strong> ###############################################</strong></p>
<p>Resulting in the output:</p>
<p><strong>> str(milan.mort)</strong><br>
<strong> ‘data.frame’: 3652 obs. of 9 variables:</strong><br>
<strong> $ day.num : int 1 2 3 4 5 6 7 8 9 10 …</strong><br>
<strong> $ day.of.week: int 2 3 4 5 6 7 1 2 3 4 …</strong><br>
<strong> $ holiday : int 1 0 0 0 0 0 0 0 0 0 …</strong><br>
<strong> $ mean.temp : num 5.6 4.1 4.6 2.9 2.2 0.7 -0.6 -0.5 0.2 1.7 …</strong><br>
<strong> $ rel.humid : num 30 26 29.7 32.7 71.3 80.7 82 82.7 79.3 69.3 …</strong><br>
<strong> $ tot.mort : num 45 32 37 33 36 45 46 38 29 39 …</strong><br>
<strong> $ resp.mort : int 2 5 0 1 1 6 2 4 1 4 …</strong><br>
<strong> $ SO2 : num 267 375 276 440 354 …</strong><br>
<strong> $ TSP : num 110 153 162 198 235 …</strong></p>
<p>As is seen above, the dataset contains 9 variables all measured at the ratio level and 3652 cases.</p>
<p>In doing exploratory research a correlation matrix is often generated as a first attempt to look at inter-relationships among the variables in the dataset. In this particular case a researcher might be interested in looking at factors that are related to total mortality as well as respiratory mortality rates.</p>
<p>A correlation matrix can be generated using the cor function which is contained in the stats package. There are a variety of functions for various types of correlation analysis. The cor function provides a fast method to calculate Pearson’s r with a large dataset such as the one used in this example.</p>
<p>To generate a zero order Pearson’s correlation  matrix use the following:</p>
<p><strong>###############################################<br>
#round the corr output to 2 decimal places<br>
#put output into variable cormatround<br>
#coerce data to matrix</strong><br>
<strong>#########################################<br>
library(Hmisc)<br>
cormatround round(cormatround, 2)<br>
#################################################</strong></p>
<p>The output is:</p>
<table width="450" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td align="left">
<pre><span>> </span><span>cormatround <span>> </span><span>round(cormatround, 2)
</span>Day.num day.of.week holiday mean.temp rel.humid tot.mort resp.mort SO2 TSP
day.num 1.00 0.00 0.01 0.02 0.12 -0.28 0.22 -0.34 0.07
day.of.week 0.00 1.00 0.00 0.00 0.00 -0.05 0.03 -0.05 -0.05
holiday 0.01 0.00 1.00 -0.07 0.01 0.00 0.01 0.00 -0.01
mean.temp 0.02 0.00 -0.07 1.00 -0.25 -0.43 -0.26 -0.66 -0.44
rel.humid 0.12 0.00 0.01 -0.25 1.00 0.01 -0.03 0.15 0.17
tot.mort -0.28 -0.05 0.00 -0.43 0.01 1.00 0.47 0.44 0.25
resp.mort -0.22 -0.03 -0.01 -0.26 -0.03 0.47 1.00 0.32 0.15
SO2 -0.34 -0.05 0.00 -0.66 0.15 0.44 0.32 1.00 0.63
TSP 0.07 -0.05 -0.01 -0.44 0.17 0.25 0.15 0.63 1.00
</span></pre>
</td>
</tr>
<tr>
<td align="left"></td>
</tr>
<tr>
<td align="left">
<table cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td height align="left" width="1" rowspan="1">
<div>></div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<p>The matrix can be examined to look at intercorrelations among the nine variables, but it is very difficult to detect patterns of correlations within the matrix.  Also, when using the cor() function raw Pearson’s coefficients are reported, but significance levels are not.</p>
<p>A correlation matrix with significance can be generated by using the <em>rcorr()</em> function, also found in the <em>Hmisc</em> package. The code is:</p>
<p><strong>#############################################</strong><br>
<strong> library(Hmisc)</strong><br>
<strong> rcorr(as.matrix(milan.mort, type=”pearson”))</strong><br>
<strong> ###################################################</strong></p>
<p>The output is:</p>
<table cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td align="left">
<pre><span>> </span><span>rcorr(as.matrix(milan.mort, type=&quot;pearson&quot;))
</span>           day.num day.of.week holiday mean.temp rel.humid tot.mort resp.mort   SO2   TSP
day.num       1.00       0.00    0.01      0.02      0.12    -0.28  -0.22 -0.34  0.07
day.of.week   0.00        1.00    0.00      0.00      0.00    -0.05 -0.03 -0.05 -0.05
holiday       0.01        0.00    1.00     -0.07      0.01     0.00 -0.01  0.00 -0.01
mean.temp     0.02        0.00   -0.07      1.00     -0.25    -0.43 -0.26 -0.66 -0.44
rel.humid     0.12        0.00    0.01     -0.25      1.00     0.01 -0.03  0.15  0.17
tot.mort     -0.28       -0.05    0.00     -0.43      0.01     1.00  0.47  0.44  0.25
resp.mort    -0.22       -0.03   -0.01     -0.26     -0.03     0.47  1.00  0.32  0.15
SO2          -0.34       -0.05    0.00     -0.66      0.15     0.44  0.32  1.00  0.63
TSP           0.07       -0.05   -0.01     -0.44      0.17     0.25  0.15  0.63  1.00

n= 3652 


P
            day.num day.of.week holiday mean.temp rel.humid tot.mort resp.mort SO2    TSP   
day.num             0.9771     0.5349   0.2220    0.0000    0.0000  0.0000  0.0000
day.of.week 0.9771              0.7632  0.8727    0.8670    0.0045  0.1175   0.0061
holiday     0.5349  0.7632              0.0000    0.4648    0.8506  0.6115    0.7793 0.4108
mean.temp   0.2220  0.8727      0.0000            0.0000    0.0000  0.0000    0.0000 0.0000
rel.humid   0.0000  0.8670      0.4648  0.0000              0.3661  0.1096    0.0000 0.0000
tot.mort    0.0000  0.0045      0.8506  0.0000    0.3661            0.0000    0.0000 0.0000
resp.mort   0.0000  0.1175      0.6115  0.0000    0.1096    0.0000            0.0000 0.0000
SO2         0.0000  0.0024      0.7793  0.0000    0.0000    0.0000  0.0000           0.0000
TSP         0.0000  0.0061      0.4108  0.0000    0.0000    0.0000  0.0000    0.0000       
</pre>
</td>
</tr>
<tr>
<td align="left"></td>
</tr>
<tr>
<td align="left">
<table cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td height align="left" width="1" rowspan="1">
<div>></div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<p>In a future tutorial I will discuss using significance levels and correlation strengths as methods of reducing complexity in very large correlation network structures.</p>
<p>The recently released package <em>qgraph ()</em> provides a number of interesting functions that are useful in visualizing complex inter-relationships among a large number of variables. To quote from the CRAN documentation file<em> qraph()</em> “Can be used to visualize data networks as well as provides an interface for visualizing weighted graphical models.” (see CRAN documentation for ‘qgraph” version 1.4.2. See also <a target="_blank" rel="nofollow" href="http://sachaepskamp.com/qgraph">http://sachaepskamp.com/qgraph</a>).</p>
<p>The <em>qgraph()</em> function has a variety of options that can be used to produce specific types of graphical representations. In this first tutorial segment I will use the milan.mort dataset and the most basic <em>qgraph</em> functions to produce a visual graphic network of intercorrelations among the 9 variables in the dataset.</p>
<p>The code is as follows:</p>
<p><strong>###################################################</strong><br>
<strong> library(qgraph)</strong><br>
<strong> #use cor function to create a correlation matrix with milan.mort dataset</strong><br>
<strong> #and put into cormat variable</strong><br>
<strong> ###################################################</strong><br>
<strong> cormat=cor(milan.mort)  #correlation matrix generated</strong><br>
<strong> ###################################################</strong><br>
<strong> ###################################################</strong><br>
<strong> #now plot a graph of the correlation matrix</strong><br>
<strong> ###################################################</strong><br>
<strong> qgraph(cormat, shape=”circle”, posCol=”darkgreen”, negCol=”darkred”, layout=”groups”, vsize=10)</strong><br>
<strong> ###################################################</strong></p>
<p>This code produces the following correlation network:</p>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/dmwiig.files.wordpress.com/2017/03/milan-mortintercormodel.png?ssl=1"><img sizes="(max-width: 391px) 100vw, 391px" data-image-description="" data-orig-file="https://dmwiig.files.wordpress.com/2017/03/milan-mortintercormodel.png" data-orig-size="567,357" data-image-title="milan.mortintercormodel" class="wp-image-302" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="302" data-permalink="https://dmwiig.net/2017/03/10/the-r-qgraph-package-using-r-to-visualize-complex-relationships-among-variables-in-a-large-dataset-part-one/milan-mortintercormodel/" alt="" data-medium-file="https://i1.wp.com/dmwiig.files.wordpress.com/2017/03/milan-mortintercormodel.png?resize=391%2C247&amp;ssl=1" data-large-file="https://dmwiig.files.wordpress.com/2017/03/milan-mortintercormodel.png?w=474" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i1.wp.com/dmwiig.files.wordpress.com/2017/03/milan-mortintercormodel.png?resize=391%2C247&ssl=1"></a></p>
<p>The correlation network provides a very useful visual picture of the intercorrelations as well as positive and negative correlations. The relative thickness and color density of the bands indicates strength of Pearson’s r and the color of each band indicates a positive or negative correlation – red for negative and green for positive.</p>
<p>By changing the “layout=” option from “groups” to “spring” a slightly different perspective can be seen. The code is:</p>
<p><strong>########################################################</strong><br>
<strong> #Code to produce alternative correlation network:</strong><br>
<strong> #######################################################</strong><br>
<strong> library(qgraph)</strong><br>
<strong> #use cor function to create a correlation matrix with milan.mort dataset</strong><br>
<strong> #and put into cormat variable</strong><br>
<strong> ##############################################################</strong><br>
<strong> cormat=cor(milan.mort) #correlation matrix generated</strong><br>
<strong> ##############################################################</strong><br>
<strong> ###############################################################</strong><br>
<strong> #now plot a circle graph of the correlation matrix</strong><br>
<strong> ##########################################################</strong><br>
<strong> qgraph(cormat, shape=”circle”, posCol=”darkgreen”, negCol=”darkred”, layout=”spring”, vsize=10)</strong><br>
<strong> ###############################################################</strong></p>
<p>The graph produced is below:</p>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/dmwiig.files.wordpress.com/2017/03/milanmortspring.png?ssl=1"><img sizes="(max-width: 300px) 100vw, 300px" data-image-description="" data-orig-file="https://dmwiig.files.wordpress.com/2017/03/milanmortspring.png" data-orig-size="567,357" data-image-title="milanmortspring" data-comments-opened="1" class="wp-image-303" data-recalc-dims="1" data-attachment-id="303" data-permalink="https://dmwiig.net/2017/03/10/the-r-qgraph-package-using-r-to-visualize-complex-relationships-among-variables-in-a-large-dataset-part-one/milanmortspring/" alt="" data-medium-file="https://i1.wp.com/dmwiig.files.wordpress.com/2017/03/milanmortspring.png?resize=300%2C189&amp;ssl=1" data-large-file="https://dmwiig.files.wordpress.com/2017/03/milanmortspring.png?w=474" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i1.wp.com/dmwiig.files.wordpress.com/2017/03/milanmortspring.png?resize=300%2C189&ssl=1"></a></p>
<p>Once again the intercorrelations, strength of r and positive and negative correlations can be easily identified. There are many more options, types of graph and procedures for analysis that can be accomplished with the <em>qgraph()</em> package. In future tutorials I will discuss some of these.</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dmwiig.wordpress.com/300/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/dmwiig.wordpress.com/300/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://dmwiig.net/2017/03/10/the-r-qgraph-package-using-r-to-visualize-complex-relationships-among-variables-in-a-large-dataset-part-one/"> r – R Statistics and Programming</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/IjecBdJFIv4"></a></div>