XGBoost in R Programming

23 Dec, 2021
XGBoost is a fast and efficient algorithm and is used by winners of many machine learning competitions. XG Boost works only with the numeric variables.
It is a part of the boosting technique in which the selection of the sample is done more intelligently to classify observations. There are interfaces of XGBoost in C++, R, Python, Julia, Java, and Scala. The core functions in XGBoost are implemented in C++, thus it is easy to share models among different interfaces. Based on the statistics from the CRAN mirror, the package has been downloaded more than 81, 000 times. XgBoost modeling consists of two techniques: Bagging and Boosting. 
Bagging: It is an approach where you can take random samples of data, build learning algorithms and take simple means to find bagging probabilities.
Boosting: It is an approach where a selection of approaches is made more intelligently i.e more and more weight is given to classify observations.
eta: It shrinks the feature weights to make the boosting process more conservative. The range is from 0 to 1. It is also knowm as learning rate or Shrinking factor. Low eta value signifies the model is more robust to overfitting.
gamma: The larger the value of gamma, more conservative the algorithm will be. It’s range is from 0 to infinity.
max_depth: The maximum depth of a tree can be specified using max_depth parameter.
Subsample: It is the proportion of rows that the model will randomly select to grow trees.
colsample_bytree: It is the ratio of variables randomly chosen to build each tree in the model.
Big Mart dataset consists of 1559 products across 10 stores in different cities. Certain attributes of each product and store have been defined. It consists of 12 features i.e Item_Identifier( is a unique product ID assigned to every distinct item), Item_Weight(includes the weight of the product), Item_Fat_Content(describes whether the product is low fat or not), Item_Visibility(mentions the percentage of the total display area of all products in a store allocated to the particular product), Item_Type(describes the food category to which the item belongs), Item_MRP(Maximum Retail Price (list price) of the product), Outlet_Identifier(unique store ID assigned. It consists of an alphanumeric string of length 6), Outlet_Establishment_Year(mentions the year in which store was established), Outlet_Size(tells the size of the store in terms of ground area covered), Outlet_Location_Type(tells about the size of the city in which the store is located), Outlet_Type(tells whether the outlet is just a grocery store or some sort of supermarket) and Item_Outlet_Sales( sales of the product in the particular store). 
R
# Loading datatrain = fread("Train_UWu5bXk.csv")test = fread("Test_u94Q5KV.csv") # Structurestr(train)
Using XGBoost algorithm on the dataset which includes 12 features with 1559 products across 10 stores in different cities. 
R
# Installing Packagesinstall.packages("data.table")install.packages("dplyr")install.packages("ggplot2")install.packages("caret")install.packages("xgboost")install.packages("e1071")install.packages("cowplot") # Loading packageslibrary(data.table) # for reading and manipulation of datalibrary(dplyr)     # for data manipulation and joininglibrary(ggplot2) # for plotinglibrary(caret)     # for modelinglibrary(xgboost) # for building XGBoost modellibrary(e1071)     # for skewnesslibrary(cowplot) # for combining multiple plots # Setting test dataset# Combining datasets# add Item_Outlet_Sales to test datatest[, Item_Outlet_Sales := NA]combi = rbind(train, test) # Missing Value Treatmentmissing_index = which(is.na(combi$Item_Weight))for(i in missing_index){item = combi$Item_Identifier[i]combi$Item_Weight[i] = mean(combi$Item_Weight                        [combi$Item_Identifier == item],                        na.rm = T)} # Replacing 0 in Item_Visibility with meanzero_index = which(combi$Item_Visibility == 0)for(i in zero_index){item = combi$Item_Identifier[i]combi$Item_Visibility[i] = mean(    combi$Item_Visibility[combi$Item_Identifier == item],    na.rm = T)} # Label Encoding# To convert categorical in numericalcombi[, Outlet_Size_num :=        ifelse(Outlet_Size == "Small", 0,        ifelse(Outlet_Size == "Medium", 1, 2))] combi[, Outlet_Location_Type_num :=        ifelse(Outlet_Location_Type == "Tier 3", 0,        ifelse(Outlet_Location_Type == "Tier 2", 1, 2))] combi[, c("Outlet_Size", "Outlet_Location_Type") := NULL] # One Hot Encoding# To convert categorical in numericalohe_1 = dummyVars("~.",        data = combi[, -c("Item_Identifier",                    "Outlet_Establishment_Year",                    "Item_Type")], fullRank = T)ohe_df = data.table(predict(ohe_1,        combi[, -c("Item_Identifier",        "Outlet_Establishment_Year", "Item_Type")])) combi = cbind(combi[, "Item_Identifier"], ohe_df) # Remove skewnessskewness(combi$Item_Visibility)skewness(combi$price_per_unit_wt) # log + 1 to avoid division by zerocombi[, Item_Visibility := log(Item_Visibility + 1)] # Scaling and Centering data# index of numeric featuresnum_vars = which(sapply(combi, is.numeric))num_vars_names = names(num_vars) combi_numeric = combi[, setdiff(num_vars_names,                "Item_Outlet_Sales"), with = F] prep_num = preProcess(combi_numeric,                method = c("center", "scale"))combi_numeric_norm = predict(prep_num, combi_numeric) # removing numeric independent variablescombi[, setdiff(num_vars_names,            "Item_Outlet_Sales") := NULL]combi = cbind(combi,            combi_numeric_norm) # Splitting data back to train and testtrain = combi[1:nrow(train)]test = combi[(nrow(train) + 1):nrow(combi)] # Removing Item_Outlet_Salestest[, Item_Outlet_Sales := NULL] # Model Building: XGBoostparam_list = list(objective = "reg:linear",eta = 0.01,gamma = 1,max_depth = 6,subsample = 0.8,colsample_bytree = 0.5) # Converting train and test into xgb.DMatrix formatDtrain = xgb.DMatrix(        data = as.matrix(train[, -c("Item_Identifier",                                "Item_Outlet_Sales")]),        label = train$Item_Outlet_Sales)Dtest = xgb.DMatrix(        data = as.matrix(test[, -c("Item_Identifier")])) # 5-fold cross-validation to# find optimal value of nroundsset.seed(112) # Setting seedxgbcv = xgb.cv(params = param_list,            data = Dtrain,            nrounds = 1000,            nfold = 5,            print_every_n = 10,            early_stopping_rounds = 30,            maximize = F) # Training XGBoost model at nrounds = 428xgb_model = xgb.train(data = Dtrain,                    params = param_list,                    nrounds = 428)xgb_model # Variable Importancevar_imp = xgb.importance(            feature_names = setdiff(names(train),            c("Item_Identifier", "Item_Outlet_Sales")),            model = xgb_model) # Importance plotxgb.plot.importance(var_imp)
Output: 
Training of Xgboost model:
The xgboost model is trained calculating the train-rmse score and test-rmse score and finding its lowest value in many rounds.
Model xgb_model:
The XgBoost models consist of 21 features with the objective of regression linear, eta is 0.01, gamma is 1, max_depth is 6, subsample is 0.8, colsample_bytree = 0.5, and silent is 1.
Variable Importance plot:
The Item_MRP is the most important variable followed by Item_Visibility and Outlet_Location_Type_num. 
So, Xgboost finds its applications in many sectors of industries and is used with full capacity.
kumar_satyam
data-science
R Machine-Learning
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to filter R DataFrame by values in a column?
R - if statement
Logistic Regression in R Programming
Replace Specific Characters in String in R
How to import an Excel File into R ?
Joining of Dataframes in R Programming