<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/an-r-tutorial-visual-representation-of-complex-multivariate-relationships-using-the-r-qgraph-package-part-two-repost/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://dmwiig.net/2018/05/07/an-r-tutorial-visual-representation-of-complex-multivariate-relationships-using-the-r-qgraph-package-part-two-repost/"> 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>This is a repost of the original article that was posted as an embedded PDF file.</p>
<p> </p>
<h5>Douglas M. Wiig<br>
April 8, 2018<br>
Abstract</h5>
<h5>
This article is part of my series of articles exploring the use of R<br>
packages that allow for visualization of complex relationships among variables.<br>
Other articles have examined visual representations produced by<br>
the <em>qgraph</em> package in both large and small samples with more than three<br>
variables.  In this article I look specifically at the R <em>qgraph</em> package with a small<br>
dataset of N=10, but a large number (14) of variables. Specifically, the R<br>
<em>qgraph.pca</em> function is examined.</h5>
<p><strong>1 The Problem</strong></p>
<p>In two previous blog posts I discussed some techniques for visualizing relationships<br>
involving two or three variables and a large number of cases. In this<br>
tutorial I will extend that discussion to show some techniques that can be used<br>
on datasets with complex multivariate relationships involving three or more<br>
variables.<br>
In this post I will use a dataset called ‘Detroit.’ This data set was originally<br>
used in the book <em>‘Subset selection in regression’</em> by Alan J. Miller published in<br>
the Chapman and Hall series of monographs on <em>Statistics and Applied Probability,</em><br>
<em>no. 40.</em> It was also used in other research and appeared in appendix A<br>
of <em>‘Regression analysis and its application: A data-oriented approach’</em> by Gunst<br>
and Mason, <em>Statistics textbooks and monographs no. 24,</em> Marcel Dekker. Editor.<br>
The Detroit dataset contains 14 variables and 10 cases. Each case represents<br>
a year during the time period 1961-1973. The variables on which data was<br>
collected are seen as possible predictors of homicide rate in Detroit during each<br>
of the years studied.<br>
These data are shown below</p>
<p>FTP UEMP MAN LIC GR CLEAR WM NMAN GOV HE WE HOM ACC ASR<br>
260.35 11.0 455.5 178.15 215.98 93.4 558724. 538.1 133.9 2.98 117.18 8.60 9.17 306.18<br>
269.80 7.0 480.2 156.41 180.48 88.5 538584. 547.6 137.6 3.09 134.02 8.90 40.27 315.16<br>
272.04 5.2 506.1 198.02 209.57 94.4 519171. 562.8 143.6 3.23 141.68 8.52 45.31 277.53<br>
272.96 4.3 535.8 222.10 231.67 92.0 500457. 591.0 150.3 3.33 147.98 8.89 49.51 234.07<br>
272.51 3.5 576.0 301.92 297.65 91.0 482418. 626.1 164.3 3.46 159.85 13.0 55.05 30.84<br>
261.34 3.2 601.7 391.22 367.62 87.4 465029. 659.8 179.5 3.60 157.19 14.57 53.90 17.99<br>
268.89 4.1 577.3 665.56 616.54 88.3 448267. 686.2 187.5 3.73 155.29 21.36 50.62 86.11<br>
295.99 3.9 596.9 1131.21 1029.75 86.1 432109. 699.6 195.4 2.91 131.75 28.03 51.47 91.59<br>
319.87 3.6 613.5 837.60 786.23 79.0 416533. 729.9 210.3 4.25 178.74 31.49 49.16 20.39<br>
341.43 7.1 569.3 794.90 713.77 73.9 401518. 757.8 223.8 4.47 178.30 37.39 45.80 23.03</p>
<p>The variables are as follows:<br>
FTP – Full-time police per 100,000 population<br>
UEMP – % unemployed in the population<br>
MAN – number of manufacturing workers in thousands<br>
LIC – Number of handgun licenses per 100,000 population<br>
GR – Number of handgun registrations per 100,000 population<br>
CLEAR – % homicides cleared by arrests<br>
WM – Number of white males in the population<br>
NMAN – Number of non-manufacturing workers in thousands<br>
GOV – Number of government workers in thousands<br>
HE – Average hourly earnings<br>
WE – Average weekly earnings<br>
HOM – Number of homicides per 100,000 of population<br>
ACC – Death rate in accidents per 100,000 population<br>
ASR – Number of assaults per 100,000 population<br>
<em>[J.C. Fisher ”Homicide in Detroit: The Role of Firearms”, Criminology, vol.14,</em><br>
<em>387-400 (1976)]</em></p>
<p><strong>2 Analysis</strong><br>
As I have noted in previous tutorials, social science research projects often start<br>
out with many potential independent predictor variables for a given dependent<br>
variable. If these are all measured at the interval or ratio level, a correlation<br>
matrix often serves as a starting point to begin analyzing relationships among<br>
variables. In this particular case a researcher might be interested in looking at<br>
factors that are related to total homicides. There are many R techniques to<br>
enter data for analysis. In this case I entered the data into an Excel spreadsheet<br>
and then loaded the file into the R environment. Install and load the following<br>
packages:<br>
<em>Hmisc</em><br>
<em>stats</em><br>
<em>qgraph</em><br>
<em>readxl</em> (only needed if importing data from Excel)</p>
<p>A correlation matrix can be generated using the cor function which is contained<br>
in the stats package. To produce a matrix using all 14 variables use the<br>
following code:<br>
<strong>#the data file has been loaded as ’detroit’</strong><br>
<strong>#the file has 14 columns</strong><br>
<strong>#run a pearson correlation and #run a pearson correlation and put into the object ’detcor’</strong><br>
<strong>detcor=cor(as.matrix(detroit[c(1:14)]), method=”pearson”)</strong><br>
<strong>#</strong><br>
<strong>#round the correlation matrix to 2 decimal places for better viewing</strong><br>
<strong>round(detcor, 2)</strong><br>
<strong>#</strong><br>
<strong>#The resulting matrix will be displayed on the screen</strong></p>
<p>Examination of the matrix shows a number of the predictors correlate with the<br>
dependent variable ’HOM.’ There are also a large number of inter-correlations<br>
among the predictor variables. This fact makes it difficult to make any generalizations<br>
based on the correlation matrix only. As demonstrated in previous<br>
tutorials, the qgraph function can be used to produce a visual representation of<br>
the correlation matrix. Use the following code:</p>
<p><strong>#basic graph with 14 vars zero order correlations</strong><br>
<strong>qgraph(detcor, shape=”circle”, posCol=”darkgreen”, negCol=”darkred”, layout=”spring”)</strong></p>
<p>This will produce graph as seen below:</p>
<p><img data-orig-file="https://dmwiig.files.wordpress.com/2018/04/qgraphplot1.png?w=456" data-orig-size="540,324" data-medium-file="https://dmwiig.files.wordpress.com/2018/04/qgraphplot1.png?w=456?w=300" data-attachment-id="359" data-image-meta="{" data-image-description="" src="https://dmwiig.files.wordpress.com/2018/04/qgraphplot1.png?w=456" data-permalink="https://dmwiig.net/2018/04/08/an-r-tutorial-visual-representation-of-complex-multivariate-relationships-using-the-r-qgraph-package-part-two/qgraphplot1/" data-large-file="https://dmwiig.files.wordpress.com/2018/04/qgraphplot1.png?w=456?w=474" alt="qgraphplot1" data-image-title="qgraphplot1" sizes="(max-width: 474px) 100vw, 474px" data-comments-opened="1" class="wp-image-359" data-recalc-dims="1"></p>
<p>The graph displays positive correlations among variable as a green line, and<br>
negative as a red line. The color intensity indicates the relative strength of the<br>
correlation. While this approach provides an improvement over the raw matrix<br>
it still rather difficult to interpret. There are many options other than those<br>
used in the above example that allow qgraph to have a great deal of flexibility in<br>
creating visual representation of complex relationships among variables. In the<br>
next section I will examine one of these options that uses principal component<br>
analysis of the data.<br>
<strong>2.1 Using <em>qgraph</em> Principal Component Analysis</strong><br>
A discussion of the theory behind principal component exploratory analysis is<br>
beyond the scope of this discussion. Suffice it to say that it allows for simplification<br>
of a large number of inter-correlations by identifying factors or dimensions<br>
that individual correlations relate to. This grouping of variables on specific factors<br>
allows qgraph to create a visual representation of these relationships. An<br>
excellent discussion of the theory of PCA along with R scripts can be found in<br>
<em>Principal Components Analysis (PCA)</em>, Steven M. Holland Department of Geology,<br>
University of Georgia, Athens, GA, 2008.<br>
To produce a graph using the ’detcor’ correlation matrix used above use the<br>
following code:</p>
<p><strong>#correlation matrix used is ’detcor’</strong><br>
<strong>#qgraph with loadings from principal components</strong><br>
<strong>#basic options used; many other options available</strong><br>
<strong>qgraph.pca(detcor, factor=3, rotation=”varimax”)</strong><br>
<strong>#this will yield 3 factors</strong><br>
This code produces the output shown below:</p>
<p><img data-orig-file="https://dmwiig.files.wordpress.com/2018/04/pcaplot.png?w=456" data-orig-size="540,324" data-medium-file="https://dmwiig.files.wordpress.com/2018/04/pcaplot.png?w=456?w=300" data-attachment-id="360" data-image-meta="{" data-image-description="" src="https://dmwiig.files.wordpress.com/2018/04/pcaplot.png?w=456" data-permalink="https://dmwiig.net/2018/04/08/an-r-tutorial-visual-representation-of-complex-multivariate-relationships-using-the-r-qgraph-package-part-two/pcaplot/" data-large-file="https://dmwiig.files.wordpress.com/2018/04/pcaplot.png?w=456?w=474" alt="pcaplot" data-image-title="pcaplot" sizes="(max-width: 474px) 100vw, 474px" data-comments-opened="1" class="wp-image-360" data-recalc-dims="1"></p>
<p>As noted above the red and green arrows indicate negative and positive loadings<br>
on the factors, and the color intensity indicates the strength. The qgraph.pca<br>
function produces a useful visual interpretation of the clustering of variables relative<br>
to the three factors extracted. This would be very difficult if not impossible<br>
with only the correlation matrix or the basic qgraph visual representation.<br>
In a future tutorial I will explore more qgraph options that can be used to<br>
explore the Detroit dataset as well as options for a larger datasets. In future<br>
articles I will also explore other R packages that are also useful for analyzing<br>
large numbers of complex variable interrelationships in very large, medium, and<br>
small samples.<br>
** When developing R code I strongly recommend using an IDE such as<br>
RStudio. This is a powerful coding environment and is free for personal use as<br>
well as being open source software. RStudio will run on a variety of platforms.<br>
If you are developing code for future publication or sharing I would also recommend<br>
TeXstudio, a LaTex based document development environment which is also free for personal use. This document was produced using TeXstudio 2.12.6<br>
and RStudio 1.0.136.</p>
<p> </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/2018/05/07/an-r-tutorial-visual-representation-of-complex-multivariate-relationships-using-the-r-qgraph-package-part-two-repost/"> 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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/6BB2wSqkdEw" height="1"></a></div>