# Import the Dodgers dataset
dodgers <- read.csv("DodgersData.csv",
fileEncoding = "UTF-8-BOM",
stringsAsFactors = FALSE)
# Remove extra spaces from text variables
text_columns <- sapply(dodgers, is.character)
dodgers[text_columns] <- lapply(dodgers[text_columns], trimws)
# Question 1a
dodgers[25, c("temp", "attend", "opponent", "bobblehead")]
## temp attend opponent bobblehead
## 25 61 36561 Astros NO
# Question 1b
median(dodgers$attend)
## [1] 40284
# Question 1c
sum(dodgers$day_night == "Night")
## [1] 66
# Question 2: Scatter plot
plot(dodgers$temp, dodgers$attend,
main = "Dodgers Attendance and Temperature",
xlab = "Temperature (°F)",
ylab = "Attendance",
pch = 19,
col = ifelse(dodgers$bobblehead == "YES",
"red", "black"))
legend("topleft",
legend = c("No Bobblehead", "Bobblehead"),
col = c("black", "red"),
pch = 19)
# Correlation for the scatter plot
cor(dodgers$temp, dodgers$attend)
## [1] 0.09895073
# Organize months chronologically
dodgers$ordered_month <- factor(
dodgers$month,
levels = c("APR", "MAY", "JUN", "JUL",
"AUG", "SEP", "OCT")
)
# Organize days chronologically
dodgers$ordered_day_of_week <- factor(
dodgers$day_of_week,
levels = c("Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday")
)
# Question 3: Regression model
model <- lm(
attend ~ ordered_month +
ordered_day_of_week +
bobblehead,
data = dodgers
)
# Display regression results
summary(model)
##
## Call:
## lm(formula = attend ~ ordered_month + ordered_day_of_week + bobblehead,
## data = dodgers)
##
## 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) 33909.16 2521.81 13.446 < 2e-16 ***
## ordered_monthMAY -2385.62 2291.22 -1.041 0.30152
## ordered_monthJUN 7163.23 2732.72 2.621 0.01083 *
## ordered_monthJUL 2849.83 2578.60 1.105 0.27303
## ordered_monthAUG 2377.92 2402.91 0.990 0.32593
## ordered_monthSEP 29.03 2521.25 0.012 0.99085
## ordered_monthOCT -662.67 4046.45 -0.164 0.87041
## ordered_day_of_weekTuesday 7911.49 2702.21 2.928 0.00466 **
## ordered_day_of_weekWednesday 2460.02 2514.03 0.979 0.33134
## ordered_day_of_weekThursday 775.36 3486.15 0.222 0.82467
## ordered_day_of_weekFriday 4883.82 2504.65 1.950 0.05537 .
## ordered_day_of_weekSaturday 6372.06 2552.08 2.497 0.01500 *
## ordered_day_of_weekSunday 6724.00 2506.72 2.682 0.00920 **
## 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
# Display statistical significance results
anova(model)
## Analysis of Variance Table
##
## Response: attend
## Df Sum Sq Mean Sq F value Pr(>F)
## ordered_month 6 948958117 158159686 4.2225 0.001158 **
## ordered_day_of_week 6 1314813030 219135505 5.8504 6.002e-05 ***
## bobblehead 1 734587177 734587177 19.6118 3.590e-05 ***
## Residuals 67 2509574563 37456337
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Question 1b: The median attendance was 40,284 fans. #Question 1c: The Dodgers played 66 night games. #Question 2: The scatter plot shows that temperature had little effect on attendance. The points do not follow a strong pattern. However, games with bobblehead promotions usually had higher attendance than games without promotions. #Question 3: The regression results show that bobblehead promotions increased attendance by about 10,715 fans per game after considering the month and day of the week. The model explained about 54.4% of attendance differences. Overall, promotions appeared to affect attendance more than temperature, but other factors may also have influenced the results.