# R: Gradient Boosted Machine to Predict MPG for 2019 Vehicles

**Data Science, Machine Learning and Predictive Analytics**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Continuing on the below post, I am going to use a gradient boosted machine model to predict combined miles per gallon for all 2019 motor vehicles.

Part 1: Using Decision Trees and Random Forest to Predict MPG for 2019 Vehicles

The raw data is located on the EPA government site

The variables/features I am using for the models are: Engine displacement (size), number of cylinders, transmission type, number of gears, air inspired method, regenerative braking type, battery capacity Ah, drivetrain, fuel type, cylinder deactivate, and variable valve.

There are 1253 vehicles in the dataset (does not include pure electric vehicles) summarized below.

fuel_economy_combined eng_disp num_cyl transmission<br /> Min. :11.00 Min. :1.000 Min. : 3.000 A :301 <br /> 1st Qu.:19.00 1st Qu.:2.000 1st Qu.: 4.000 AM : 46 <br /> Median :23.00 Median :3.000 Median : 6.000 AMS: 87 <br /> Mean :23.32 Mean :3.063 Mean : 5.533 CVT: 50 <br /> 3rd Qu.:26.00 3rd Qu.:3.600 3rd Qu.: 6.000 M :148 <br /> Max. :58.00 Max. :8.000 Max. :16.000 SA :555 <br /> SCV: 66 <br /> num_gears air_aspired_method<br /> Min. : 1.000 Naturally Aspirated :523 <br /> 1st Qu.: 6.000 Other : 5 <br /> Median : 7.000 Supercharged : 55 <br /> Mean : 7.111 Turbocharged :663 <br /> 3rd Qu.: 8.000 Turbocharged+Supercharged: 7 <br /> Max. :10.000 <br /> <br /> regen_brake batt_capacity_ah <br /> No :1194 Min. : 0.0000 <br /> Electrical Regen Brake: 57 1st Qu.: 0.0000 <br /> Hydraulic Regen Brake : 2 Median : 0.0000 <br /> Mean : 0.3618 <br /> 3rd Qu.: 0.0000 <br /> Max. :20.0000 <br /> <br /> drive cyl_deactivate<br /> 2-Wheel Drive, Front :345 Y: 172<br /> 2-Wheel Drive, Rear :345 N:1081<br /> 4-Wheel Drive :174 <br /> All Wheel Drive :349 <br /> Part-time 4-Wheel Drive: 40 <br /> <br /> <br /> fuel_type <br /> Diesel, ultra low sulfur (15 ppm, maximum): 28 <br /> Gasoline (Mid Grade Unleaded Recommended) : 16 <br /> Gasoline (Premium Unleaded Recommended) :298 <br /> Gasoline (Premium Unleaded Required) :320 <br /> Gasoline (Regular Unleaded Recommended) :591 <br /> <br /> <br /> variable_valve<br /> N: 38 <br /> Y:1215 <br />

Starting with an untuned base model:

trees <- 1200<br />m_boosted_reg_untuned <- gbm(<br /> formula = fuel_economy_combined ~ .,<br /> data = train,<br /> n.trees = trees,<br /> distribution = "gaussian"<br />)<br />

> summary(m_boosted_reg_untuned)<br /> var rel.inf<br />eng_disp eng_disp 41.26273684<br />batt_capacity_ah batt_capacity_ah 24.53458898<br />transmission transmission 11.33253784<br />drive drive 8.59300859<br />regen_brake regen_brake 8.17877824<br />air_aspired_method air_aspired_method 2.11397865<br />num_gears num_gears 1.90999021<br />fuel_type fuel_type 1.65692562<br />num_cyl num_cyl 0.22260369<br />variable_valve variable_valve 0.11043532<br />cyl_deactivate cyl_deactivate 0.08441602<br />> boosted_stats_untuned<br /> RMSE Rsquared MAE <br />2.4262643 0.8350367 1.7513331 <br />

The untuned GBM model performs better than the multiple linear regression model, but worse than the random forest.

I am going to tune the GBM by running a grid search:

#create hyperparameter grid<br />hyper_grid <- expand.grid(<br /> shrinkage = seq(.07, .12, .01),<br /> interaction.depth = 1:7,<br /> optimal_trees = 0,<br /> min_RMSE = 0<br />)<br /><br />#grid search<br />for (i in 1:nrow(hyper_grid)) {<br /> set.seed(123)<br /> gbm.tune <- gbm(<br /> formula = fuel_economy_combined ~ .,<br /> data = train_random,<br /> distribution = "gaussian",<br /> n.trees = 5000,<br /> interaction.depth = hyper_grid$interaction.depth[i],<br /> shrinkage = hyper_grid$shrinkage[i],<br /> )<br /> <br /> hyper_grid$optimal_trees[i] <- which.min(gbm.tune$train.error)<br /> hyper_grid$min_RMSE[i] <- sqrt(min(gbm.tune$train.error))<br /> <br /> cat(i, "\n")<br />}<br />

The hyper grid is 42 rows which is all combinations of shrinkage and interaction depths specified above.

> head(hyper_grid)<br /> shrinkage interaction.depth optimal_trees min_RMSE<br />1 0.07 1 0 0<br />2 0.08 1 0 0<br />3 0.09 1 0 0<br />4 0.10 1 0 0<br />5 0.11 1 0 0<br />6 0.12 1 0 0

After running the grid search, it is apparent that there is overfitting. This is something to be very careful about. I am going to run a 5 fold cross validation to estimate out of bag error vs MSE. After running the 5 fold CV, this is the best model that does not overfit:

> m_boosted_reg <- gbm(<br /> formula = fuel_economy_combined ~ .,<br /> data = train,<br /> n.trees = trees,<br /> distribution = "gaussian",<br /> shrinkage = .09,<br /> cv.folds = 5,<br /> interaction.depth = 5<br />)<br /><br />best.iter <- gbm.perf(m_boosted_reg, method = "cv")<br />pred_boosted_reg_ <- predict(m_boosted_reg,n.trees=1183, newdata = test)<br />mse_boosted_reg_ <- RMSE(pred = pred_boosted_reg, obs = test$fuel_economy_combined) ^2<br />boosted_stats<-postResample(pred_boosted_reg,test$fuel_economy_combined)<br />

The fitted black curve above is MSE and the fitted green curve is the out of bag estimated error. 1183 is the optimal amount of iterations.

> pred_boosted_reg <- predict(m_boosted_reg,n.trees=1183, newdata = test)<br />> mse_boosted_reg <- RMSE(pred = pred_boosted_reg, obs = test$fuel_economy_combined) ^2<br />> boosted_stats<-postResample(pred_boosted_reg,test$fuel_economy_combined)<br />> boosted_stats<br /> RMSE Rsquared MAE <br />1.8018793 0.9092727 1.3334459 <br />> mse_boosted_reg<br />3.246769 <br />

The tuned gradient boosted model performs better than the random forest with a MSE of 3.25 vs 3.67 for the random forest.

> summary(res)<br /> Min. 1st Qu. Median Mean 3rd Qu. Max. <br />-5.40000 -0.90000 0.00000 0.07643 1.10000 9.10000 <br />

50% of the predictions are within 1 MPG of the EPA Government Estimate.

The largest residuals are exotics and a hybrid which are the more unique data points in the dataset.

> tmp[which(abs(res) > boosted_stats[1] * 3), ] <br /> Division Carline fuel_economy_combined pred_boosted_reg<br />642 HYUNDAI MOTOR COMPANY Ioniq Blue 58 48.5<br />482 KIA MOTORS CORPORATION Forte FE 35 28.7<br />39 Lamborghini Aventador Coupe 11 17.2<br />40 Lamborghini Aventador Roadster 11 17.2<br />

**leave a comment**for the author, please follow the link and comment on their blog:

**Data Science, Machine Learning and Predictive Analytics**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.