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)
ANOVA testing Null Hypothesis (H0): The league a baseball team plays in does not have a statistically significant effect on the number of runs scored (RS).
baseball_data <- week3_dataset %>%
group_by(League) %>%
mutate(LeagueCount = n()) %>%
mutate(League = ifelse(LeagueCount < 10, "Other", as.character(League))) %>%
select(-LeagueCount)
result <- aov(RS ~ League, data = baseball_data)
summary(result)
## Df Sum Sq Mean Sq F value Pr(>F)
## League 1 369466 369466 45.7 2.13e-11 ***
## Residuals 1230 9944501 8085
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
model1 <- lm(RS ~ OBP, data = baseball_data)
summary(model1)
##
## Call:
## lm(formula = RS ~ OBP, data = baseball_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -122.129 -27.110 1.284 26.441 135.265
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1076.6 24.7 -43.59 <2e-16 ***
## OBP 5490.4 75.6 72.62 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 39.82 on 1230 degrees of freedom
## Multiple R-squared: 0.8109, Adjusted R-squared: 0.8107
## F-statistic: 5274 on 1 and 1230 DF, p-value: < 2.2e-16
baseball_data %>%
ggplot()+
geom_point(mapping = aes(x = OBP, y = RS))+
geom_smooth(mapping = aes(x = OBP, y = RS), method = lm)+
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
The R squared is 0.81 and p-value is under 0.05. Which means that the
OBP significantly influences RS.
model2 <- lm(RS ~ OBP + SLG, data = baseball_data)
summary(model2)
##
## Call:
## lm(formula = RS ~ OBP + SLG, data = baseball_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -78.365 -16.821 -1.208 16.477 92.684
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -811.66 16.75 -48.47 <2e-16 ***
## OBP 2830.70 77.94 36.32 <2e-16 ***
## SLG 1517.58 35.17 43.15 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 25.12 on 1229 degrees of freedom
## Multiple R-squared: 0.9248, Adjusted R-squared: 0.9247
## F-statistic: 7557 on 2 and 1229 DF, p-value: < 2.2e-16
SLG is a stats that I think will influence RS a lot, since higher slugging means more total bases. So it will likely improve the probability to score. The result comes out really good. The r squared is 0.92 and p-value is lower then 0.05. It means that the OBP and SLG combined together will have a great influence one run scored.