<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/r-tutorial-visualizing-multivariate-relationships-in-large-datasets/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://dmwiig.net/2017/02/06/r-tutorial-visualizing-multivariate-relationships-in-large-datasets/"> 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>R Tutorial: Visualizing multivariate relationships in Large Datasets</p>
<p>A tutorial by D.M. Wiig</p>
<p>In two previous blog posts I discussed some techniques for visualizing relationships involving two or three variables and a large number of cases. In this tutorial I will extend that discussion to show some techniques that can be used on large datasets and complex multivariate relationships involving three or more variables.</p>
<p>In this tutorial I will use the R package <em>nmle</em> which contains the dataset <em>MathAchieve.</em> Use the code below to install the package and load it into the R environment:</p>
<p><strong>####################################################</strong><br>
<strong>#code for visual large dataset MathAchieve</strong><br>
<strong>#first show 3d scatterplot; then show tableplot variations</strong><br>
<strong>####################################################</strong><br>
<strong>install.packages(“nmle”) #install nmle package</strong><br>
<strong>library(nlme) #load the package into the R environment</strong><br>
<strong>####################################################</strong></p>
<p>Once the package is installed take a look at the structure of the data set by using:</p>
<p><strong>####################################################</strong><br>
<strong>attach(MathAchieve) #take a look at the structure of the dataset</strong><br>
<strong>str(MathAchieve)</strong><br>
<strong>####################################################</strong></p>
<p><em>Classes ‘nfnGroupedData’, ‘nfGroupedData’, ‘groupedData’ and ‘data.frame’: 7185 obs. of 6 variables:</em><br>
<em> $ School : Ord.factor w/ 160 levels “8367”&lt;“8854″&lt;..: 59 59 59 59 59 59 59 59 59 59 …</em><br>
<em> $ Minority: Factor w/ 2 levels “No”,”Yes”: 1 1 1 1 1 1 1 1 1 1 …</em><br>
<em> $ Sex : Factor w/ 2 levels “Male”,”Female”: 2 2 1 1 1 1 2 1 2 1 …</em><br>
<em> $ SES : num -1.528 -0.588 -0.528 -0.668 -0.158 …</em><br>
<em> $ MathAch : num 5.88 19.71 20.35 8.78 17.9 …</em><br>
<em> $ MEANSES : num -0.428 -0.428 -0.428 -0.428 -0.428 -0.428 -0.428 -0.428 -0.428 -0.428 …</em><br>
<em> – attr(*, “formula”)=Class ‘formula’ language MathAch ~ SES | School</em><br>
<em> .. ..- attr(*, “.Environment”)=&lt;environment: R_GlobalEnv> </em><br>
<em> – attr(*, “labels”)=List of 2</em><br>
<em> ..$ y: chr “Mathematics Achievement score”</em><br>
<em> ..$ x: chr “Socio-economic score”</em><br>
<em> – attr(*, “FUN”)=function (x) </em><br>
<em> ..- attr(*, “source”)= chr “function (x) max(x, na.rm = TRUE)”</em><br>
<em> – attr(*, “order.groups”)= logi TRUE</em><br>
<em>></em></p>
<p>As can be seen from the output shown above the <em>MathAchieve</em> dataset consists of 7185 observations and six variables. Three of these variables are numeric and three are factors. This presents some difficulties when visualizing the data. With over 7000 cases a two-dimensional scatterplot showing bivariate correlations among the three numeric variables is of limited utility.</p>
<p>We can use a 3D scatterplot and a linear regression model to more clearly visualize and examine relationships among the three numeric variables. The variable SES is a vector measuring socio-economic status, <em>MathAch</em> is a numeric vector measuring mathematics achievment scores, and <em>MEANSES</em> is a vector measuring the mean <em>SES</em> for the school attended by each student in the sample.</p>
<p>We can look at the correlation matrix of these 3 variables to get a sense of the relationships among the variables:</p>
<p><strong>> ####################################################</strong><br>
<strong>> #do a correlation matrix with the 3 numeric vars; </strong><br>
<strong>> ###################################################</strong><br>
<strong>> data(“MathAchieve”)</strong><br>
<strong>> cor(as.matrix(MathAchieve[c(4,5,6)]), method=”pearson”)  </strong></p>
<p><em>SES MathAch MEANSES</em><br>
<em>SES 1.0000000 0.3607556 0.5306221</em><br>
<em>MathAch 0.3607556 1.0000000 0.3437221</em><br>
<em>MEANSES 0.5306221 0.3437221 1.0000000</em><br>
<em>> </em></p>
<p>In using the <em>cor()</em> function as seen above we can determine the variables used by specifying the column that each numeric variable is in as shown in the output from the <em>str() </em>function.  The 3 numeric variables, for example, are in columns 4, 5, and 6 of the matrix.</p>
<p>As discussed in previous tutorials we can visualize the relationship among these three variable by using a 3D scatterplot. Use the code as seen below:</p>
<p><strong>####################################################</strong><br>
<strong>#install.packages(“nlme”)</strong><br>
<strong>install.packages(“scatterplot3d”)</strong><br>
<strong>library(scatterplot3d)</strong><br>
<strong>library(nlme) #load nmle package</strong><br>
<strong>attach(MathAchieve) #MathAchive dataset is in environment</strong><br>
<strong>scatterplot3d(SES, MEANSES, MathAch, main=”Basic 3D Scatterplot”) #do the plot with default options</strong><br>
<strong>####################################################</strong></p>
<p>The resulting plot is:</p>
<p><a target="_blank" rel="nofollow" href="https://i0.wp.com/dmwiig.files.wordpress.com/2017/02/mathach3dscatter.png?ssl=1"><img sizes="(max-width: 300px) 100vw, 300px" data-image-description="" data-orig-file="https://dmwiig.files.wordpress.com/2017/02/mathach3dscatter.png" data-orig-size="567,357" data-image-title="mathach3dscatter" class="wp-image-292" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="292" data-permalink="https://dmwiig.net/2017/02/06/r-tutorial-visualizing-multivariate-relationships-in-large-datasets/mathach3dscatter/" alt="mathach3dscatter" data-medium-file="https://i0.wp.com/dmwiig.files.wordpress.com/2017/02/mathach3dscatter.png?resize=300%2C189&amp;ssl=1" data-large-file="https://dmwiig.files.wordpress.com/2017/02/mathach3dscatter.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://i0.wp.com/dmwiig.files.wordpress.com/2017/02/mathach3dscatter.png?resize=300%2C189&ssl=1"></a></p>
<p>Even though the scatter plot lacks detail due to the large sample size it is still possible to see the moderate correlations shown in the correlation matrix by noting the shape and direction of the data points  .  A regression plane can be calculated and added to the plot using the following code:</p>
<p><strong>scatterplot3d(SES, MEANSES, MathAch, main=”Basic 3D Scatterplot”) #do the plot with default options</strong><br>
<strong>####################################################</strong><br>
<strong>##use a linear regression model to plot a regression plane</strong><br>
<strong>#y=MathAchieve, SES, MEANSES are predictor variables</strong><br>
<strong>####################################################</strong><br>
<strong>model1=lm(MathAch ~ SES + MEANSES)    ## generate a regression </strong><br>
<strong>#take a look at the regression output</strong><br>
<strong>summary(model1)</strong><br>
<strong>#run scatterplot again putting results in model</strong><br>
<strong>model &lt;- scatterplot3d(SES, MEANSES, MathAch, main=”Basic 3D Scatterplot”)     #do the plot with default options</strong><br>
<strong>#link the scatterplot and linear model using the plane3d function</strong><br>
<strong>model$plane3d(model1)        ## link the 3d scatterplot in ‘model’ to the ‘plane3d’ option with ‘model1’ regression information</strong><br>
<strong>####################################################</strong></p>
<p>The resulting output is seen below:</p>
<p><em>Call:</em><br>
<em>lm(formula = MathAch ~ SES + MEANSES)</em></p>
<p><em>Residuals:</em><br>
<em> Min 1Q Median 3Q Max </em><br>
<em>-20.4242 -4.6365 0.1403 4.8534 17.0496</em></p>
<p><em>Coefficients:</em><br>
<em> Estimate Std. Error t value Pr(>|t|) </em><br>
<em>(Intercept) 12.72590 0.07429 171.31 &lt;2e-16 ***</em><br>
<em>SES 2.19115 0.11244 19.49 &lt;2e-16 ***</em><br>
<em>MEANSES 3.52571 0.21190 16.64 &lt;2e-16 ***</em><br>
<em>—</em><br>
<em>Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1</em></p>
<p><em>Residual standard error: 6.296 on 7182 degrees of freedom</em><br>
<em>Multiple R-squared: 0.1624, Adjusted R-squared: 0.1622 </em><br>
<em>F-statistic: 696.4 on 2 and 7182 DF, p-value: &lt; 2.2e-16</em></p>
<p>and the plot with the plane is:</p>
<p><a target="_blank" rel="nofollow" href="https://i2.wp.com/dmwiig.files.wordpress.com/2017/02/mathachplot2.png?ssl=1"><img sizes="(max-width: 300px) 100vw, 300px" data-image-description="" data-orig-file="https://dmwiig.files.wordpress.com/2017/02/mathachplot2.png" data-orig-size="567,357" data-image-title="mathachplot2" data-comments-opened="1" class="wp-image-296" data-recalc-dims="1" data-attachment-id="296" data-permalink="https://dmwiig.net/2017/02/06/r-tutorial-visualizing-multivariate-relationships-in-large-datasets/mathachplot2/" alt="mathachplot2" data-medium-file="https://i2.wp.com/dmwiig.files.wordpress.com/2017/02/mathachplot2.png?resize=300%2C189&amp;ssl=1" data-large-file="https://dmwiig.files.wordpress.com/2017/02/mathachplot2.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://i2.wp.com/dmwiig.files.wordpress.com/2017/02/mathachplot2.png?resize=300%2C189&ssl=1"></a></p>
<p>While the above analysis gives us useful information, it is limited by the mixture of numeric values and factors.  A more detailed visual analysis that will allow the display and comparison of all six of the variables is possible by using the functions available in the R package <em>Tableplots</em>.   This package was created to aid in the visualization and inspection of large datasets with multiple variables.</p>
<p>The <em>MathAchieve</em> contains a total of six variables and 7185 cases.  The <em>Tableplots</em> package can be used with datasets larger than 10,000 observations and up to 12 or so variables. It can be used visualize relationships among variables using the same measurement scale or mixed measurement types.</p>
<p>To look at a comparisons of each data type and then view all 6 together begin with the following:</p>
<p><strong>####################################################</strong><br>
<strong>attach(MathAchieve) #attach the dataset</strong><br>
<strong>#set up 3 data frames with numeric, factors, and mixed</strong><br>
<strong>####################################################</strong><br>
<strong>mathmix &lt;- data.frame(SES,MathAch,MEANSES,School=factor(School),Minority=factor(Minority),Sex=factor(Sex)) #all 6 vars</strong><br>
<strong>mathfact &lt;- data.frame(School=factor(School),Minority=factor(Minority),Sex=factor(Sex)) #3 factor vars</strong><br>
<strong>mathnum &lt;- data.frame(SES,MathAch,MEANSES) #3 numeric vars</strong><br>
<strong>####################################################</strong></p>
<p>To view a comparison of the 3 numeric variables use:</p>
<p><strong>####################################################</strong><br>
<strong>require(tabplot) #load tabplot package</strong><br>
<strong>tableplot(mathnum) #generate a table plot with numeric vars only</strong><br>
<strong>####################################################</strong></p>
<p>resulting in the following output:</p>
<p><a target="_blank" rel="nofollow" href="https://i0.wp.com/dmwiig.files.wordpress.com/2017/02/mathnumplot.png?ssl=1"><img sizes="(max-width: 300px) 100vw, 300px" data-image-description="" data-orig-file="https://dmwiig.files.wordpress.com/2017/02/mathnumplot.png" data-orig-size="567,357" data-image-title="mathnumplot" data-comments-opened="1" class="wp-image-295" data-recalc-dims="1" data-attachment-id="295" data-permalink="https://dmwiig.net/2017/02/06/r-tutorial-visualizing-multivariate-relationships-in-large-datasets/mathnumplot/" alt="mathnumplot" data-medium-file="https://i0.wp.com/dmwiig.files.wordpress.com/2017/02/mathnumplot.png?resize=300%2C189&amp;ssl=1" data-large-file="https://dmwiig.files.wordpress.com/2017/02/mathnumplot.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://i0.wp.com/dmwiig.files.wordpress.com/2017/02/mathnumplot.png?resize=300%2C189&ssl=1"></a></p>
<p>To view only the 3 factor variables use:</p>
<p><strong>####################################################</strong><br>
<strong>require(tabplot)   #load tabplot package</strong><br>
<strong>tableplot(mathfact)    #generate a table plot with factors only</strong><br>
<strong>####################################################</strong></p>
<p>Resulting in:</p>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/dmwiig.files.wordpress.com/2017/02/mathfactplot.png?ssl=1"><img sizes="(max-width: 300px) 100vw, 300px" data-image-description="" data-orig-file="https://dmwiig.files.wordpress.com/2017/02/mathfactplot.png" data-orig-size="567,357" data-image-title="mathfactplot" data-comments-opened="1" class="wp-image-293" data-recalc-dims="1" data-attachment-id="293" data-permalink="https://dmwiig.net/2017/02/06/r-tutorial-visualizing-multivariate-relationships-in-large-datasets/mathfactplot/" alt="mathfactplot" data-medium-file="https://i1.wp.com/dmwiig.files.wordpress.com/2017/02/mathfactplot.png?resize=300%2C189&amp;ssl=1" data-large-file="https://dmwiig.files.wordpress.com/2017/02/mathfactplot.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/02/mathfactplot.png?resize=300%2C189&ssl=1"></a></p>
<p>To view and compare table plots of all six variables use:</p>
<p><strong>####################################################</strong><br>
<strong>require(tabplot)    #load tabplot package</strong><br>
<strong>tableplot(mathmix)    #generate a table plot with all six variables</strong><br>
<strong>####################################################</strong></p>
<p>Resulting in:</p>
<p><a target="_blank" rel="nofollow" href="https://i2.wp.com/dmwiig.files.wordpress.com/2017/02/mathmixplot.png?ssl=1"><img sizes="(max-width: 300px) 100vw, 300px" data-image-description="" data-orig-file="https://dmwiig.files.wordpress.com/2017/02/mathmixplot.png" data-orig-size="567,357" data-image-title="mathmixplot" data-comments-opened="1" class="wp-image-294" data-recalc-dims="1" data-attachment-id="294" data-permalink="https://dmwiig.net/2017/02/06/r-tutorial-visualizing-multivariate-relationships-in-large-datasets/mathmixplot/" alt="mathmixplot" data-medium-file="https://i2.wp.com/dmwiig.files.wordpress.com/2017/02/mathmixplot.png?resize=300%2C189&amp;ssl=1" data-large-file="https://dmwiig.files.wordpress.com/2017/02/mathmixplot.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://i2.wp.com/dmwiig.files.wordpress.com/2017/02/mathmixplot.png?resize=300%2C189&ssl=1"></a></p>
<p>Using <em>tableplots</em> is useful in visualizing relationships among a set of variabes. The fact that comparisons can be made using mixed levels of measurement and very large sample sizes provides a tool that the researcher can use for initial exploratory data analysis.</p>
<p>The above visual table comparisons agree with the moderate correlation among the three numeric variables found in the correlation and regression models discussed above.  It is also possible to add some additional interpretation by viewing and comparing the mix of both factor and numeric variables.</p>
<p>In this tutorial I have provided a very basic introduction to the use of table plots in visualizing data. Interested readers can find an abundance of information about <em>Tableplot</em> options and interpretations in the CRAN documentation.</p>
<p>In my next tutorial I will continue a discussion of methods to visualize large and complex datasets by looking at some techniques that allow exploration of very large datasets and up to 12 variables or more.</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dmwiig.wordpress.com/290/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/dmwiig.wordpress.com/290/" 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/02/06/r-tutorial-visualizing-multivariate-relationships-in-large-datasets/"> 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/hyZAGm4_yuo"></a></div>