R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

rm(list = ls())       
set.seed(123)       

library("gbm")        
## Warning: package 'gbm' was built under R version 4.5.3
## Loaded gbm 2.3.1
## This version of gbm is no longer under development. Consider transitioning to gbm3, https://github.com/gbm-developers/gbm3
library("caret")      
## Warning: package 'caret' was built under R version 4.5.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.5.3
## Loading required package: lattice
library("ggplot2")    

# DATASET
data_train <- read.csv("C:/Users/HP/OneDrive/Documents/MUTIA SEMES-5/MACHINE LEARNING/PRAKTIKUM ADABOOST U GRADIENTBOOST/train.csv")  
data_test  <- read.csv("C:/Users/HP/OneDrive/Documents/MUTIA SEMES-5/MACHINE LEARNING/PRAKTIKUM ADABOOST U GRADIENTBOOST/test.csv")   

# PRE-PROCESSING DATA
# Memilih variabel
df <- data_train[, c("price",
                     "room_type", "city", "accommodates",
                     "bedrooms", "bathrooms", "number_of_reviews")]

data_test <- data_test[, c("room_type", "city", "accommodates",
                           "bedrooms", "bathrooms", "number_of_reviews")]

# Memeriksa nilai hilang
colSums(is.na(df))          
##             price         room_type              city      accommodates 
##                 0                 0                 0                 0 
##          bedrooms         bathrooms number_of_reviews 
##                66               144                 0
colSums(is.na(data_test))   
##         room_type              city      accommodates          bedrooms 
##                 0                 0                 0                25 
##         bathrooms number_of_reviews 
##                56                 0
# Imputasi nilai hilang dengan median (data train)
med_bedrooms  <- median(df$bedrooms,  na.rm = TRUE)
med_bathrooms <- median(df$bathrooms, na.rm = TRUE)
med_bedrooms
## [1] 1
med_bathrooms
## [1] 1
df$bedrooms[is.na(df$bedrooms)]   <- med_bedrooms
df$bathrooms[is.na(df$bathrooms)] <- med_bathrooms

data_test$bedrooms[is.na(data_test$bedrooms)]   <- med_bedrooms
data_test$bathrooms[is.na(data_test$bathrooms)] <- med_bathrooms

# Memeriksa nilai hilang setelah imputasi
colSums(is.na(df))         
##             price         room_type              city      accommodates 
##                 0                 0                 0                 0 
##          bedrooms         bathrooms number_of_reviews 
##                 0                 0                 0
colSums(is.na(data_test))  
##         room_type              city      accommodates          bedrooms 
##                 0                 0                 0                 0 
##         bathrooms number_of_reviews 
##                 0                 0
# Mengubah variabel kategorik menjadi factor (data train)
df$room_type <- as.factor(df$room_type)
df$city      <- as.factor(df$city)

# Mengubah variabel kategorik menjadi factor (data test)
data_test$room_type <- as.factor(data_test$room_type)
data_test$city      <- as.factor(data_test$city)

# Memeriksa hasil pre-processing
str(df)                    
## 'data.frame':    51879 obs. of  7 variables:
##  $ price            : num  150 169 145 750 115 ...
##  $ room_type        : Factor w/ 3 levels "Entire home/apt",..: 1 1 1 1 1 2 1 1 2 2 ...
##  $ city             : Factor w/ 6 levels "Boston","Chicago",..: 5 5 5 6 3 6 4 4 6 4 ...
##  $ accommodates     : int  3 7 5 4 2 2 3 2 2 2 ...
##  $ bedrooms         : int  1 3 1 2 0 1 1 1 1 1 ...
##  $ bathrooms        : num  1 1 1 1 1 1 1 1 1 1 ...
##  $ number_of_reviews: int  2 6 10 0 4 3 15 9 159 2 ...
str(data_test)             
## 'data.frame':    22232 obs. of  6 variables:
##  $ room_type        : Factor w/ 3 levels "Entire home/apt",..: 1 2 1 2 1 1 1 2 1 2 ...
##  $ city             : Factor w/ 6 levels "Boston","Chicago",..: 4 2 4 4 3 5 4 4 5 5 ...
##  $ accommodates     : int  4 2 4 2 4 6 4 8 2 1 ...
##  $ bedrooms         : int  2 1 1 1 1 3 2 1 1 1 ...
##  $ bathrooms        : num  1.5 1.5 1 1 1 1 1 1 1 1 ...
##  $ number_of_reviews: int  29 0 73 2 0 14 248 44 0 0 ...
summary(df)                 
##      price                  room_type          city        accommodates  
##  Min.   :   1.0   Entire home/apt:28966   Boston : 2432   Min.   : 1.00  
##  1st Qu.:  75.0   Private room   :21423   Chicago: 2573   1st Qu.: 2.00  
##  Median : 111.0   Shared room    : 1490   DC     : 3947   Median : 2.00  
##  Mean   : 160.4                           LA     :15733   Mean   : 3.15  
##  3rd Qu.: 185.0                           NYC    :22655   3rd Qu.: 4.00  
##  Max.   :1999.0                           SF     : 4539   Max.   :16.00  
##     bedrooms        bathrooms     number_of_reviews
##  Min.   : 0.000   Min.   :0.000   Min.   :  0.00   
##  1st Qu.: 1.000   1st Qu.:1.000   1st Qu.:  1.00   
##  Median : 1.000   Median :1.000   Median :  6.00   
##  Mean   : 1.261   Mean   :1.233   Mean   : 20.82   
##  3rd Qu.: 1.000   3rd Qu.:1.000   3rd Qu.: 23.00   
##  Max.   :10.000   Max.   :8.000   Max.   :605.00
# PEMBAGIAN DATA TRAIN (80% training, 20% testing)
index      <- sample(1:nrow(df), round(0.8 * nrow(df)))
train_data <- df[index, ]
test_data  <- df[-index, ]

nrow(train_data)     
## [1] 41503
nrow(test_data)       
## [1] 10376
sd(test_data$price)
## [1] 168.2594
# MODEL GRADIENT BOOSTING
model_GBR <- gbm(
  formula           = price ~ .,
  distribution      = "gaussian",   # loss kuadrat: 1/2 (y - F)^2
  data              = train_data,
  n.trees           = 1000,         # batas atas jumlah pohon (M)
  shrinkage         = 0.05,         # learning rate (v)
  interaction.depth = 3,            # kedalaman tiap pohon
  n.minobsinnode    = 10,           # minimum observasi per daun
  bag.fraction      = 0.8,          # tiap pohon memakai 80% data
  cv.folds          = 5,            # 5-fold cross validation
  verbose           = FALSE
)
model_GBR
## gbm(formula = price ~ ., distribution = "gaussian", data = train_data, 
##     n.trees = 1000, interaction.depth = 3, n.minobsinnode = 10, 
##     shrinkage = 0.05, bag.fraction = 0.8, cv.folds = 5, verbose = FALSE)
## A gradient boosted model with gaussian loss function.
## 1000 iterations were performed.
## The best cross-validation iteration was 441.
## There were 6 predictors of which 6 had non-zero influence.
# Jumlah pohon optimal (early stopping via cross validation)
best <- gbm.perf(model_GBR, method = "cv")

best
## [1] 441
# PENTINGNYA VARIABEL (relative influence)
par(mar = c(5, 10, 2, 2))   
imp <- summary(model_GBR, n.trees = best, plotit = TRUE, las = 1)

imp
##                                 var   rel.inf
## bedrooms                   bedrooms 28.882400
## bathrooms                 bathrooms 24.326745
## accommodates           accommodates 17.382558
## room_type                 room_type 12.467774
## city                           city  8.563438
## number_of_reviews number_of_reviews  8.377085
# EFEK MARGINAL (partial dependence)
plot(model_GBR, i.var = "bedrooms",          n.trees = best)

plot(model_GBR, i.var = "bathrooms",         n.trees = best)

plot(model_GBR, i.var = "accommodates",      n.trees = best)

plot(model_GBR, i.var = "room_type",         n.trees = best)

plot(model_GBR, i.var = "city",              n.trees = best)

plot(model_GBR, i.var = "number_of_reviews", n.trees = best)

# PREDIKSI DAN EVALUASI PADA DATA TESTING
pred_GBR <- predict(model_GBR, newdata = test_data, n.trees = best)

metrik <- function(aktual, prediksi) {
  c(RMSE = sqrt(mean((aktual - prediksi)^2)),
    MAE  = mean(abs(aktual - prediksi)),
    R2   = 1 - sum((aktual - prediksi)^2) / sum((aktual - mean(aktual))^2))
}
metrik(test_data$price, pred_GBR)
##        RMSE         MAE          R2 
## 118.4304426  62.0686399   0.5045382
# Lihat hasil prediksi
hasil          <- test_data
hasil$prediksi <- pred_GBR
tail(hasil, 10)
##       price       room_type city accommodates bedrooms bathrooms
## 51824   100    Private room  NYC            2        1         1
## 51826   119 Entire home/apt   LA            1        0         1
## 51833    50    Private room  NYC            2        1         1
## 51855    65    Private room   LA            2        1         1
## 51865    75    Private room  NYC            2        1         1
## 51866   150 Entire home/apt  NYC            2        0         1
## 51870    95 Entire home/apt   LA            4        0         1
## 51872   100    Private room  NYC            2        1         1
## 51877   185 Entire home/apt  NYC            5        2         1
## 51879   128 Entire home/apt   LA            4        1         1
##       number_of_reviews  prediksi
## 51824                55  81.93235
## 51826                31  98.33694
## 51833                 0 102.95077
## 51855                26  70.13682
## 51865                16  83.89093
## 51866                 5 147.55183
## 51870               177 117.05415
## 51872                32  83.51347
## 51877                43 202.38698
## 51879               205 127.99602
# GRAFIK AKTUAL VS PREDIKSI (30 listing pertama data testing)
grafik <- data.frame(index    = 1:30,
                     Aktual   = hasil$price[1:30],
                     Prediksi = hasil$prediksi[1:30])

ggplot(grafik, aes(x = index)) +
  geom_line(aes(y = Aktual,   color = "Aktual")) +
  geom_line(aes(y = Prediksi, color = "Prediksi")) +
  geom_point(aes(y = Aktual)) +
  geom_point(aes(y = Prediksi)) +
  labs(x = "index", y = "price", color = "Keterangan:")

# TUNING HYPERPARAMETER DENGAN CARET (grid search, 5-fold CV)
ctrl <- trainControl(method = "cv", number = 5)
grid <- expand.grid(n.trees           = c(50, 100, 200),
                    interaction.depth = c(2, 3, 4),
                    shrinkage         = c(0.01, 0.05, 0.1),
                    n.minobsinnode    = 10)
model_cv <- train(price ~ ., data = train_data,
                  method    = "gbm",
                  trControl = ctrl,
                  tuneGrid  = grid,
                  verbose   = FALSE)
model_cv$bestTune                                    
##    n.trees interaction.depth shrinkage n.minobsinnode
## 27     200                 4       0.1             10
head(model_cv$results[order(model_cv$results$RMSE), ])  
##    shrinkage interaction.depth n.minobsinnode n.trees     RMSE  Rsquared
## 27      0.10                 4             10     200 119.4655 0.5009168
## 26      0.10                 4             10     100 119.5967 0.5001003
## 24      0.10                 3             10     200 119.6760 0.4993670
## 18      0.05                 4             10     200 119.7780 0.4986695
## 23      0.10                 3             10     100 120.2048 0.4953328
## 15      0.05                 3             10     200 120.3682 0.4939011
##         MAE   RMSESD RsquaredSD     MAESD
## 27 62.22150 1.494024 0.01789341 0.4392205
## 26 62.47220 1.477695 0.01857675 0.3548022
## 24 62.56417 1.380440 0.01699232 0.4580608
## 18 62.55562 1.310189 0.01752277 0.3868770
## 23 62.95249 1.216403 0.01672065 0.3893082
## 15 62.91205 1.599969 0.02003256 0.4112791
# MODEL DENGAN HYPERPARAMETER HASIL TUNING
terbaik <- model_cv$bestTune

model_tuning <- gbm(
  formula           = price ~ .,
  distribution      = "gaussian",
  data              = train_data,
  n.trees           = terbaik$n.trees,
  shrinkage         = terbaik$shrinkage,
  interaction.depth = terbaik$interaction.depth,
  n.minobsinnode    = terbaik$n.minobsinnode,
  bag.fraction      = 0.8,
  verbose           = FALSE
)
model_tuning
## gbm(formula = price ~ ., distribution = "gaussian", data = train_data, 
##     n.trees = terbaik$n.trees, interaction.depth = terbaik$interaction.depth, 
##     n.minobsinnode = terbaik$n.minobsinnode, shrinkage = terbaik$shrinkage, 
##     bag.fraction = 0.8, verbose = FALSE)
## A gradient boosted model with gaussian loss function.
## 200 iterations were performed.
## There were 6 predictors of which 6 had non-zero influence.
pred_tuning <- predict(model_tuning, newdata = test_data,
                       n.trees = terbaik$n.trees)

# Perbandingan model utama dan model hasil tuning
rbind("Model Utama"        = metrik(test_data$price, pred_GBR),
      "Model Hasil Tuning" = metrik(test_data$price, pred_tuning))
##                        RMSE      MAE        R2
## Model Utama        118.4304 62.06864 0.5045382
## Model Hasil Tuning 118.2787 61.97061 0.5058067
# PREDIKSI HARGA UNTUK DATA BARU
data_test$prediksi_price <- predict(model_tuning, newdata = data_test,
                                    n.trees = best)
## Warning in predict.gbm(model_tuning, newdata = data_test, n.trees = best):
## Number of trees not specified or exceeded number fit so far. Using 200.
head(data_test, 10)                 
##          room_type    city accommodates bedrooms bathrooms number_of_reviews
## 1  Entire home/apt      LA            4        2       1.5                29
## 2     Private room Chicago            2        1       1.5                 0
## 3  Entire home/apt      LA            4        1       1.0                73
## 4     Private room      LA            2        1       1.0                 2
## 5  Entire home/apt      DC            4        1       1.0                 0
## 6  Entire home/apt     NYC            6        3       1.0                14
## 7  Entire home/apt      LA            4        2       1.0               248
## 8     Private room      LA            8        1       1.0                44
## 9  Entire home/apt     NYC            2        1       1.0                 0
## 10    Private room     NYC            1        1       1.0                 0
##    prediksi_price
## 1       191.48228
## 2        91.91072
## 3       129.83958
## 4        74.97097
## 5       396.67526
## 6       256.66769
## 7       150.61667
## 8       133.44691
## 9       185.19097
## 10       86.14146