Step 2: Linear Regression Reading

At first I thought linear regression was mostly just used to make predictions and create a line on a graph. But now I think it can also help us understand how two variables are connected and how changes in one variable are related to changes in another. I also learned that regression can help us find patterns in data and see what factors might be useful when predicting an outcome.

Question: How can we tell if a relationship in a regression model is actually strong enough to matter?

Pre-Processing

Load the required libraries and Dodgers dataset.

library(lattice)
library(ggplot2)
library(readr)

DodgersData <- read_csv("DodgersData (1).csv")
## Rows: 81 Columns: 12
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (9): month, day_of_week, opponent, skies, day_night, cap, shirt, firewor...
## dbl (3): day, attend, temp
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Data Cleanup and Exploratory Analysis

# Check the structure of the data
str(DodgersData)
## spc_tbl_ [81 × 12] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ month      : chr [1:81] "APR" "APR" "APR" "APR" ...
##  $ day        : num [1:81] 10 11 12 13 14 15 23 24 25 27 ...
##  $ attend     : num [1:81] 56000 29729 28328 31601 46549 ...
##  $ day_of_week: chr [1:81] "Tuesday" "Wednesday" "Thursday" "Friday" ...
##  $ opponent   : chr [1:81] "Pirates" "Pirates" "Pirates" "Padres" ...
##  $ temp       : num [1:81] 67 58 57 54 57 65 60 63 64 66 ...
##  $ skies      : chr [1:81] "Clear" "Cloudy" "Cloudy" "Cloudy" ...
##  $ day_night  : chr [1:81] "Day" "Night" "Night" "Night" ...
##  $ cap        : chr [1:81] "NO" "NO" "NO" "NO" ...
##  $ shirt      : chr [1:81] "NO" "NO" "NO" "NO" ...
##  $ fireworks  : chr [1:81] "NO" "NO" "NO" "YES" ...
##  $ bobblehead : chr [1:81] "NO" "NO" "NO" "NO" ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   month = col_character(),
##   ..   day = col_double(),
##   ..   attend = col_double(),
##   ..   day_of_week = col_character(),
##   ..   opponent = col_character(),
##   ..   temp = col_double(),
##   ..   skies = col_character(),
##   ..   day_night = col_character(),
##   ..   cap = col_character(),
##   ..   shirt = col_character(),
##   ..   fireworks = col_character(),
##   ..   bobblehead = col_character()
##   .. )
##  - attr(*, "problems")=<pointer: 0x634af1588480>
# View first few rows
head(DodgersData)
## # A tibble: 6 × 12
##   month   day attend day_of_week opponent  temp skies  day_night cap   shirt
##   <chr> <dbl>  <dbl> <chr>       <chr>    <dbl> <chr>  <chr>     <chr> <chr>
## 1 APR      10  56000 Tuesday     Pirates     67 Clear  Day       NO    NO   
## 2 APR      11  29729 Wednesday   Pirates     58 Cloudy Night     NO    NO   
## 3 APR      12  28328 Thursday    Pirates     57 Cloudy Night     NO    NO   
## 4 APR      13  31601 Friday      Padres      54 Cloudy Night     NO    NO   
## 5 APR      14  46549 Saturday    Padres      57 Cloudy Night     NO    NO   
## 6 APR      15  38359 Sunday      Padres      65 Clear  Day       NO    NO   
## # ℹ 2 more variables: fireworks <chr>, bobblehead <chr>
# First 10 rows
head(DodgersData, 10)
## # A tibble: 10 × 12
##    month   day attend day_of_week opponent   temp skies  day_night cap   shirt
##    <chr> <dbl>  <dbl> <chr>       <chr>     <dbl> <chr>  <chr>     <chr> <chr>
##  1 APR      10  56000 Tuesday     Pirates      67 Clear  Day       NO    NO   
##  2 APR      11  29729 Wednesday   Pirates      58 Cloudy Night     NO    NO   
##  3 APR      12  28328 Thursday    Pirates      57 Cloudy Night     NO    NO   
##  4 APR      13  31601 Friday      Padres       54 Cloudy Night     NO    NO   
##  5 APR      14  46549 Saturday    Padres       57 Cloudy Night     NO    NO   
##  6 APR      15  38359 Sunday      Padres       65 Clear  Day       NO    NO   
##  7 APR      23  26376 Monday      Braves       60 Cloudy Night     NO    NO   
##  8 APR      24  44014 Tuesday     Braves       63 Cloudy Night     NO    NO   
##  9 APR      25  26345 Wednesday   Braves       64 Cloudy Night     NO    NO   
## 10 APR      27  44807 Friday      Nationals    66 Clear  Night     NO    NO   
## # ℹ 2 more variables: fireworks <chr>, bobblehead <chr>

Practice: 20th Home Game

DodgersData[20, c("temp", "attend", "opponent", "bobblehead")]
## # A tibble: 1 × 4
##    temp attend opponent bobblehead
##   <dbl>  <dbl> <chr>    <chr>     
## 1    70  47077 Snakes   YES

Average Attendance

meanattend <- mean(DodgersData$attend)

meanattend
## [1] 41040.07

Number of Bobblehead Promotions

promotions <- sum(DodgersData$bobblehead == "YES")

promotions
## [1] 11

Discussion Questions

Q1a: Let R identify the temperature, the attendance, the opponent, and the promotion for the 25th home game of the season.

DodgersData[25, c("temp", "attend", "opponent", "bobblehead")]
## # A tibble: 1 × 4
##    temp attend opponent bobblehead
##   <dbl>  <dbl> <chr>    <chr>     
## 1    61  36561 Astros   NO

Answer:

The 25th Dodgers home game had a temperature of 61°F with 36,561 people in attendance. The Dodgers played against the Astros, and there was no bobblehead promotion.

Q1b: What is the median value of attendance?

medianattend <- median(DodgersData$attend)

medianattend
## [1] 40284

Answer:

The median attendance was 40,284 people. This means that half of the Dodgers home games had attendance below 40,284 and the other half had attendance above 40,284.

Q1c: How many night games did the Dodgers have?

nightgames <- sum(DodgersData$day_night == "Night")

nightgames
## [1] 66

Answer:

The Dodgers had 66 night games during the season.

Exploratory Analysis

Attendance by Weather

ggplot(DodgersData, aes(x = temp, y = attend/1000, color = fireworks)) +
  geom_point() +
  facet_wrap(day_night ~ skies) +
  ggtitle("Dodgers Attendance By Temperature By Time of Game and Skies") +
  theme(plot.title = element_text(
    lineheight = 3,
    face = "bold",
    color = "black",
    size = 10
  )) +
  xlab("Temperature (Degrees Fahrenheit)") +
  ylab("Attendance (Thousands)")

Attendance by Opponent

ggplot(DodgersData, aes(x = attend/1000, y = opponent, color = day_night)) +
  geom_point() +
  ggtitle("Dodgers Attendance By Opponent") +
  theme(plot.title = element_text(
    lineheight = 3,
    face = "bold",
    color = "black",
    size = 10
  )) +
  xlab("Attendance (Thousands)") +
  ylab("Opponent")

Bobblehead Box Plot

ggplot(DodgersData, aes(x = bobblehead, y = attend)) +
  geom_boxplot() +
  ggtitle("Dodgers Attendance by Bobblehead Promotion") +
  xlab("Bobblehead Promotion") +
  ylab("Attendance")

Q2: Interpret one of the box plots or scatter plots in plain language.

Answer:

Looking at the box plot, Dodgers games with bobblehead promotions generally had higher attendance than games without bobblehead promotions. This suggests that bobblehead promotions may have a positive relationship with attendance. Based on the linear regression reading, this shows how a predictor variable can help explain changes in an outcome.

Predictive Model

# Create regression model
my.model <- attend ~ month + day_of_week + bobblehead

# Fit the model
my.model.fit <- lm(my.model, data = DodgersData)

# Show regression results
summary(my.model.fit)
## 
## Call:
## lm(formula = my.model, data = DodgersData)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -10786.5  -3628.1   -516.1   2230.2  14351.0 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          38792.98    2364.68  16.405  < 2e-16 ***
## monthAUG              2377.92    2402.91   0.990   0.3259    
## monthJUL              2849.83    2578.60   1.105   0.2730    
## monthJUN              7163.23    2732.72   2.621   0.0108 *  
## monthMAY             -2385.62    2291.22  -1.041   0.3015    
## monthOCT              -662.67    4046.45  -0.164   0.8704    
## monthSEP                29.03    2521.25   0.012   0.9908    
## day_of_weekMonday    -4883.82    2504.65  -1.950   0.0554 .  
## day_of_weekSaturday   1488.24    2442.68   0.609   0.5444    
## day_of_weekSunday     1840.18    2426.79   0.758   0.4509    
## day_of_weekThursday  -4108.45    3381.22  -1.215   0.2286    
## day_of_weekTuesday    3027.68    2686.43   1.127   0.2638    
## day_of_weekWednesday -2423.80    2485.46  -0.975   0.3330    
## bobbleheadYES        10714.90    2419.52   4.429 3.59e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6120 on 67 degrees of freedom
## Multiple R-squared:  0.5444, Adjusted R-squared:  0.456 
## F-statistic: 6.158 on 13 and 67 DF,  p-value: 2.083e-07

Q3: Explain the final statistical results in plain language.

Answer:

The regression results show that bobblehead promotions had a positive relationship with Dodgers attendance. After accounting for the month and day of the week, games with a bobblehead promotion were associated with about 10,715 more attendees compared to games without one.

The model had an R-squared value of about 0.544, meaning that about 54.4% of the differences in attendance were explained by the variables included in the regression model. This suggests that bobblehead promotions can be a useful predictor of Dodgers attendance.

Q4: Develop two draft hypotheses for your group project.

Answer:

Hypothesis 1: The average amount of pharmaceutical sales will differ depending on the drug category.

This hypothesis can help us determine whether certain types of drugs have higher demand than others.

Hypothesis 2: The average amount of pharmaceutical sales will differ depending on the month or season.

This hypothesis can help us determine whether pharmaceutical sales change throughout the year and whether businesses should adjust their inventory based on seasonal demand.