<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/correlation-and-correlogram-exercises/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2017/04/08/correlation-and-correlogram-exercises/?utm_source=rss&utm_medium=rss&utm_campaign=correlation-and-correlogram-exercises"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="nofollow" href="http://r-exercises.com/wp-content/uploads/2017/04/Correlations_figure_2.png?x61433"><img sizes="(max-width: 300px) 100vw, 300px" alt="" class="wp-image-4892" src="https://i0.wp.com/r-exercises.com/wp-content/uploads/2017/04/Correlations_figure_2-300x165.png?resize=300%2C165" data-recalc-dims="1"></a><br>
Correlation analysis is one of the most popular techniques for data exploration. This set of exercises is intended to help you to extend, speed up, and validate your correlation analysis. It allows to practice in:<br>
– calculating linear and nonlinear correlation coefficients,<br>
– testing those coefficients for statistical significance,<br>
– creating correlation matrices to study interdependence between variables in dataframes,<br>
– drawing graphical representations of those matrices (correlograms),<br>
– calculating coefficients for partial correlation between two variables (controlling for their correlation with other variables).<br>
The exercises make use of functions from the packages <code>Hmisc</code>, <code>corrgram</code>, and <code>ggm</code>. Please install these packages, but do not load them before starting the exercises in which they are needed (to avoid a namespace conflict) (the <code>ggm</code> package contains the function called <code>rcorr</code> which masks the <code>rcorr</code> function from the <code>Hmisc</code> package, and vice versa. If you want to return to the <code>rcorr</code> function from the <code>Hmisc</code> after loading the <code>ggm</code> package run <code>detach(package:ggm)</code>).<br>
Exercises are based on a reduced version of the <code>auto</code> dataset from the <code>corrgram</code> package (<a target="_blank" rel="nofollow" href="http://r-exercises.com/wp-content/uploads/2017/04/auto.csv">download here</a>). The dataset contains characteristics of 1979 automobile models.<br>
Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://r-exercises.com/2017/04/08/correlation-and-correlogram-exercises-solutions/">here</a></p>
<p><strong>Exercise 1</strong><br>
Calculate simple (linear) correlation between car price and its fuel economy (measured in miles per gallon, or mpg).</p>
<p><strong>Exercise 2</strong><br>
Use the <code>cor.test</code> function to check whether the obtained coefficient is statistically significant at 5% level.</p>
<p><strong>Exercise 3</strong><br>
Simple correlation assumes a linear relationship between variables, but it may be useful to relax this assumption. Calculate Spearman’s correlation coefficient for the same variables, and find its statistical significance.</p>
<p><strong>Exercise 4</strong><br>
In R, it is possible to calculate correlation for all pairs of numeric variables in a dataframe at once. However, this requires excluding non-numeric variables first.<br>
Create a new dataframe, <code>auto_num</code>, that contains only columns with numeric values from the <code>auto</code> dataframe. You can do this using the <code>Filter</code> function.</p>
<p><strong>Exercise 5</strong><br>
Use the <code>cor</code> function to create a matrix of correlation coefficients for variables in the <code>auto_num</code> dataframe.</p>
<p><strong>Exercise 6</strong><br>
The standard <code>cor.test</code> function does not work with dataframes. However, statistical significance of correlation coefficients for a dataframe can be verified using the <code>rcorr</code> function from the <code>Hmisc</code> package.<br>
Transform the <code>auto_num</code> dataframe into a matrix (<code>auto_mat</code>), and use it to check significance of the correlation coefficients with the <code>rcorr</code> function.</p>
<p><strong>Exercise 7</strong><br>
Use the <code>corrgram</code> function from the corrgram package to create a default correlogram to visualize correlations between variables in the <code>auto</code> dataframe.</p>
<p><strong>Exercise 8</strong><br>
Create another correlogram that (1) includes only the lower panel, (2) uses pie diagrams to represent correlation coefficients, and (3) orders the variables using the default order.</p>
<p><strong>Exercise 9</strong><br>
Create a new dataframe, <code>auto_subset</code>, by subsetting the <code>auto</code> dataframe to include only the <code>Price</code>, <code>MPG</code>, <code>Hroom</code>, and <code>Rseat</code> variables. Use the new dataframe to create a correlogram that (1) shows correlation coefficients on the lower panel, and (2) shows scatter plots (points) on the upper panel.</p>
<p><strong>Exercise 10</strong><br>
Use the the <code>correlations</code> function from the <code>ggm</code> package to create a correlation matrix with both full and partial correlation coefficients for the <code>auto_subset</code> dataframe. Find the partial correlation between car price and its fuel economy.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a title="Accessing Dataframe Objects Exercises" target="_blank" rel="nofollow" href="http://r-exercises.com/2016/05/20/accessing-dataframe-objects-exercises/">Accessing Dataframe Objects Exercises </a></li>
<li><a title="Cross Tabulation with Xtabs exercises" target="_blank" rel="nofollow" href="http://r-exercises.com/2016/05/12/cross-tabulation-with-xtabs-exercises/">Cross Tabulation with Xtabs exercises </a></li>
<li><a title="Merging Dataframes Exercises" target="_blank" rel="nofollow" href="http://r-exercises.com/2016/04/14/merging-dataframes-exercises/">Merging Dataframes Exercises </a></li>
<li><a target="_blank" rel="nofollow" href="http://r-exercises.com/start-here-to-learn-r/">Explore all our (>1000) R exercises</a></li>
<li><a target="_blank" rel="nofollow" href="http://r-exercises.com/r-courses/">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r-exercises.com/2017/04/08/correlation-and-correlogram-exercises/?utm_source=rss&utm_medium=rss&utm_campaign=correlation-and-correlogram-exercises"> R-exercises</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/aBFRHCvulqI"></a></div>