Loading in Data

Data for the problem set is loaded here.

airbnb <- read.csv("airbnb.csv")

Problems

Problem 1

Inspect the data file with your own eyes for a moment. Either by opening up it in Excel or looking through it variable by variable in R. Briefly summarize what you see and what variables you find. [2 points]

  • This data file is for the Los Angeles Airbnb. It indicates the host’s names, location, room type, prices and host’s ID.

Problem 2

How many observations and variables are in the Airbnb data set? [2 points]

  • Observation = 44594
  • Variables = 15
dim(airbnb)
[1] 44594    15

Problem 3

Let’s look at the neighborhood variable. How many different neighborhoods show up in the data? Compute a frequency table for the neighborhoods of Los Angeles in the dataset. Which neighborhood has the most listings? How many listings does Burbank have? [2.5 points]

unique_neighborhoods <- unique(airbnb$neighborhood)
length(unique_neighborhoods)
[1] 266
freq_table <- table(airbnb$neighborhood)
which.max(freq_table)
Hollywood 
      103 
freq_table["Burbank"]
Burbank 
    539 

Problem 4

The dataset also includes some information on the “type” of room(s) that are in the rental, in the variable room_type. How many different room types do there seem to be in the data? There is also a more broad neighborhood_group variable. How many different areas are there in this variable? Finally, compute a two-way frequency table for these two variables. Which total is the largest in this two-way frequency table and what is that count? [2.5 points]

  • 4 types of room types
  • 3 different areas
  • largest total in a two-way frequency table is 32863
length(unique(airbnb$room_type))
[1] 4
length(unique(airbnb$neighborhood_group))
[1] 3
table(airbnb$room_type,airbnb$neighborhood_group)
                 
                  City of Los Angeles Other Cities Unincorporated Areas
  Entire home/apt               17294        12799                 2770
  Hotel room                       17           53                    0
  Private room                   4410         5171                 1325
  Shared room                     538          172                   45

Problem 5

The dataset includes some pricing information, too, in the price variable. What is the average price per night in the data? What is the median price? Answer the same questions for the minimum number of nights required in the reservation (minimum_nights). Has answering this question made you skeptical at all about any of the data points in the data? Why? [2 points]

  •  Kinda. The mean and median can give an idea about the central tendency of the data. If there's any outliers then it would be concerning about how reliable the data points are, but I think it is better to have a visualized boxplot or scatterplot to understand the data more than just numbers.
average_minimum_nights <- mean(airbnb$minimum_nights, na.rm = TRUE)
median_minimum_nights <- median(airbnb$minimum_nights, na.rm = TRUE)

mean(airbnb$price)
[1] 291.8501
median(airbnb$price)
[1] 150
median(airbnb$minimum_nights)
[1] 7

Problem 6

Suppose you are interested in finding rentals that allow bookings for one or two nights. Create a new variable two_nights that is TRUE for rentals that allow one or two night bookings and FALSE otherwise. What percentage of the rentals meet this criterion? Compute a two-way frequency table between your new variable and neighborhood_group. What percentage of the data are in Los Angeles and have a minimum number of nights requirement of two or less? [3 points] City of Los Angeles - 3801/18458+3801= 0.17076239 = 17.076%

  • 16224/28370+16224= 0.36381576 = 36.381%
two_nights <- airbnb$minimum_nights <= 2

table(airbnb$neighborhood_group,two_nights)
                      two_nights
                       FALSE  TRUE
  City of Los Angeles  18458  3801
  Other Cities          8551  9644
  Unincorporated Areas  1361  2779
summary(two_nights)
   Mode   FALSE    TRUE 
logical   28370   16224 

Problem 7

What is the most number of reviews for a listing in the data (look at number_of_reviews)? In what neighborhood is this listing? What room type is it? [2.5 points]

max_reviews <- subset(airbnb,number_of_reviews == max(number_of_reviews))
max_reviews$number_of_reviews
[1] 2472
max_reviews$neighborhood
[1] "Westlake"
max_reviews$room_type
[1] "Entire home/apt"

Problem 8

What is the median price for a rental in Pasadena? What is the median price for a rental in Santa Monica? [2 points]

  • Median price for Pasadena is 165
  • Median price for Santa Monica is 185
Pasadena <- subset(airbnb, neighborhood=="Pasadena")

median(Pasadena$price)
[1] 165
Santamonica <- subset(airbnb, neighborhood=="Santa Monica")

median(Santamonica$price)
[1] 185

Problem 9

Is there a higher percentage of “entire home” Airbnb rentals in Pasadena or Santa Monica? (Hint: you will probably want to use table() and prop.table() to answer this efficiently.) [2.5 points] Santa Monica has a high percentage of ‘Entire home’ than Santa Monica.

table(airbnb$room_type,airbnb$neighborhood== "Santa Monica")
                 
                  FALSE  TRUE
  Entire home/apt 31754  1109
  Hotel room         69     1
  Private room    10669   237
  Shared room       749     6
table(airbnb$room_type,airbnb$neighborhood== "Pasadena")
                 
                  FALSE  TRUE
  Entire home/apt 32365   498
  Hotel room         70     0
  Private room    10729   177
  Shared room       751     4
percentage_entire_home_pasadena <-mean(Pasadena$room_type == "Entire home/apt") * 100

Problem 10

This question asks you to build a couple of graphics using ggplot2. First, load in the Lahman package of baseball data, specifically the “Teams” part of the data. Select all years after 2000. Convert this to a tibble to ensure the format works. You can then construct a boxplot of home runs (HR), where each year is separated out as its own boxplot in the figure. Convert the boxplots to be horizontal and use the fill option to color them by year. (Hint: you will need to use the yearID variable and you may need to convert it to a factor variable.) What are some takeaways for you from this plot? [3 points]

  • Some takeaways from this plot is that you can see how the distribution of home runs varies across the different years. You can see the patterns in the home run performance over time. Lastly, you can see any outliers in specific years that may stand out in the home run scores.
library(Lahman)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.0
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
teams_data <- Lahman::Teams

teams_data_filtered <- teams_data %>% filter(yearID > 2000)

teams_data_tibble <- as_tibble(teams_data_filtered)

teams_data_tibble$yearID <- as.factor(teams_data_tibble$yearID)

ggplot(teams_data_tibble,aes(x = HR, y = yearID, fill= yearID)) + geom_boxplot() + labs(title = "Boxplot of Home Runs (HR) by Year", x = "Home Runs", y = "Years") + theme_minimal()

Problem 11

Construct the same boxplot figure you did above, but replace home runs with attendance. What can you infer from this figure? For the year 2019, now, construct a scatterplot using ggplot2. Compare the two variables we’ve been looking at here, home runs (HR) and attendance. You can place HR on the horizontal axis and attendance on the vertical axis. Each dot should be for a different team. Please also use the plotting option to allow all National League (NL) teams to be one color and all American League (AL) teams to be another color. What can you infer from this scatterplot? [3 points]

  • Based on the scatterplot, you can look for the cluster and the color differentation to help distinguish the two teams. It allows us to identify any trends/variation in attendance over time and the relationship between home runs and attandance for 2019.
teams_data_filtered <- teams_data %>% filter(yearID == 2019)

teams_data_2019 <- as_tibble(teams_data_filtered)

ggplot(teams_data_2019, aes(x = HR, y = attendance, color = lgID)) + geom_point() + labs(title = "Scatterplot of Home Runs vs Attendance (2019)",x = "Home Runs",y = "Attendance") + scale_color_manual(values = c("NL" = "pink", "AL" = "blue")) + theme_minimal()

Problem 12

Let’s expand the scatterplot you made for 2019 to include multiple years. Use 2011 to 2019, for a total of nine years. Create a single plot with all nine years (you will probably want to use facet_wrap()), using three years per row. What can you infer from this new plot across the nine years? Next, make the same plot, but replace home runs with wins (W). How does this change the plot? [3 points]

  • By replacing home runs with win it changes the plots insights into the dynamics of the teams performance that is measured by home runs or win of fans attendance over the nine year period. The Home Run plot observe how the relationship between home runs and attendance evolve across the years and on the other hand, the wins plot examine how the relationship of wins and attendance varies over the years.
teams_data_multiple_years <- teams_data_tibble %>% filter(yearID %in% c(2011:2019))

ggplot(teams_data_multiple_years, aes(x = W, y = attendance, color = lgID)) + geom_point() + facet_wrap(~yearID, nrow = 3) + labs(title = "Scatterplot of Home Runs vs Attendance (2011-2019)", x = "Home Run", y = "Attendance") + scale_color_manual(values = c("NL"= "green", "AL" = "orange")) + theme_minimal()

teams_data_multiple_years <- teams_data_tibble %>% filter(yearID %in% c(2011:2019))

ggplot(teams_data_multiple_years, aes(x = W, y = attendance, color = lgID)) + geom_point() + facet_wrap(~yearID, nrow = 3) + labs(title = "Scatterplot of Wins vs Attendance (2011-2019)",x = "Wins", y = "Attendance") + scale_color_manual(values = c("NL" = "orange", "AL" = "green")) + theme_minimal()