Once upon a time, people traveled all over the world, and some stayed in hotels and others chose to stay in other people’s houses that they booked through Airbnb. Recent developments in Edinburgh regarding the growth of Airbnb and its impact on the housing market means a better understanding of the Airbnb listings is needed. Using data provided by Airbnb, we can explore how Airbnb availability and prices vary by neighbourhood.

Getting started

Before we introduce the data, let’s warm up with some simple exercises.

Packages

We’ll use the tidyverse package for much of the data wrangling and visualization and the data lives in the dsbox package. You probably already have tidyverse installed. You will need to install dsbox, from github, as the package is not available for the most recent version of R. Once installed, you can comment (add #) this line of code and simply load the libraries into your environment, if needed.

# devtools::install_github("tidyverse/dsbox")

Load the packages by running the following:

library(tidyverse)
library(dsbox)
library(ggridges)

Submitting the lab

Instructions for code: There R chunks where you should write your code. For some exercises, you might save your answer as a particular variable. For example, we might give you a code chunk that looks like this:

set.seed(4291)
# insert code here save the median of your simulated data as 
# 'medx'

And you might complete it like so:

set.seed(4291)
# insert code here save the median of your simulated data as 
# 'medx'
x <- rnorm(1000)
medx <- median(x)
medx
## [1] 0.01612433

It is a good idea to put the variable name at the bottom so it prints (assuming its not a huge object), and usually this should be already part of the provided code. It also helps you check your work.

Of note: Sometimes an exercise will ask for code AND pose a question. Make sure that if the answer to the question is not an output of the code, then you must answer it separately in a non-code text box. For example the problem might ask you to make a plot and describe its prominent features. You would write the code to make the plot, but also write a sentence or two outside of the code block (plain text) to describe the features of the plot.

Submission: You must submit both the PDF and .Rmd to your submission folder on Google drive by the due date and time.

Data

The data can be found in the dsbox package, and it’s called edibnb. Since the dataset is distributed with the package, we don’t need to load it separately; it becomes available to us when we load the package.

You can view the dataset as a spreadsheet using the View() function. Note that you should not put this function in your R Markdown document, but instead type it directly in the Console, as it pops open a new window (and the concept of popping open a window in a static document doesn’t really make sense…). When you run this in the console, you’ll see the following data viewer window pop up.

View(edibnb)

You can find out more about the dataset by inspecting its documentation, which you can access by running ?edibnb in the Console or using the Help menu in RStudio to search for edibnb. You can also find this information here.

Exercises

Hint: The Markdown Quick Reference sheet has an example of inline R code that might be helpful. You can access it from the Help menu in RStudio.

  1. Run View(edibnb) in your Console to view the data in the data viewer.
  1. What does each row in the dataset represent?

Your non-coding answer:

# Each row represents a different Airbnb location in Edinburgh.

Each column represents a variable. b. Get a list of the variables in the data frame and their data types.

str(edibnb)
## spc_tbl_ [13,245 × 10] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ id                  : num [1:13245] 15420 24288 38628 44552 47616 ...
##  $ price               : num [1:13245] 80 115 46 32 100 71 175 150 139 190 ...
##  $ neighbourhood       : chr [1:13245] "New Town" "Southside" NA "Leith" ...
##  $ accommodates        : num [1:13245] 2 4 2 2 2 3 5 5 6 10 ...
##  $ bathrooms           : num [1:13245] 1 1.5 1 1 1 1 1 1 1 2 ...
##  $ bedrooms            : num [1:13245] 1 2 0 1 1 1 2 3 4 4 ...
##  $ beds                : num [1:13245] 1 2 2 1 1 2 3 4 5 7 ...
##  $ review_scores_rating: num [1:13245] 99 92 94 93 98 97 100 92 96 99 ...
##  $ number_of_reviews   : num [1:13245] 283 199 52 184 32 762 7 28 222 142 ...
##  $ listing_url         : chr [1:13245] "https://www.airbnb.com/rooms/15420" "https://www.airbnb.com/rooms/24288" "https://www.airbnb.com/rooms/38628" "https://www.airbnb.com/rooms/44552" ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   id = col_double(),
##   ..   price = col_double(),
##   ..   neighbourhood = col_character(),
##   ..   accommodates = col_double(),
##   ..   bathrooms = col_double(),
##   ..   bedrooms = col_double(),
##   ..   beds = col_double(),
##   ..   review_scores_rating = col_double(),
##   ..   number_of_reviews = col_double(),
##   ..   listing_url = col_character()
##   .. )

You can find descriptions of each of the variables in the help file for the dataset, which you can access by running ?edibnb in your Console.

  1. Computing summary statistics is always the first step in the exploratory analysis. The summaries may include average, median, maximum, minimum, etc. One simple method is to use the summary function.
  1. How many observations (rows) does the dataset have?
nrow(edibnb)
## [1] 13245
  1. Get key summary statistics for all numeric variables, and return them in a neatly organized table. The statistics should include the minimum, median, IQR, maximum, mean and standard deviation.
# Pulling only numeric variables from data
edibnb2 <- edibnb[sapply(edibnb, is.numeric)]

# Creating a function to pull each descriptive statistic from each variable
sumstat <- function(x) {
  return(c(minimum = min(x, na.rm = TRUE),
           maximum = max(x, na.rm = TRUE),
           mean = mean(x, na.rm = TRUE),
           median = median(x, na.rm = TRUE),
           IQR = IQR(x, na.rm = TRUE),
           stand_dev = sd(x, na.rm = TRUE)))}

# Applying the function and converting data to a table
table <- t(apply(edibnb2, 2, sumstat))
table <- as.data.frame(table)

# Converting mean & stand_dev values from scientific notation to regular numeric format
table$mean <- format(table$mean, scientific = FALSE)
table$stand_dev <- format(table$stand_dev, scientific = FALSE)

table
  1. Create a faceted histogram where each facet represents a neighborhood and displays the distribution of Airbnb prices in that neighborhood. Be sure to label your visualization. Think critically about whether it makes more sense to stack the facets on top of each other in a column, lay them out in a row, or wrap them around. Along with your visualization, include your reasoning for the layout you chose for your facets.

REMINDER A `ggplot2’ plot is comprised of three fundamental building blocks:

  1. Data: typically entered as a dataframe.
  2. aesthetics (aes): to define which columns from the data will be used along which axes, and map the values to colors, symbol size, etc.
  3. geom(s): to define the type of plot - point (geom_point), bars (geom_bar), lines (geom_line), etc.

`ggplot2’ works in layers. We can create a base layer, and then add additional layers to it. New layers can be added using “+” operator.

unique(edibnb$neighbourhood)
##  [1] "New Town"    "Southside"   NA            "Leith"       "Old Town"   
##  [6] "West End"    "Haymarket"   "Morningside" "Newington"   "Marchmont"  
## [11] "Cannonmills" "Tollcross"   "Bruntsfield" "Stockbridge"
# Plotting
plot <- edibnb |>
  ggplot(aes(x = price)) +
  geom_histogram(fill = "#CB7287", color = "#EFE4DC", binwidth = 20) +
  facet_wrap(~ neighbourhood, scales = "fixed") +
  labs(x = "Price",
       y = "Count",
       title = "Distribution of Airbnb Prices in Neighborhoods in Edinburgh, Scotland") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        strip.text = element_text(size = 8))
plot
## Warning: Removed 199 rows containing non-finite values (`stat_bin()`).

Your non-coding answer about reasoning for the layers and layout you chose.

# I decided to choose to stack the plots on top of one another simply because there are too many neighborhoods to put into a row or column. I believe it would have made more sense to place the plots side by side in a row had there not been so many neighborhoods.
  1. Your answer to this exercise will include three pipelines, and a narrative.
  1. Use a single pipeline to identify the neighbourhoods with the top five median listing prices.
top5median <- edibnb |>
  group_by(neighbourhood) |>
  summarize(medprice = median(price, na.rm = TRUE)) |>
  arrange(desc(medprice))
  
head(top5median, 5)
  1. Then, in another pipeline filter the data for these five neighbourhoods and make ridge plots of the distributions of listing prices in these five neighbourhoods.
# Filtering for top 5 median listing price neighborhoods
edibnb3 <- edibnb |>
  filter(neighbourhood %in% c("New Town", "Old Town", "West End", "Stockbridge", "Bruntsfield"))

# Creating a color palette
desiredcolors <- c("#CCA1C9", "#FFD3DD", "#F3A0AD", "#BED1E3", "#92A1C3")

# Plotting
plot2 <- edibnb3 |>
  ggplot(aes(x = price, y = neighbourhood, fill = neighbourhood)) +
  geom_density_ridges(color = "#FFF5EE", scale = 0.6) +
  scale_fill_manual(values = desiredcolors) +
  labs(title = "Distribution of Listing Prices in Top 5 Median (Listing Price) Neighbourhoods",
       x = "Price",
       y = "Neighbourhood") +
  theme_minimal() +
  theme(legend.position = "none")

plot2
## Warning: Removed 104 rows containing non-finite values
## (`stat_density_ridges()`).

  1. In a third pipeline calculate the minimum, mean, median, standard deviation, IQR, and maximum listing price in each of these neighbourhoods.
sumstat2 <- edibnb3 |>
  group_by(neighbourhood) |>
  summarize(minimum = min(price, na.rm = TRUE),
            maximum = max(price, na.rm = TRUE),
            mean = mean(price, na.rm = TRUE), 
            median = median(price, na.rm = TRUE),
            IQR = IQR(price, na.rm = TRUE),
            standard_deviation = sd(price, na.rm = TRUE))

sumstat2
  1. Use visualization in Exercise 3 and the summary statistics to describe the distribution of listing prices in the neighborhoods.

Your non-coding narrative here:

# It looks like New Town has the highest range from minimum to maximum price while Stockbridge has the lowest range. Understandably, New Town has the highest mean, median, and IQR. Old Town has the highest standard deviation, but not by a lot from New Town.