---
title: "Case Study: Hotel Bookings with Children"
author: "Keith VanderLinden"
date: "Spring, 2022"
output: html_document
---

```{r warning=FALSE, message=FALSE}
library(tidyverse)
```

This document describes a simple analysis of the hotel booking dataset. 
This example is adapted from A. Scotina&rsquo;s [Taking a Vacation with #TidyTuesday hotel bookings](https://scotinastats.rbind.io/post/hotel-bookings-tidymodels/), with an emphasis on data wrangling. Our interest is in understanding booking patterns over the the course of the year.

## Loading the Hotel Bookings Dataset

We start by reloading the [TidyTuesday Hotel Dataset](https://github.com/rfordatascience/tidytuesday/blob/master/data/2020/2020-02-11/readme.md). The dataset is available here from TidyTuesday-GitHub: [hotels.csv](https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-02-11/hotels.csv), but it&rsquo;s only 16K so we&rsquo;ll download it to reduce traffic on the server.

```{r warning=FALSE, message=FALSE}
hotels_original <- read_csv("data/hotels.csv")
glimpse(hotels_original)
```

We can see that date of the booking is split into multiple columns: `arrival_date_year`, `arrival_date_month`, `arrival_date_day_of_month`. 

## Wrangling Dates

To plot a time-series showing the distribution of bookings over time, we&rsquo;ll need to create a full date that can be sequenced. We&rsquo;ll use `lubridate`.

```{r warning=FALSE, message=FALSE}
library(lubridate)

hotels <- hotels_original %>%
  mutate(full_date = ymd(paste(arrival_date_year, 
                               arrival_date_month, 
                               arrival_date_day_of_month
                               )))

hotels %>%
  select(full_date, arrival_date_year, arrival_date_month, arrival_date_day_of_month)
```
This concatenated the year-month-day elements of the data together and used `ymd()` to produce a date object that can be sequenced chronologically. Here&rsquo;s a density plot of the bookings over time.

```{r warning=FALSE, message=FALSE}
hotels %>%
  ggplot() +
  aes(x = full_date) +
  geom_density()
```

For a more general analysis, it would be better to group the months together so that we can see annual trends. To support this, we&rsquo;ll factorize the month character vector, count the bookings (i.e., group-by and sum), and then plot the bookings count for each month.

```{r warning=FALSE, message=FALSE}
hotels <- hotels %>%
    mutate(arrival_date_month_factor = 
               fct_recode(arrival_date_month))

hotels %>%
  count(arrival_date_month_factor, hotel) %>%
  ggplot() + 
  aes(x = arrival_date_month_factor, y = n, fill = hotel) +
  geom_col() +
  labs(x = NULL, y = "Frequency", fill = NULL) +
  coord_flip() 
```

Alas, the months are ordered alphabetically, not chronologically. We&rsquo;ll rebuild the month factor, this time &ldquo;re-leveling&rdquo; (i.e., ordering) according to the chronological order of the months.

```{r warning=FALSE, message=FALSE}
hotels <- hotels %>%
    mutate(arrival_date_month_factor = 
             fct_relevel(
               fct_recode(arrival_date_month),
               levels = month.name))

hotels %>%
  count(arrival_date_month_factor, hotel) %>%
  ggplot() + 
  aes(x = fct_rev(arrival_date_month_factor), y = n, fill = hotel) +
  geom_col() +
  labs(x = NULL, y = "Frequency", fill = NULL) +
  coord_flip() 
```

`month.name` is a character vector that lists the month names (in English) in order: `r month.name`.

It appears that there are fewer bookings in winter (December-February) overall.

An alternative way to get the months:

```{r}
hotels <- hotels %>%
    mutate(arrival_date_month_factor = 
               month(full_date, label = TRUE, abbr = FALSE))
```

## Checking Booking Patterns with and without Children

Here, add a column indicating if there are any children in the party and then plot the booking patterns with and without children. This code uses a conditional control structure implemented by the `ifelse` function, which we&rsquo;ll discuss later in the course.

```{r warning=FALSE, message=FALSE}
hotels <- hotels %>%
  mutate(
    any_kids = ifelse(children + babies > 0, 
                      "w/ children", 
                      "w/o children")
    )

hotels %>%
  select(any_kids, adults, children, babies)
```

We can see here that some of the entries for children and babies are `NA`, which leads to `NA` values for `any_kids`, which then leads to problems in the graphing. 

```{r warning=FALSE, message=FALSE}
hotels %>%
  select(any_kids, adults, children, babies) %>%
  filter(is.na(any_kids))
```

We didn&rsquo;t notice this at first, but it led to issues in the graphing, so we redid this mutation to simply drop the (four) offending records.

```{r}
hotels <- hotels %>%
  filter(!is.na(children), !is.na(babies)) %>%
  mutate(
    any_kids = ifelse(children + babies > 0, 
                      "w/ children", 
                      "w/o children")
    )

hotels %>%
  select(any_kids, adults, children, babies) %>%
  filter(is.na(any_kids))
```

Now, we plot the annual distribution of bookings with and without children.

```{r warning=FALSE, message=FALSE}
hotels %>%
  count(arrival_date_month_factor, hotel, any_kids) %>%
  group_by(hotel, any_kids) %>%
  mutate(prop = n/sum(n)) %>%
  ggplot() +
  aes(x = fct_rev(arrival_date_month_factor), 
      y = prop, 
      fill = hotel) +
  geom_col(position = "dodge") +
  coord_flip() +
  scale_y_continuous(labels = scales::percent_format()) +
  labs(x = NULL, y = "Percent of hotel stays", fill = NULL) +
  facet_wrap( ~ any_kids)
```

Here, we see that bookings with children are much higher in the summer months and that bookings without children are more evenly distributed.

## Summary

We&rsquo;ve demonstrated that there are tends in the annual distribution of bookings at the two different hotels, plotting both a continuous time-series over date and a binned time-series over the months of the year. This work required the use of a variety of the `dplyr` wrangling commands.

A. Scotina goes on to bin the bookings over season rather than month and then to use those categories to train a predictive model for hotel bookings. Training a sufficiently-predictive model over continuous time or even over months, is difficult, so the seasonal binning was important.
