<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/machine-learning-regression-trees-and-model-trees-predicting-wine-quality/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://feedproxy.google.com/~r/DataRValue/~3/wJ2QyTJQOvM/machine-learning-regression-trees-and.html"> Data R Value</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><span><span lang="en"><span>We will develop a </span></span></span><span><span lang="en"><span><span><span lang="en"><span>forecasting example</span></span></span> using model trees and regression trees algorithms. The exercise was originally published in <i>“Machine Learning in R” </i>by Brett Lantz, PACKT publishing 2015 (open source community experience destilled).</span></span></span></p>
<p><span><span lang="en"><span><span><span lang="en"><span><span lang="en"><span>The example we will develop is about predicting wine quality.</span></span></span></span></span></span></span></span></p>
<div><a target="_blank" rel="nofollow" href="https://i2.wp.com/3.bp.blogspot.com/-rjPuZ2Bw9HU/WRCD_SF1wfI/AAAAAAAAC8E/fX9flvfgEJMTdFIbLbPX0wUheycA_7HQwCLcB/s1600/Different-Types-of-Wine-v2.jpg?ssl=1"><img src="https://i1.wp.com/3.bp.blogspot.com/-rjPuZ2Bw9HU/WRCD_SF1wfI/AAAAAAAAC8E/fX9flvfgEJMTdFIbLbPX0wUheycA_7HQwCLcB/s400/Different-Types-of-Wine-v2.jpg?resize=300%2C400&ssl=1" border="0" data-recalc-dims="1"></a></div>
<div></div>
<div><span><span>Image from Wine Folly</span></span></div>
<p><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span><span lang="en"><span><span lang="en"><span>We will carry out the exercise verbatim as published in the aforementioned reference</span></span></span></span></span></span><span lang="en"><span>.</span></span></span></span></span></span></span></span></p>
<p><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span><span><span lang="en"><span><span lang="en"><span>For more details on the </span></span></span></span></span></span></span></span></span></span></span></span></span><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span>model trees and regression trees</span></span></span> algorithms it is recommended to check the aforementioned reference or </span></span></span></span></span><span><span lang="en"><span><span lang="en"><span><span lang="en"><span>any other bibliography of your choice</span></span>.</span></span></span></span></span></span></span></span></span></span></span></span></span><br><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span><br></span></span></span></span></span></span></span></span><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span><br><b>### install an load required packages</b><br>#install.packages(“rpart”)<br>#install.packages(“rpart.plot”)
<p>library(rpart)<br>library(rpart.plot)<br>library(RWeka)</p>
<p><b>### read and explore the data</b><br>wine &lt;-read.csv(“whitewines.csv”)<br>str(wine)</p>
<p><b>### examine the distribution of the outcome variable</b><br>hist(wine$quality, main= “Wine Quality”, col= “red”)<br><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span></span></span></span></span></span></span></span></span>
<div><a target="_blank" rel="nofollow" href="https://i0.wp.com/2.bp.blogspot.com/-5vZ2YzDjAC8/WRCEZxn3mYI/AAAAAAAAC8I/oLrr1av2_P0WhCmVW3cN9Dp2WJMtVFtowCLcB/s1600/wine_1.png?ssl=1"><img src="https://i0.wp.com/2.bp.blogspot.com/-5vZ2YzDjAC8/WRCEZxn3mYI/AAAAAAAAC8I/oLrr1av2_P0WhCmVW3cN9Dp2WJMtVFtowCLcB/s400/wine_1.png?resize=397%2C400&ssl=1" border="0" data-recalc-dims="1"></a></div></span>
<p><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span><b>### examine output for outliers or other potential data problems</b><br>summary(wine)
<p><b>### split the data into training and testing datasets<br>### 75% – 25%</b><br>wine_train &lt;-wine[1:3750, ]<br>wine_test &lt;-wine[3751:4898, ]</p>
<p><b>### training a model on the data
<p>### training a regression tree model<br>m.rpart &lt;-rpart(quality ~ ., data = wine_train)</p></b>
<p><b>### explore basic information about the tree<br>### for each node in the tree, the number of examples reaching<br>### the decision point is listed</b><br>m.rpart<br><b><br>### more detailed summary of tree’s fit</b><br>summary(m.rpart)</p>
<p><b>### visualazing the decision tree</b><br>rpart.plot(m.rpart, digits = 3)<br><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span></span></span></span></span></span></span></span></span>
<div><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span></span></span></span></span></span></span></span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span> </span></span></span></span></span></span></span></span><a target="_blank" rel="nofollow" href="https://i2.wp.com/2.bp.blogspot.com/-d_Mer08-0Vg/WRCEw6q6G4I/AAAAAAAAC8M/pprvlgryjKQIrHe6E_656vkxFUufeRpqQCLcB/s1600/wine_2.png?ssl=1"><img src="https://i0.wp.com/2.bp.blogspot.com/-d_Mer08-0Vg/WRCEw6q6G4I/AAAAAAAAC8M/pprvlgryjKQIrHe6E_656vkxFUufeRpqQCLcB/s400/wine_2.png?resize=397%2C400&ssl=1" border="0" data-recalc-dims="1"></a></div></span>
<p><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span></span></span></span></span></span></span></span></span><br><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span>rpart.plot(m.rpart, digits = 4, fallen.leaves = TRUE,<br>           type = 3, extra = 101)</span></span></span></span></span></span></span></span><br><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span><b></b></span></span></span></span></span></span></span></span>
<div><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span><b> </b></span></span></span></span></span></span></span></span><a target="_blank" rel="nofollow" href="https://i1.wp.com/2.bp.blogspot.com/-43vN_vPu1jQ/WRCE7sM6LnI/AAAAAAAAC8Q/UiLhbbeL47sONWKR4YimC90xmOr4NcCDgCLcB/s1600/wine_3.png?ssl=1"><img src="https://i2.wp.com/2.bp.blogspot.com/-43vN_vPu1jQ/WRCE7sM6LnI/AAAAAAAAC8Q/UiLhbbeL47sONWKR4YimC90xmOr4NcCDgCLcB/s400/wine_3.png?resize=397%2C400&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span><b>### evaluating model performance</b><br>p.rpart &lt;-predict(m.rpart, wine_test)
<p><b>### model is not correctly identifying the extreme cases</b><br>summary(p.rpart)<br>summary(wine_test$quality)</p>
<p><b>### correlation between actual and predicted quality</b><br>cor(p.rpart, wine_test$quality)</p>
<p><b>### Measuring performance with the mean absolute error</b></p>
<p>MAE &lt;-function(actual, predicted) {<br>  mean(abs(actual – predicted))<br>}<br><b><br>### the MAE for our predictions</b><br>MAE(p.rpart, wine_test$quality)</p>
<p><b>### the mean quality rating in the training data</b><br>mean(wine_train$quality)<br><b><br>### If we predicted the value 5.87 for every wine sample,<br>### we would have a mean absolute error of only about 0.67:</b><br>MAE(5.87, wine_test$quality)<br><b><br>### improving model performance?
<p>### model tree<br>### model tree improves on regression trees by replacing the<br>### leaf nodes with regression models<br>### M5 algorithm (M5-prime)<br>m.m5p &lt;-M5P(quality ~ ., data = wine_train)<br><b><br>### examine the tree and linear models</b><br>m.m5p<br>summary(m.m5p)</p></b>
<p><b>### predicted values and performance</b><br>p.m5p &lt;-predict(m.m5p, wine_test)</p>
<p>summary(p.m5p)</p>
<p>cor(p.m5p, wine_test$quality)</p>
<p>MAE(wine_test$quality, p.m5p)<br><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span></span></span></span></span></span></span></span></span><br><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span></span></span></span></span></span></span></span></span><br><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span><b>###</b></span></span></span></span></span></span></span></span><br><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span>You can get the example and the dataset in:</span></span></span></span></span></span></span></span><br><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span><a target="_blank" rel="nofollow" href="https://github.com/pakinja/Data-R-Value/tree/master/MachineLearning_RegressionTrees_ModelTrees">https://github.com/pakinja/Data-R-Value</a> </span></span></span></span></span></span></span></span></p></span>
<p><img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/DataRValue/~4/wJ2QyTJQOvM"></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="http://feedproxy.google.com/~r/DataRValue/~3/wJ2QyTJQOvM/machine-learning-regression-trees-and.html"> Data R Value</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/vP2zVZ-vX-4"></a></div></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>