<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/quantile-regression-in-r-exercises/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/06/05/quantile-regression-in-r-exercises/?utm_source=rss&utm_medium=rss&utm_campaign=quantile-regression-in-r-exercises"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2017/05/Rplot2_ex4.jpeg" target="_blank"><img alt="" src="https://i2.wp.com/www.r-exercises.com/wp-content/uploads/2017/05/Rplot2_ex4-300x122.jpg?resize=150%2C150" class="wp-image-5754" data-recalc-dims="1"></a></p>
<p>The standard OLS (Ordinary Least Squares) model explains the relationship between independent variables and the conditional mean of the dependent variable. In contrast, quantile regression models this relationship for different quantiles of the dependent variable.<br>
In this exercise set we will use the <code>quantreg</code> package (package description: <a rel="nofollow" href="https://cran.r-project.org/web/packages/quantreg/quantreg.pdf" target="_blank">here</a>) to implement quantile regression in R.</p>
<p>Answers to the exercises are available <a rel="nofollow" href="http://r-exercises.com/2017/06/05/quantile-regression-in-r-solutions/" target="_blank">here</a>.</p>
<p><strong>Exercise 1</strong><br>
Load the <code>quantreg</code> package and the <code>barro</code> dataset (Barro and Lee, 1994). This has data on GDP growth rates for various countries.<br>
Next, summarize the data.</p>
<p><strong>Exercise 2</strong><br>
The dependent variable is <code>y.net</code> (Annual change per capita GDP). The remaining variables will be used to explain <code>y.net</code>. It is easier to combine variables using <code>cbind</code> before applying regression techniques. Combine variables so that we can write <code>Y ~ X</code>.</p>
<p><strong>Exercise 3</strong><br>
Regress <code>y.net</code> on the independent variables using OLS. We will use this result as benchmark for comparison.</p>
<p><strong>Exercise 4</strong><br>
Using the <code>rq</code> function, estimate the model at the median <code>y.net</code>. Compare results from exercise-3.</p>
<div>
<aside><img src="http://www.r-exercises.com/2017/06/05/quantile-regression-in-r-exercises/?utm_source=rss&utm_medium=rss&utm_campaign=quantile-regression-in-r-exercises"></aside>
<div><strong>Learn more</strong> about Model Evaluation in the online course <a rel="nofollow" href="http://www.r-exercises.com/product/regression-machine-learning-with-r/" target="_blank">Regression Machine Learning with R</a>. In this course you will learn how to:
<ul>
<li>Avoid model over-fitting using cross-validation for optimal parameter selection</li>
<li>Explore maximum margin methods such as best penalty of error term support vector machines with linear and non-linear kernels.</li>
<li>And much more</li>
</ul>
</div>
</div>
<p><strong>Exercise 5</strong><br>
Estimate the model for the first and third quartiles and compare results.</p>
<p><strong>Exercise 6</strong><br>
Using a single command estimate the model for 10 equally spaced deciles of <code>y.net</code>.</p>
<p><strong>Exercise 7</strong><br>
<code>quantreg</code> package also offers shrinkage estimators to determine which variables play the most important role in predicting <code>y.net</code>. Estimate the model with LASSO based quantile regression at the median level with lambda=0.5.</p>
<p><strong>Exercise 8</strong><br>
Quantile plots are most useful for interpreting results. To do that we need to define the sequence of percentiles. Use the <code>seq</code> function to define the sequence of percentiles from 5% to 95% with a jump of 5%.</p>
<p><strong>Exercise 9</strong><br>
Use the result from exercise-8 to plot the graphs. Note that the red line is the OLS estimate bounded by the dotted lines which represent confidence intervals.</p>
<p><strong>Exercise 10</strong><br>
Using results from exercise-5, test whether coefficients are significantly different for the first and third quartile based regressions.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/05/01/forecasting-multivariate-regression-exercises-part-4/" title="Forecasting: Multivariate Regression Exercises (Part-4)" target="_blank">Forecasting: Multivariate Regression Exercises (Part-4) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/05/29/instrumental-variables-in-r-exercises-part-3/" title="Instrumental Variables in R exercises (Part-3)" target="_blank">Instrumental Variables in R exercises (Part-3) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/05/05/forecasting-arimax-model-exercises-part-5/" title="Forecasting: ARIMAX Model Exercises (Part-5)" target="_blank">Forecasting: ARIMAX Model Exercises (Part-5) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/" target="_blank">Explore all our (>1000) R exercises</a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/r-courses/" target="_blank">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://www.r-exercises.com/2017/06/05/quantile-regression-in-r-exercises/?utm_source=rss&utm_medium=rss&utm_campaign=quantile-regression-in-r-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 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 width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/1SsfETObcCM" height="1"></a></div>