library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ 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
library(ggplot2)
library(dbplyr)
##
## Attaching package: 'dbplyr'
##
## The following objects are masked from 'package:dplyr':
##
## ident, sql
library(ggthemes)
library(ggrepel)
week3_dataset <- read.csv("baseball.csv",header = TRUE)
Hypothesis 1 is to compare AL and NL SLG Hypothesis 1 is to OBP of the playoffs and non-playoffs team
avg_by_league <- week3_dataset %>%
group_by(League)%>%
summarize(avg = mean(SLG))
playoff_vs_nonPlayoff <- week3_dataset %>%
group_by(Playoffs)%>%
summarise(OBP = mean(OBP))
avg_diff <- (avg_by_league$avg[1] -
avg_by_league$avg[2])
playoff_diff <- (playoff_vs_nonPlayoff$OBP[2] -
playoff_vs_nonPlayoff$OBP[1])
paste(avg_diff, playoff_diff)
## [1] "0.010586038961039 0.0137022632242649"
Alpha level (significance level): 0.05 Power level: 0.80 Choose these alpha level as 0.05 and power level as 0.8 just for common defaults hypothesis testing. effect size:
sd <- sd(week3_dataset$SLG)
Hypothesis1_D <- avg_diff/sd
Hypothesis2_D <- playoff_diff/sd
Hypothesis1_D
## [1] 0.3182154
Hypothesis2_D
## [1] 0.4118888
AL_teams <- subset(week3_dataset, League == "AL")
NL_teams <- subset(week3_dataset, League == "NL")
alpha <- 0.05
mean_al_slg <- mean(AL_teams$SLG)
mean_nl_slg <- mean(NL_teams$SLG)
var_al_slg <- var(AL_teams$SLG)
var_nl_slg <- var(NL_teams$SLG)
numerator <- (mean_al_slg - mean_nl_slg)^2
denominator <- (var_al_slg / length(mean_al_slg)) + (var_nl_slg / length(mean_nl_slg))
lr_statistic <- numerator / denominator
critical_value <- qnorm(1 - alpha)
lr_statistic
## [1] 0.05190443
critical_value
## [1] 1.644854
Fail to reject the null hypothesis since the likelihood ratio is less then critical value
contingency_table <- table(week3_dataset$SLG, week3_dataset$League)
fisher_test_result_hypothesis1 <- chisq.test(contingency_table)
## Warning in chisq.test(contingency_table): Chi-squared approximation may be
## incorrect
print(fisher_test_result_hypothesis1)
##
## Pearson's Chi-squared test
##
## data: contingency_table
## X-squared = 183.17, df = 161, p-value = 0.1113
since the P-value is greater then 0.05 there is no significant association between making the playoffs and the league.
playoff_teams <- subset(week3_dataset, Playoffs == 1)
non_playoff_teams <- subset(week3_dataset, Playoffs == 0)
alpha <- 0.05
mean_playoff_obp <- mean(playoff_teams$OBP)
mean_non_playoff_obp <- mean(non_playoff_teams$OBP)
var_playoff_obp <- var(playoff_teams$OBP)
var_non_playoff_obp <- var(non_playoff_teams$OBP)
numerator <- (mean_playoff_obp - mean_non_playoff_obp)^2
denominator <- (var_playoff_obp / length(playoff_teams)) + (var_non_playoff_obp / length(non_playoff_teams))
lr_statistic <- numerator / denominator
critical_value <- qnorm(1 - alpha)
lr_statistic
## [1] 7.260359
critical_value
## [1] 1.644854
Reject null hypothesis since likelihood ratio is greater then critical value
contingency_table <- table(week3_dataset$SLG, week3_dataset$Playoffs)
fisher_test_result_hypothesis1 <- chisq.test(contingency_table)
## Warning in chisq.test(contingency_table): Chi-squared approximation may be
## incorrect
print(fisher_test_result_hypothesis1)
##
## Pearson's Chi-squared test
##
## data: contingency_table
## X-squared = 282.11, df = 161, p-value = 1.166e-08
week3_dataset %>%
ggplot() +
geom_boxplot(mapping = aes(x = as.factor(League), y = SLG), fill = "lightblue") +
labs(title = "Distribution of On-Base Percentage (OBP) by Playoff Status",
x = "Playoff Status",
y = "OBP") +
theme_minimal()
week3_dataset %>%
ggplot() +
geom_boxplot(mapping = aes(x = as.factor(Playoffs), y = OBP), fill = "lightblue") +
labs(title = "Distribution of On-Base Percentage (OBP) by Playoff Status",
x = "Playoff Status",
y = "OBP") +
theme_minimal()