<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-forecasting-numeric-data-with-multiple-linear-regression-medical-expenses/"></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/Ofqt4uihOg0/machine-learning-forecasting-numeric.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 forecasting example using multiple linear regression. 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 lang="en"><span>The example we will develop is about predicting medical expenses.</span></span></span></span></span></p>
<div><a target="_blank" rel="nofollow" href="https://i1.wp.com/3.bp.blogspot.com/-BSPZlZWxDTE/WQtcRQucnCI/AAAAAAAAC3s/IyfVxps9V7cFWFQAmCgSzW9CNlca19naACLcB/s1600/medical_exp.jpg.png?ssl=1"><img src="https://i2.wp.com/3.bp.blogspot.com/-BSPZlZWxDTE/WQtcRQucnCI/AAAAAAAAC3s/IyfVxps9V7cFWFQAmCgSzW9CNlca19naACLcB/s320/medical_exp.jpg.png?resize=320%2C212&ssl=1" border="0" data-recalc-dims="1"></a></div>
<div></div>
<p><span><span lang="en"><span><span lang="en"><span></span></span></span></span></span><br><span><span lang="en"><span><span lang="en"><span></span></span></span></span></span><br><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 and </span></span></span></span></span></span><span lang="en"><span>including some simple transformations to the dataset.</span></span></span></span></span></span></span></span><br><span><span lang="en"><span><span lang="en"><span><br></span></span></span></span></span><span><span lang="en"><span><span lang="en"><span>For more details on the linear regression 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></p>
<p><span><span lang="en"><span><span lang="en"><span><b>### install and load required packages</b><br>#install.packages(“psych”)<br>library(psych)
<p><b>### read and explore the data</b><br>insurance &lt;-read.csv(“insurance.csv”, stringsAsFactors = TRUE)<br>str(insurance)</p>
<p><b>### model dependent variable: $expenses<br>### change $charges name to $expenses</b><br>colnames(insurance)[7] &lt;-“expenses”<br>summary(insurance$expenses)<br>hist(insurance$expenses, main = “Insurance Expenses”, col = “red”,<br>     xlab = “Expenses (USD)”)</p>
<p><b>### explore $region</b><br>table(insurance$region)</p>
<p><b>### exoploring relationships among features<br>### correlation matrix</b><br>cor(insurance[c(“age”, “bmi”, “children”, “expenses”)])<br><span><span lang="en"><span><span lang="en"><span></span></span></span></span></span><br><span><span lang="en"><span><span lang="en"><span><br></span></span></span></span></span>
<div><a target="_blank" rel="nofollow" href="https://i0.wp.com/3.bp.blogspot.com/-8N441yZjHYA/WQtX2R2iAdI/AAAAAAAAC2o/SxMkW_Lm9SUcJ8HAVwf3zmDd98p_7lqnQCLcB/s1600/linear_2.png?ssl=1"><img src="https://i2.wp.com/3.bp.blogspot.com/-8N441yZjHYA/WQtX2R2iAdI/AAAAAAAAC2o/SxMkW_Lm9SUcJ8HAVwf3zmDd98p_7lqnQCLcB/s400/linear_2.png?resize=400%2C91&ssl=1" border="0" data-recalc-dims="1"></a></div></span>
<p><span><span lang="en"><span><span lang="en"><span><br></span></span></span></span></span><span><span lang="en"><span><span lang="en"><span><b>### visualizing relationships among features<br>### scatterplot matrix</b><br>pairs(insurance[c(“age”, “bmi”, “children”, “expenses”)])</span></span></span></span></span><br><span><span lang="en"><span><span lang="en"><span></span></span></span></span></span><br><span><span lang="en"><span><span lang="en"><span><br></span></span></span></span></span>
<div><a target="_blank" rel="nofollow" href="https://i2.wp.com/3.bp.blogspot.com/-mdKb9gmrYQU/WQtX_laivtI/AAAAAAAAC2w/EVtt4Z7yk64qWBA-3ZcuDZ3UlrrxMOQtwCLcB/s1600/linear_3.png?ssl=1"><img src="https://i2.wp.com/3.bp.blogspot.com/-mdKb9gmrYQU/WQtX_laivtI/AAAAAAAAC2w/EVtt4Z7yk64qWBA-3ZcuDZ3UlrrxMOQtwCLcB/s400/linear_3.png?resize=400%2C363&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p><span><span lang="en"><span><span lang="en"><span><b>### scatterplots, distributions and correlations</b><br>pairs.panels(insurance[c(“age”, “bmi”, “children”, “expenses”)])</span></span></span></span></span>
<div><a target="_blank" rel="nofollow" href="https://i1.wp.com/4.bp.blogspot.com/-RpIcCG0TsdY/WQtYGz9o95I/AAAAAAAAC20/rqEItro8TjEMd2VqeXODKqBN6fusyz0qgCEw/s1600/linear_4.png?ssl=1"><img src="https://i0.wp.com/4.bp.blogspot.com/-RpIcCG0TsdY/WQtYGz9o95I/AAAAAAAAC20/rqEItro8TjEMd2VqeXODKqBN6fusyz0qgCEw/s400/linear_4.png?resize=400%2C363&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p><span><span lang="en"><span><span lang="en"><span></span></span></span></span></span><br><span><span lang="en"><span><span lang="en"><span><b>### training a model on the data</b><br>ins_model &lt;-lm(expenses ~ age + children + bmi + sex +<br>                  smoker + region, data = insurance)<br><b>### this do the same</b><br>#ins_model &lt;-lm(expenses ~ ., data = insurance)<br><b><br>### explore model parameters</b><br>ins_model</span></span></span></span></span><br><span><span lang="en"><span><span lang="en"><span><br><b></b></span></span></span></span></span>
<div><a target="_blank" rel="nofollow" href="https://i1.wp.com/2.bp.blogspot.com/-zpDp_nhz1y0/WQtYo5fX_4I/AAAAAAAAC24/91osHUrJzIsxyusWwHTc_yu2hOzb_ndRACLcB/s1600/linear_5.png?ssl=1"><img src="https://i1.wp.com/2.bp.blogspot.com/-zpDp_nhz1y0/WQtYo5fX_4I/AAAAAAAAC24/91osHUrJzIsxyusWwHTc_yu2hOzb_ndRACLcB/s400/linear_5.png?resize=400%2C151&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p><span><span lang="en"><span><span lang="en"><span><b>### evaluating model performance</b><br>summary(ins_model)</span></span></span></span></span></p>
<p><span><span lang="en"><span><span lang="en"><span></span></span></span></span></span>
<div><a target="_blank" rel="nofollow" href="https://i2.wp.com/4.bp.blogspot.com/-RDn9b3n9odA/WQtY0zAtxKI/AAAAAAAAC28/f-ZYA9UNP10e-fdHpRs0Qxb8oJaqi4fVACLcB/s1600/linear_6.png?ssl=1"><img src="https://i2.wp.com/4.bp.blogspot.com/-RDn9b3n9odA/WQtY0zAtxKI/AAAAAAAAC28/f-ZYA9UNP10e-fdHpRs0Qxb8oJaqi4fVACLcB/s400/linear_6.png?resize=400%2C378&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p><span><span lang="en"><span>The model explains 74.9% of the variation of the dependent variable (adjusted R-squared: 0.7494).</span></span></span><br><span><span lang="en"><span><span lang="en"><span><br></span></span></span></span></span><span><span lang="en"><span><span lang="en"><span><b>### improving model performance</b><br><b><br>### adding non-linear relationships<br>### adding second order term on $age</b><br>insurance$age2 &lt;-insurance$age^2
<p><b>### converting a numeric variable to a binary indicator<br>### $bmi feature only have impact above some value</b><br>insurance$bmi30 &lt;-ifelse(insurance$bmi >= 30, 1, 0)<br><b><br>### putting it all together<br>### improved regression model</b><br>ins_model2 &lt;-lm(expenses ~ age + age2 + children + bmi + sex +<br>                   bmi30*smoker + region, data = insurance)</p>
<p>summary(ins_model2)</p></span>
<div><a target="_blank" rel="nofollow" href="https://i1.wp.com/3.bp.blogspot.com/-eM7AqsdqFtk/WQtaD_c0gqI/AAAAAAAAC3I/pN9ArjxSVQ8H6vz49Od4H0DpFzPjGIPUACLcB/s1600/linear_7.png?ssl=1"><img src="https://i0.wp.com/3.bp.blogspot.com/-eM7AqsdqFtk/WQtaD_c0gqI/AAAAAAAAC3I/pN9ArjxSVQ8H6vz49Od4H0DpFzPjGIPUACLcB/s400/linear_7.png?resize=400%2C392&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p><span><span lang="en"><span><span lang="en"><span><span lang="en"><span>The accuracy of the model has improved to an 86.5% of explanation of the variation of the independent variable.</span></span> </span></span></span></span></span></p>
<p><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></span></span></span><span><a target="_blank" rel="nofollow" href="https://github.com/pakinja/Data-R-Value/tree/master/MachineLearning_Linear_Regression">https://github.com/pakinja/Data-R-Value</a></span> </span></span></span></span></span><br><span><span lang="en"><span><span lang="en"><span><br></span></span></span></span></span></p>
<p><img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/DataRValue/~4/Ofqt4uihOg0"></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/Ofqt4uihOg0/machine-learning-forecasting-numeric.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/gqj4A5deAdI"></a></div></span></span></span></span></span></span></span></span>