Uvod

Izabrani skup podataka opisuje podatke o potražnji dve vrste hotela. Jedan od hotela (H1 - resort hotel) je odmarališni, dok je drugi tip (H2 - city hotel) gradski tip hotela. Oba skupa podataka obuhvataju rezervacije registrovane između 01.07.2015. i 31.08.2017. godine. Na osnovu opisa seta, znamo da se oba hotela nalaze u Portugalu, prvi u provinciji Algarve, a drugi u Lisabonu. Skup podataka sadrži 119 390 primera i 32 atributa, pa ćemo zbog pojednostavljanja eksploratorne analize izabrati one atribute koji najbolje opisuju skup podataka.

Ono što imamo za cilj jeste da na osnovu podataka iz seta predvidimo da li će neki turista prihvatiti ili odbiti rezervaciju, kao i da li je moguće sprečiti otkazivanje rezervacija. Atribut is_canceled uzima vrednosti 0 (nije otkazana rezervacija) ili 1 (jeste otkazana rezervacija).

Učitavanje skupa podataka

library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(scales)
library(tidyverse)
## -- Attaching packages ------------------------------------------------------------------------ tidyverse 1.3.0 --
## <U+221A> tibble  2.1.3     <U+221A> purrr   0.3.3
## <U+221A> tidyr   1.0.0     <U+221A> stringr 1.4.0
## <U+221A> readr   1.3.1     <U+221A> forcats 0.4.0
## -- Conflicts --------------------------------------------------------------------------- tidyverse_conflicts() --
## x readr::col_factor() masks scales::col_factor()
## x purrr::discard()    masks scales::discard()
## x dplyr::filter()     masks stats::filter()
## x dplyr::lag()        masks stats::lag()
library(readr)
library(zeallot)
library(countrycode)
library(ISLR)
## Warning: package 'ISLR' was built under R version 3.6.3
library(caret)
## Warning: package 'caret' was built under R version 3.6.3
## Loading required package: lattice
## 
## Attaching package: 'caret'
## The following object is masked from 'package:purrr':
## 
##     lift
library(rpart)
library(rpart.plot)
## Warning: package 'rpart.plot' was built under R version 3.6.3
library(rattle)
## Warning: package 'rattle' was built under R version 3.6.3
## Loading required package: bitops
## Rattle: A free graphical interface for data science with R.
## Version 5.4.0 Copyright (c) 2006-2020 Togaware Pty Ltd.
## Type 'rattle()' to shake, rattle, and roll your data.
library(arm)
## Warning: package 'arm' was built under R version 3.6.3
## Loading required package: MASS
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
## 
##     select
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## Loading required package: lme4
## Warning: package 'lme4' was built under R version 3.6.3
## 
## arm (Version 1.11-2, built: 2020-7-27)
## Working directory is C:/Users/Nevena/Documents/BookingHotel
## 
## Attaching package: 'arm'
## The following object is masked from 'package:scales':
## 
##     rescale
data <-
  read_csv(
    "hotel_bookings.csv"
  )
## Parsed with column specification:
## cols(
##   .default = col_double(),
##   hotel = col_character(),
##   arrival_date_month = col_character(),
##   meal = col_character(),
##   country = col_character(),
##   market_segment = col_character(),
##   distribution_channel = col_character(),
##   reserved_room_type = col_character(),
##   assigned_room_type = col_character(),
##   deposit_type = col_character(),
##   agent = col_character(),
##   company = col_character(),
##   customer_type = col_character(),
##   reservation_status = col_character(),
##   reservation_status_date = col_date(format = "")
## )
## See spec(...) for full column specifications.
head(data)
## # A tibble: 6 x 32
##   hotel is_canceled lead_time arrival_date_ye~ arrival_date_mo~ arrival_date_we~
##   <chr>       <dbl>     <dbl>            <dbl> <chr>                       <dbl>
## 1 Reso~           0       342             2015 July                           27
## 2 Reso~           0       737             2015 July                           27
## 3 Reso~           0         7             2015 July                           27
## 4 Reso~           0        13             2015 July                           27
## 5 Reso~           0        14             2015 July                           27
## 6 Reso~           0        14             2015 July                           27
## # ... with 26 more variables: arrival_date_day_of_month <dbl>,
## #   stays_in_weekend_nights <dbl>, stays_in_week_nights <dbl>, adults <dbl>,
## #   children <dbl>, babies <dbl>, meal <chr>, country <chr>,
## #   market_segment <chr>, distribution_channel <chr>, is_repeated_guest <dbl>,
## #   previous_cancellations <dbl>, previous_bookings_not_canceled <dbl>,
## #   reserved_room_type <chr>, assigned_room_type <chr>, booking_changes <dbl>,
## #   deposit_type <chr>, agent <chr>, company <chr>, days_in_waiting_list <dbl>,
## #   customer_type <chr>, adr <dbl>, required_car_parking_spaces <dbl>,
## #   total_of_special_requests <dbl>, reservation_status <chr>,
## #   reservation_status_date <date>

Pre nego što sredimo podatke, napravićemo podskup našeg skupa podataka, odnosno izabraćemo atribute koje ćemo koristiti za analizu i proverićemo kog su tipa podaci.

hotel_data = subset(data, select=c("hotel","is_canceled","lead_time","arrival_date_year","arrival_date_month","stays_in_weekend_nights","stays_in_week_nights","country","customer_type","agent","company","reservation_status","adr","reserved_room_type","assigned_room_type"))

str(hotel_data)
## Classes 'tbl_df', 'tbl' and 'data.frame':    119390 obs. of  15 variables:
##  $ hotel                  : chr  "Resort Hotel" "Resort Hotel" "Resort Hotel" "Resort Hotel" ...
##  $ is_canceled            : num  0 0 0 0 0 0 0 0 1 1 ...
##  $ lead_time              : num  342 737 7 13 14 14 0 9 85 75 ...
##  $ arrival_date_year      : num  2015 2015 2015 2015 2015 ...
##  $ arrival_date_month     : chr  "July" "July" "July" "July" ...
##  $ stays_in_weekend_nights: num  0 0 0 0 0 0 0 0 0 0 ...
##  $ stays_in_week_nights   : num  0 0 1 1 2 2 2 2 3 3 ...
##  $ country                : chr  "PRT" "PRT" "GBR" "GBR" ...
##  $ customer_type          : chr  "Transient" "Transient" "Transient" "Transient" ...
##  $ agent                  : chr  "NULL" "NULL" "NULL" "304" ...
##  $ company                : chr  "NULL" "NULL" "NULL" "NULL" ...
##  $ reservation_status     : chr  "Check-Out" "Check-Out" "Check-Out" "Check-Out" ...
##  $ adr                    : num  0 0 75 75 98 ...
##  $ reserved_room_type     : chr  "C" "C" "A" "A" ...
##  $ assigned_room_type     : chr  "C" "C" "C" "A" ...
summary(hotel_data)
##     hotel            is_canceled       lead_time   arrival_date_year
##  Length:119390      Min.   :0.0000   Min.   :  0   Min.   :2015     
##  Class :character   1st Qu.:0.0000   1st Qu.: 18   1st Qu.:2016     
##  Mode  :character   Median :0.0000   Median : 69   Median :2016     
##                     Mean   :0.3704   Mean   :104   Mean   :2016     
##                     3rd Qu.:1.0000   3rd Qu.:160   3rd Qu.:2017     
##                     Max.   :1.0000   Max.   :737   Max.   :2017     
##  arrival_date_month stays_in_weekend_nights stays_in_week_nights
##  Length:119390      Min.   : 0.0000         Min.   : 0.0        
##  Class :character   1st Qu.: 0.0000         1st Qu.: 1.0        
##  Mode  :character   Median : 1.0000         Median : 2.0        
##                     Mean   : 0.9276         Mean   : 2.5        
##                     3rd Qu.: 2.0000         3rd Qu.: 3.0        
##                     Max.   :19.0000         Max.   :50.0        
##    country          customer_type         agent             company         
##  Length:119390      Length:119390      Length:119390      Length:119390     
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##  reservation_status      adr          reserved_room_type assigned_room_type
##  Length:119390      Min.   :  -6.38   Length:119390      Length:119390     
##  Class :character   1st Qu.:  69.29   Class :character   Class :character  
##  Mode  :character   Median :  94.58   Mode  :character   Mode  :character  
##                     Mean   : 101.83                                        
##                     3rd Qu.: 126.00                                        
##                     Max.   :5400.00

Zbog politike privatnosti nekih kompanija, korisnici ne daju informacije o agencijama preko kojih vrše rezervacije. Iz tog razloga, proveravamo koliko imamo neprijavljenih vrednosti za agente i kompanije.

length(which(hotel_data$agent == "NULL"))
## [1] 16340
length(which(hotel_data$company == "NULL"))
## [1] 112593

Vidimo da neprijavljenih kompanija 112593 ima u 119390 primera, pa atribut company možemo zanemariti. Ispitaćemo koliko imamo različitih agenata u setu podataka.

length(unique(hotel_data$agent))
## [1] 334

Broj različitih agenata je 334, od ukupno 16340 prijavljenih. Tokom dalje analize odlučićemo da li ćemo uvrstiti i ovaj atribut u analizu.

Nakon toga, pretvorićemo sve promenljive koje su tipa character u factor.

hotel_data[sapply(hotel_data, is.character)] <-
  lapply(hotel_data[sapply(hotel_data, is.character)], as.factor)

str(hotel_data)
## Classes 'tbl_df', 'tbl' and 'data.frame':    119390 obs. of  15 variables:
##  $ hotel                  : Factor w/ 2 levels "City Hotel","Resort Hotel": 2 2 2 2 2 2 2 2 2 2 ...
##  $ is_canceled            : num  0 0 0 0 0 0 0 0 1 1 ...
##  $ lead_time              : num  342 737 7 13 14 14 0 9 85 75 ...
##  $ arrival_date_year      : num  2015 2015 2015 2015 2015 ...
##  $ arrival_date_month     : Factor w/ 12 levels "April","August",..: 6 6 6 6 6 6 6 6 6 6 ...
##  $ stays_in_weekend_nights: num  0 0 0 0 0 0 0 0 0 0 ...
##  $ stays_in_week_nights   : num  0 0 1 1 2 2 2 2 3 3 ...
##  $ country                : Factor w/ 178 levels "ABW","AGO","AIA",..: 137 137 60 60 60 60 137 137 137 137 ...
##  $ customer_type          : Factor w/ 4 levels "Contract","Group",..: 3 3 3 3 3 3 3 3 3 3 ...
##  $ agent                  : Factor w/ 334 levels "1","10","103",..: 334 334 334 157 103 103 334 156 103 40 ...
##  $ company                : Factor w/ 353 levels "10","100","101",..: 353 353 353 353 353 353 353 353 353 353 ...
##  $ reservation_status     : Factor w/ 3 levels "Canceled","Check-Out",..: 2 2 2 2 2 2 2 2 1 1 ...
##  $ adr                    : num  0 0 75 75 98 ...
##  $ reserved_room_type     : Factor w/ 10 levels "A","B","C","D",..: 3 3 1 1 1 1 3 3 1 4 ...
##  $ assigned_room_type     : Factor w/ 12 levels "A","B","C","D",..: 3 3 3 1 1 1 3 3 1 4 ...

Pre nego što izanaliziramo izabrane atribute, videćemo za koji tip smeštaja se turisti radije opredeljuju.

table(hotel_data$hotel)
## 
##   City Hotel Resort Hotel 
##        79330        40060

Vidimo da su među turistima popularniji gradski tipovi hotela. Predstavićemo to i grafički.

ggplot(data = hotel_data, aes(x = hotel)) +
  geom_bar(stat = "count") +
  labs(title = "Broj rezervacija u zavisnosti od vrste smestaja",
       x = "Tip hotela",
       y = "Broj rezervacija")

U svakoj kategoriji hotela proveravamo koliko je bilo otkazanih rezervacija, a koliko rezervacija nije otkazano.

table(hotel_data$is_canceled, hotel_data$hotel)
##    
##     City Hotel Resort Hotel
##   0      46228        28938
##   1      33102        11122

Vizualizovaćemo podatke, s tim što ćemo pripadnost svakoj grupi predstaviti procentualno:

ggplot(data = hotel_data,
       aes(
         x = hotel,
         y = prop.table(stat(count)),
         fill = factor(is_canceled),
         label = scales::percent(prop.table(stat(count)))
       )) +
  geom_bar(position = position_dodge()) +
  geom_text(
    stat = "count",
    position = position_dodge(.9),
    vjust = -0.5,
    size = 3
  ) +
  scale_y_continuous(labels = scales::percent) +
  labs(title = "Status rezervacije u zavisnosti od vrste sme㤼㹡taja",
       x = "Tip hotela",
       y = "Zastupljenost") +
  theme_classic() +
  scale_fill_discrete(
    name = "Status rezervacije",
    breaks = c("0", "1"),
    labels = c("Nije otkazano", "Otkazano")
  )

Kao što sa grafika možemo videti, broj potvrđenih rezervacija je u obe grupe veći od broja otkazanih rezervacija.

Sada ćemo ispitati status otkazivanja rezervacija u odnosu na lead_time. Lead time predstavlja vreme proteklo od trenutka rezervacije do trenutka dolaska u hotel. Ispitaćemo kakva je podela prema tipu hotela, koliko je rezervacija otkazano, a koliko ne u zavisnosti od vremena.

ggplot(data = hotel_data, aes(
  x = hotel,
  y = lead_time,
  fill = factor(is_canceled)
)) +
  geom_boxplot(position = position_dodge()) +
  labs(
    title = "Status rezervacije",
    subtitle = "Vreme realizacije",
    x = "Tip hotela",
    y = "Vreme realizacije (dani)"
  ) +
  scale_fill_discrete(
    name = "Status rezervacije",
    breaks = c("0", "1"),
    labels = c("Nije otkazano", "Otkazano")
  ) + theme_light()

Ono što možemo videti sa grafika jeste da je otkazivanje rezervacija ređe ukoliko je prošlo manje vremena od datuma rezervacije. Na to može uticati i politika hotela, odnosno nemogućnost otkazivanja rezervacije ukoliko je vreme prijema uskoro. Isti standard primećen je u oba tipa hotela.

Sada ćemo videti kakva je raspodela otkazivanja bila po godinama. Kao što je već u opisu seta rečeno, dataset sadrži vrmenski period koji je zahvatio tri godine.

unique(hotel_data$arrival_date_year)
## [1] 2015 2016 2017
ggplot(data = hotel_data,
       aes(
         x = arrival_date_year,
         y = prop.table(stat(count)),
         fill = factor(is_canceled),
         label = scales::percent(prop.table(stat(count)))
       )) +
  geom_bar(position = position_dodge()) +
  geom_text(
    stat = "count",
    position = position_dodge(.9),
    vjust = -0.5,
    size = 3
  ) +
  scale_y_continuous(labels = scales::percent) +
  labs(title = "Status rezervacije u zavisnosti od godine",
       x = "Tip hotela",
       y = "Zastupljenost") +
  theme_classic() +
  scale_fill_discrete(
    name = "Status rezervacije",
    breaks = c("0", "1"),
    labels = c("Nije otkazano", "Otkazano")
  )

Primetimo da je 2016. bio značajno veći broj rezervacija, a to objašnjava činjenica da nisu u istraživanje obuhvaćene cele 2015. i 2017. godina. Da bismo imali precizniji uvid, posmatraćemo status rezervacija po mesecima.

hotel_data$arrival_date_month <-
  factor(hotel_data$arrival_date_month, levels = month.name)

ggplot(data = hotel_data, aes(x = arrival_date_month)) +
  geom_bar(fill = "steelblue") +
  geom_text(stat = "count", aes(label = ..count..), hjust = 1) +
  coord_flip() + labs(title = "Status rezervacije (po mesecima)",
                      x = "Mesec",
                      y = "Zastupljenost") +
  theme_classic()

Vidimo da je najviše rezervacija napravljeno tokom jula i avgusta, kao i tokom maja i oktobra. Pretpostavlja se da je jedan od faktora i vreme, pošto je prema vremenskim prognozama najprijatnije vreme u Portugalu baš tokom navedenih meseci. Ispitaćemo kakav je raspored otkazivanja i prijema u toku navedenih meseci.

ggplot(hotel_data, aes(arrival_date_month, fill = hotel)) +
  geom_bar(position = position_dodge()) +
  labs(title = "Status rezervacije (po mesecima)",
       x = "Mesec",
       y = "Zastupljenost") + facet_wrap(~arrival_date_year)+ 
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

Ispitujemo iz kojih država su prispele rezervacije turista.

length(unique(hotel_data$country))
## [1] 178
library(countrycode)

hotel_data$county_name <- countrycode(hotel_data$country, 
                                     origin = "iso3c",
                                     destination = "country.name")
## Warning in countrycode(hotel_data$country, origin = "iso3c", destination = "country.name"): Some values were not matched unambiguously: CN, NULL, TMP
ggplot(hotel_data, aes(county_name, fill = hotel)) + 
  geom_bar(stat = "count", position = position_dodge()) + 
  labs(title = "Status rezervacije (po drzavama)",
       x = "Drzava",
       y = "Zastupljenost") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1),
        panel.background = element_blank())

Kako imamo veliki broj država iz kojih su izvršene rezervacije, posmatraćemo sve one iz kojih je bilo više od 500 rezervacija.

hotel_data <- hotel_data %>% 
  group_by(country) %>% 
  filter(n() > 500)

length(unique(hotel_data$country))
## [1] 20

Države u setu podataka nisu navedene po nazivu već pomoću ISO 3155–3:2013 koda, pa ćemo pomoću paketa countrycode prevesti kod u naziv kako bismo videli odakle dolazi najveći broj turista.

library(countrycode)

hotel_data$county_name <- countrycode(hotel_data$country, 
                                     origin = "iso3c",
                                     destination = "country.name")
## Warning in countrycode(hotel_data$country, origin = "iso3c", destination = "country.name"): Some values were not matched unambiguously: CN
ggplot(hotel_data, aes(county_name, fill = hotel)) + 
  geom_bar(stat = "count", position = position_dodge()) + 
  labs(title = "Status rezervacije (po drzavama)",
       x = "Drzava",
       y = "Zastupljenost") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1),
        panel.background = element_blank())

Vidimo da je najveći broj rezervacija iz Portugala, što nije iznenađujuće jer se i sami hoteli nalaze u Portugalu. Osim toga, najveći broj turista je iz Velike Britanije, Francuske, Nemačke i Španije. Kod svih osim kod turista iz Velike Britanije i Irske veća je stopa rezervacija u gradskim hotelima. Kod turista iz Nemačke i Francuske je posebno izražena stopa rezervacija u gradskim hotelima, dok je kod turista iz Velike Britanije i Irske posebno izražena stopa rezervacija u odmaralištima pa hoteli mogu naći način da zainteresuju turiste iz drugih zemalja ili pak naći način da zadrže turiste koji se već opredejuju za njihov tip hotela.

ggplot(hotel_data, aes(x = adr,  fill = hotel, color = hotel)) + 
  geom_density(alpha = 0.2) + 
  labs(title = "Prosecna dnevna cena smestaja",
       x = "Cena (u eurima)",
       y = "Zastupljenost")  + 
  theme_classic() + theme(legend.position = "top")

Kako je grafik prilično zbijen, uklonićemo netipične vrednosti da bismo dobili reprezentativniji uzorak.

max(hotel_data$adr)
## [1] 5400
hotel_data <- hotel_data[hotel_data$adr != 5400,]

Pogledaćemo da li je imalo efekta uklanjanje samo jedne vrednosti.

ggplot(data = hotel_data, aes(
  x = hotel,
  y = adr,
  fill = factor(is_canceled)
)) +
  geom_boxplot(position = position_dodge()) +
  labs(
    title = "Prosecna cena za dan",
    x = "Tip hotela",
    y = "Cena"
  ) +
  scale_fill_discrete(
    name = "Status rezervacije",
    breaks = c("0", "1"),
    labels = c("Nije otkazano", "Otkazano")
  ) + theme_light()

Ne postoji veća oscilacija u ceni.

Ispitaćemo koji tip turista vrši rezervacije i prikazaćemo podatke o ceni za svaki od tipova.

ggplot(hotel_data, aes(customer_type, fill = hotel)) + 
  geom_bar(stat = "count", position = position_dodge()) + 
  labs(title = "Tip hotela u zavisnosti od tipa korisnika",
       x = "Tip korisnika",
       y = "Zastupljenost") +
  theme(axis.text.x = element_text(angle = 90),
        panel.background = element_blank())

ggplot(hotel_data, aes(x = customer_type, y = adr, fill = hotel)) + 
  geom_boxplot(position = position_dodge()) + 
  labs(
       x = "Tip korisnika",
       y = "Cena nocenja (u eurima)") + theme_classic()

Najviše rezervacija je tipa transient, a to su prolazni turisti koji se u smeštaju kratko zadržavaju. Veće oscilacije u ceni nema.Sve kategorije turista se radije opredeljuju za gradski tip hotela.

Proverićemo kakvo je bilo opredeljenje turista kojima je umesto rezervisane sobe dodeljena soba drugog tipa.

df <- subset(hotel_data, 
             as.character(hotel_data$reserved_room_type) != as.character(hotel_data$assigned_room_type))

table(df$is_canceled)
## 
##     0     1 
## 13222   766

Većinski nije uticalo na status rezervacije.

Modeli mašinskog učenja

Podela skupa podataka na training i test

Prilikom podele skupa na train i test skup, nećemo uključiti atribute company i reservation_status.

sample_size = floor(0.8*nrow(hotel_data))
train_ind = sample(seq_len(nrow(hotel_data)), size = sample_size)

train = hotel_data[train_ind,]
test = hotel_data[-train_ind,]

train <- train  %>%  dplyr::select(-company)
test <- test  %>%  dplyr::select(-company)
str(train)
## Classes 'grouped_df', 'tbl_df', 'tbl' and 'data.frame':  89614 obs. of  15 variables:
##  $ hotel                  : Factor w/ 2 levels "City Hotel","Resort Hotel": 1 1 1 1 1 2 1 2 2 1 ...
##  $ is_canceled            : num  0 0 0 0 1 0 1 1 0 0 ...
##  $ lead_time              : num  123 8 46 0 121 178 27 12 1 47 ...
##  $ arrival_date_year      : num  2015 2017 2016 2015 2016 ...
##  $ arrival_date_month     : Factor w/ 12 levels "January","February",..: 8 5 11 8 6 8 4 10 8 10 ...
##  $ stays_in_weekend_nights: num  2 0 0 0 1 2 1 2 2 2 ...
##  $ stays_in_week_nights   : num  4 4 2 1 2 5 2 2 0 3 ...
##  $ country                : Factor w/ 178 levels "ABW","AGO","AIA",..: 44 137 44 137 137 137 137 137 137 30 ...
##  $ customer_type          : Factor w/ 4 levels "Contract","Group",..: 3 4 3 3 3 3 3 3 3 3 ...
##  $ agent                  : Factor w/ 334 levels "1","10","103",..: 127 334 127 334 151 165 324 103 103 305 ...
##  $ reservation_status     : Factor w/ 3 levels "Canceled","Check-Out",..: 2 2 2 2 1 2 1 1 2 2 ...
##  $ adr                    : num  63.8 160 67.5 75 90 ...
##  $ reserved_room_type     : Factor w/ 10 levels "A","B","C","D",..: 1 1 1 1 1 1 1 1 1 4 ...
##  $ assigned_room_type     : Factor w/ 12 levels "A","B","C","D",..: 5 1 1 1 2 1 1 1 1 4 ...
##  $ county_name            : chr  "Germany" "Portugal" "Germany" "Portugal" ...
##  - attr(*, "groups")=Classes 'tbl_df', 'tbl' and 'data.frame':   20 obs. of  2 variables:
##   ..$ country: Factor w/ 178 levels "ABW","AGO","AIA",..: 13 16 26 30 32 35 44 52 57 60 ...
##   ..$ .rows  :List of 20
##   .. ..$ : int  89 160 206 210 273 320 353 409 449 563 ...
##   .. ..$ : int  48 65 187 198 276 282 326 558 599 608 ...
##   .. ..$ : int  26 64 101 205 284 306 348 371 392 416 ...
##   .. ..$ : int  10 75 175 196 274 315 420 434 546 614 ...
##   .. ..$ : int  144 145 148 200 227 231 342 367 440 544 ...
##   .. ..$ : int  25 168 447 448 489 678 699 725 760 840 ...
##   .. ..$ : int  1 3 15 16 18 56 59 61 96 106 ...
##   .. ..$ : int  49 58 84 91 121 125 126 150 154 171 ...
##   .. ..$ : int  29 43 57 62 67 78 79 95 105 118 ...
##   .. ..$ : int  12 20 23 30 37 38 45 54 55 60 ...
##   .. ..$ : int  74 129 131 244 248 281 322 331 350 357 ...
##   .. ..$ : int  443 638 838 1430 1714 1889 1966 1980 2043 2057 ...
##   .. ..$ : int  31 42 94 102 119 130 146 228 266 280 ...
##   .. ..$ : int  71 92 147 157 179 191 221 292 299 336 ...
##   .. ..$ : int  688 1063 1078 1112 1113 1173 1664 2084 2194 2370 ...
##   .. ..$ : int  87 308 894 1288 1463 1896 2041 2169 2452 2654 ...
##   .. ..$ : int  2 4 5 6 7 8 9 11 14 17 ...
##   .. ..$ : int  209 363 427 469 495 731 921 979 985 1236 ...
##   .. ..$ : int  32 296 301 484 519 547 562 652 667 783 ...
##   .. ..$ : int  13 39 108 194 283 289 318 319 368 388 ...
##   ..- attr(*, ".drop")= logi TRUE
str(test)
## Classes 'grouped_df', 'tbl_df', 'tbl' and 'data.frame':  22404 obs. of  15 variables:
##  $ hotel                  : Factor w/ 2 levels "City Hotel","Resort Hotel": 2 2 2 2 2 2 2 2 2 2 ...
##  $ is_canceled            : num  0 0 0 0 0 0 1 1 0 0 ...
##  $ lead_time              : num  7 72 72 72 127 99 45 40 113 14 ...
##  $ arrival_date_year      : num  2015 2015 2015 2015 2015 ...
##  $ arrival_date_month     : Factor w/ 12 levels "January","February",..: 7 7 7 7 7 7 7 7 7 7 ...
##  $ stays_in_weekend_nights: num  0 2 2 2 2 2 1 1 2 0 ...
##  $ stays_in_week_nights   : num  4 4 4 4 5 5 3 3 5 2 ...
##  $ country                : Factor w/ 178 levels "ABW","AGO","AIA",..: 60 137 137 137 60 137 137 137 125 171 ...
##  $ customer_type          : Factor w/ 4 levels "Contract","Group",..: 3 3 3 3 1 3 3 3 4 3 ...
##  $ agent                  : Factor w/ 334 levels "1","10","103",..: 113 113 113 113 13 103 104 104 47 105 ...
##  $ reservation_status     : Factor w/ 3 levels "Canceled","Check-Out",..: 2 2 2 2 2 2 1 1 2 2 ...
##  $ adr                    : num  153 84.7 84.7 99.7 95 ...
##  $ reserved_room_type     : Factor w/ 10 levels "A","B","C","D",..: 7 1 1 4 4 4 4 4 5 1 ...
##  $ assigned_room_type     : Factor w/ 12 levels "A","B","C","D",..: 7 1 1 4 9 4 4 4 5 3 ...
##  $ county_name            : chr  "United Kingdom" "Portugal" "Portugal" "Portugal" ...
##  - attr(*, "groups")=Classes 'tbl_df', 'tbl' and 'data.frame':   20 obs. of  2 variables:
##   ..$ country: Factor w/ 178 levels "ABW","AGO","AIA",..: 13 16 26 30 32 35 44 52 57 60 ...
##   ..$ .rows  :List of 20
##   .. ..$ : int  378 394 495 1689 1749 1750 1753 1754 1940 1941 ...
##   .. ..$ : int  406 471 472 607 914 1650 1652 1660 1764 1808 ...
##   .. ..$ : int  319 1056 1073 1413 1581 1611 1612 1720 1778 1863 ...
##   .. ..$ : int  334 1173 1226 1365 1407 1436 1842 2022 2112 2162 ...
##   .. ..$ : int  345 744 3014 3883 3886 3954 4274 4463 5253 5254 ...
##   .. ..$ : int  26 55 125 212 249 361 405 819 1387 1450 ...
##   .. ..$ : int  24 297 318 323 374 547 559 719 930 931 ...
##   .. ..$ : int  14 36 53 54 58 59 62 64 65 78 ...
##   .. ..$ : int  18 23 185 220 226 262 283 333 382 383 ...
##   .. ..$ : int  1 5 15 28 29 32 35 43 49 72 ...
##   .. ..$ : int  40 48 69 105 164 214 216 222 260 266 ...
##   .. ..$ : int  1379 2399 2503 2504 2575 2651 5236 5492 7874 7949 ...
##   .. ..$ : int  33 57 282 568 1272 1342 1500 1678 1908 2023 ...
##   .. ..$ : int  193 259 327 331 364 393 529 601 602 752 ...
##   .. ..$ : int  9 136 1145 2190 2483 2484 2617 3046 3336 4195 ...
##   .. ..$ : int  12 70 234 579 683 1275 1282 1328 1329 1330 ...
##   .. ..$ : int  2 3 4 6 7 8 13 17 19 20 ...
##   .. ..$ : int  1297 1435 1565 1818 2054 2225 2226 2419 2427 2471 ...
##   .. ..$ : int  1308 1434 1783 1784 1785 1786 1787 1788 1789 1790 ...
##   .. ..$ : int  10 11 16 45 63 138 175 293 1239 1374 ...
##   ..- attr(*, ".drop")= logi TRUE
train <- train  %>% dplyr::select(-reservation_status)
test <- test  %>% dplyr::select(-reservation_status)

Vidimo da je struktura trening i test skupa ista.

Stablo odlučivanja

mod_tree <- rpart(is_canceled ~ ., data = train, method = "class")
fancyRpartPlot(mod_tree)

printcp(mod_tree)
## 
## Classification tree:
## rpart(formula = is_canceled ~ ., data = train, method = "class")
## 
## Variables actually used in tree construction:
## [1] country   lead_time
## 
## Root node error: 33705/89614 = 0.37611
## 
## n= 89614 
## 
##        CP nsplit rel error  xerror      xstd
## 1 0.18881      0   1.00000 1.00000 0.0043024
## 2 0.01000      2   0.62237 0.62344 0.0037629

Na dobijenom grafiku vidimo koje varijable u najvećoj meri utiču na predviđanje.

mod_tree$variable.importance
##               lead_time                 country                   agent 
##               5218.5533               5128.6723               3268.8513 
##                     adr       arrival_date_year    stays_in_week_nights 
##               1259.5645                573.5828                439.0300 
## stays_in_weekend_nights      assigned_room_type      arrival_date_month 
##                386.8650                170.3067                126.8651
tree.pred <- predict(mod_tree, test, type = "class")

conf <- table(tree.pred, test$is_canceled)

Pomoću podataka iz matrice konfuzije, videćemo kolika je preciznost našeg modela.

sum(diag(conf)) / sum(conf)
## [1] 0.7626317

Naivni Bayes

library(e1071)
## Warning: package 'e1071' was built under R version 3.6.3
nb_model = naiveBayes(as.factor(is_canceled) ~., data=train)

modelPred <- predict(nb_model, test$is_canceled)
## Warning in predict.naiveBayes(nb_model, test$is_canceled): Type mismatch between
## training and new data for variable 'lead_time'. Did you use factors with numeric
## labels for training, and numeric values for new data?
## Warning in predict.naiveBayes(nb_model, test$is_canceled): Type mismatch between
## training and new data for variable 'arrival_date_year'. Did you use factors with
## numeric labels for training, and numeric values for new data?
## Warning in predict.naiveBayes(nb_model, test$is_canceled): Type mismatch between
## training and new data for variable 'stays_in_weekend_nights'. Did you use
## factors with numeric labels for training, and numeric values for new data?
## Warning in predict.naiveBayes(nb_model, test$is_canceled): Type mismatch between
## training and new data for variable 'stays_in_week_nights'. Did you use factors
## with numeric labels for training, and numeric values for new data?
## Warning in predict.naiveBayes(nb_model, test$is_canceled): Type mismatch between
## training and new data for variable 'adr'. Did you use factors with numeric
## labels for training, and numeric values for new data?
cMatrix <- table(modelPred, test$is_canceled)

plot(cMatrix)

confusionMatrix(cMatrix)
## Confusion Matrix and Statistics
## 
##          
## modelPred     0     1
##         0 14047  8357
##         1     0     0
##                                           
##                Accuracy : 0.627           
##                  95% CI : (0.6206, 0.6333)
##     No Information Rate : 0.627           
##     P-Value [Acc > NIR] : 0.503           
##                                           
##                   Kappa : 0               
##                                           
##  Mcnemar's Test P-Value : <2e-16          
##                                           
##             Sensitivity : 1.000           
##             Specificity : 0.000           
##          Pos Pred Value : 0.627           
##          Neg Pred Value :   NaN           
##              Prevalence : 0.627           
##          Detection Rate : 0.627           
##    Detection Prevalence : 1.000           
##       Balanced Accuracy : 0.500           
##                                           
##        'Positive' Class : 0               
## 

Ukoliko bi trebalo da izaberemo koji algoritam za predviđanje ćemo koristiti, odabrali bismo stablo odlučivanja jer ima veću preciznost (accuracy).