<span><span lang="en"><span>We will develop a forecasting example using multiple linear regression. The exercise was originally published in <i>&quot;Machine Learning in R&quot; </i>by Brett Lantz, PACKT publishing 2015 (open source community experience destilled).</span></span></span><br><br><span><span lang="en"><span><span lang="en"><span>The example we will develop is about predicting medical expenses.</span></span></span></span></span><br><br><div><a href="https://3.bp.blogspot.com/-BSPZlZWxDTE/WQtcRQucnCI/AAAAAAAAC3s/IyfVxps9V7cFWFQAmCgSzW9CNlca19naACLcB/s1600/medical_exp.jpg.png"></a></div><div><br></div><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><br><br><span><span lang="en"><span><span lang="en"><span>### install and load required packages<br>#install.packages(&quot;psych&quot;)<br>library(psych)<br><br>### read and explore the data<br>insurance &lt;- read.csv(&quot;insurance.csv&quot;, stringsAsFactors = TRUE)<br>str(insurance)<br><br>### model dependent variable: $expenses<br>### change $charges name to $expenses<br>colnames(insurance)[7] &lt;- &quot;expenses&quot;<br>summary(insurance$expenses)<br>hist(insurance$expenses, main = &quot;Insurance Expenses&quot;, col = &quot;red&quot;,<br>     xlab = &quot;Expenses (USD)&quot;)<br><br>### explore $region<br>table(insurance$region)<br><br>### exoploring relationships among features<br>### correlation matrix<br>cor(insurance[c(&quot;age&quot;, &quot;bmi&quot;, &quot;children&quot;, &quot;expenses&quot;)])</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><br><div><a href="https://3.bp.blogspot.com/-8N441yZjHYA/WQtX2R2iAdI/AAAAAAAAC2o/SxMkW_Lm9SUcJ8HAVwf3zmDd98p_7lqnQCLcB/s1600/linear_2.png"></a></div><span><span lang="en"><span><span lang="en"><span><br></span></span></span></span></span><span><span lang="en"><span><span lang="en"><span>### visualizing relationships among features<br>### scatterplot matrix<br>pairs(insurance[c(&quot;age&quot;, &quot;bmi&quot;, &quot;children&quot;, &quot;expenses&quot;)])</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><br><div><a href="https://3.bp.blogspot.com/-mdKb9gmrYQU/WQtX_laivtI/AAAAAAAAC2w/EVtt4Z7yk64qWBA-3ZcuDZ3UlrrxMOQtwCLcB/s1600/linear_3.png"></a></div><br><span><span lang="en"><span><span lang="en"><span>### scatterplots, distributions and correlations<br>pairs.panels(insurance[c(&quot;age&quot;, &quot;bmi&quot;, &quot;children&quot;, &quot;expenses&quot;)])</span></span></span></span></span><br><div><a href="https://4.bp.blogspot.com/-RpIcCG0TsdY/WQtYGz9o95I/AAAAAAAAC20/rqEItro8TjEMd2VqeXODKqBN6fusyz0qgCEw/s1600/linear_4.png"></a></div><span><span lang="en"><span><span lang="en"><span></span></span></span></span></span><br><span><span lang="en"><span><span lang="en"><span>### training a model on the data<br>ins_model &lt;- lm(expenses ~ age + children + bmi + sex +<br>                  smoker + region, data = insurance)<br>### this do the same<br>#ins_model &lt;- lm(expenses ~ ., data = insurance)<br><br>### explore model parameters<br>ins_model</span></span></span></span></span><br><span><span lang="en"><span><span lang="en"><span><br></span></span></span></span></span><br><div><a href="https://2.bp.blogspot.com/-zpDp_nhz1y0/WQtYo5fX_4I/AAAAAAAAC24/91osHUrJzIsxyusWwHTc_yu2hOzb_ndRACLcB/s1600/linear_5.png"></a></div><br><span><span lang="en"><span><span lang="en"><span>### evaluating model performance<br>summary(ins_model)</span></span></span></span></span><br><br><span><span lang="en"><span><span lang="en"><span></span></span></span></span></span><br><div><a href="https://4.bp.blogspot.com/-RDn9b3n9odA/WQtY0zAtxKI/AAAAAAAAC28/f-ZYA9UNP10e-fdHpRs0Qxb8oJaqi4fVACLcB/s1600/linear_6.png"></a></div><br><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>### improving model performance<br><br>### adding non-linear relationships<br>### adding second order term on $age<br>insurance$age2 &lt;- insurance$age^2<br><br>### converting a numeric variable to a binary indicator<br>### $bmi feature only have impact above some value<br>insurance$bmi30 &lt;- ifelse(insurance$bmi >= 30, 1, 0)<br><br>### putting it all together<br>### improved regression model<br>ins_model2 &lt;- lm(expenses ~ age + age2 + children + bmi + sex +<br>                   bmi30*smoker + region, data = insurance)<br><br>summary(ins_model2)</span></span></span></span></span><br><br><div><a href="https://3.bp.blogspot.com/-eM7AqsdqFtk/WQtaD_c0gqI/AAAAAAAAC3I/pN9ArjxSVQ8H6vz49Od4H0DpFzPjGIPUACLcB/s1600/linear_7.png"></a></div><br><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><br><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></span></span></span><span><a target="_blank" 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><br><br><br>