Loading needed packages.
library(readxl)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(lmtest)
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(ggplot2)
library(ggpubr)
Importing the data set into R.
X4_22_data <- read_excel("Documents/4-22_data.xlsx")
champions <- read_excel("Documents/champions.xlsx")
Adding the expected wins and winning percenatage into the data set.
X4_22_data1 <- mutate(X4_22_data, wpyth = P**13.91/(P**13.91 + OP**13.91)) %>%
mutate(X4_22_data, ExpectedWins = wpyth *GP)
X4_22_data2 <- mutate(X4_22_data, wpyth = P**16.5/(P**16.5 + OP**16.5)) %>%
mutate(X4_22_data, ExpectedWins = wpyth *GP)
Test to see if 13.91 or 16.5 is the better exponent.
ModelA <- lm(WPCT~wpyth, X4_22_data1)
summary(ModelA)
##
## Call:
## lm(formula = WPCT ~ wpyth, data = X4_22_data1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.125635 -0.024631 0.002145 0.025831 0.100661
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.003257 0.005839 -0.558 0.577
## wpyth 1.005067 0.011201 89.729 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.03784 on 538 degrees of freedom
## Multiple R-squared: 0.9374, Adjusted R-squared: 0.9372
## F-statistic: 8051 on 1 and 538 DF, p-value: < 2.2e-16
ModelB <- lm(WPCT~wpyth, X4_22_data2)
summary(ModelB)
##
## Call:
## lm(formula = WPCT ~ wpyth, data = X4_22_data2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.127510 -0.025784 0.001659 0.025893 0.100114
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.063982 0.005118 12.50 <2e-16 ***
## wpyth 0.870151 0.009689 89.81 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.0378 on 538 degrees of freedom
## Multiple R-squared: 0.9375, Adjusted R-squared: 0.9374
## F-statistic: 8066 on 1 and 538 DF, p-value: < 2.2e-16
Linear regression assumption tests.
plot(ModelB)
resettest(WPCT~wpyth, 3, type="regressor", X4_22_data2)
##
## RESET test
##
## data: WPCT ~ wpyth
## RESET = 0.19737, df1 = 1, df2 = 537, p-value = 0.657
shapiro.test(ModelB$residuals)
##
## Shapiro-Wilk normality test
##
## data: ModelB$residuals
## W = 0.99633, p-value = 0.2488
bptest(ModelB)
##
## studentized Breusch-Pagan test
##
## data: ModelB
## BP = 0.99349, df = 1, p-value = 0.3189
Scatter plot showing the Actual vs Predicted Wins for the Years 2004-2022.
WPCT_wpyth2 <- ggplot(X4_22_data2, aes(WPCT, wpyth))
WPCT_wpyth2 +
geom_point() +
geom_smooth(se=F) +
labs(title = 'Actual vs Predicted Wins for the Years 2004-2022', x = "Winning Percentage", y = "Predicted Winning Percentage") +
stat_cor(aes(label=..rr.label..), label.x=.25, label.y=.7)
## Warning: The dot-dot notation (`..rr.label..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(rr.label)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
18 Scatter plots showing Actual vs Predicted Wins for the Years 2004-2022.
WPCT_wpyth2 +
geom_point() +
facet_wrap(~Year, ncol=6) +
geom_smooth(se=F, color="red") +
labs(title = 'Actual vs Predicted Wins for the Years 2004-2022', x = "Winning Percentage", y = "Predicted Winning Percentage") +
stat_cor(aes(label=..rr.label..), label.x=.25, label.y=.2)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Importing the R-squared data set for each year. Then combining it with the previously imported data set. Lastly, deleted all the colums except year and r-squares to create the upcoming plots.
r_square <- read_excel("Documents/r-square.xlsx")
NBAR <- inner_join(X4_22_data2, r_square, by= c("Year"))
NBA_yearly_regressions <- NBAR %>%
group_by(Year) %>%
select(Year, R)
A graph showing the adjsuted R-squared values for each year.
NBA_yearly_regressions %>%
ggplot(aes(x=Year, y= R)) +
geom_smooth() +
geom_line() +
geom_point() +
labs(title =" Adjusted R-Square Values Over the Years", x= "Season", y="Adjusted R-square") +
scale_y_continuous(limits = c(0,1)) +
scale_x_discrete(limits = c("4-5","5-6","6-7","7-8","8-9","9-10","10-11","11-12","12-13","13-14","14-15","15-16","16-17","17-18","18-19","19-20","20-21","21-22"))
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
A historgram showing the Wins Above Expected for the years 2004-2022.
NBAR2 <- mutate(NBAR, WinsAboveExpected=W-ExpectedWins)
NBAR2 %>%
ggplot(aes(x=WinsAboveExpected)) +
geom_histogram(color="white") +
labs(title ="Wins Above Expected for the years 2004-2022")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
A table showing the teams that had over a -7 and 7 wins above expected.
NBAR2 %>%
filter(abs(WinsAboveExpected) > 7) %>%
arrange(desc(WinsAboveExpected)) %>%
mutate(WinsAboveExpected = round(WinsAboveExpected, 2)) %>%
select(Year,Team, W,L, WPCT, wpyth, WinsAboveExpected) %>%
print(n=24)
## # A tibble: 24 × 7
## Year Team W L WPCT wpyth WinsAboveExpected
## <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 20-21 OKC 22 50 0.306 0.169 9.83
## 2 21-22 POR 27 55 0.329 0.210 9.77
## 3 7-8 BKN 34 48 0.415 0.300 9.43
## 4 5-6 UTAH 41 41 0.5 0.386 9.35
## 5 15-16 MEM 42 40 0.512 0.409 8.49
## 6 5-6 POR 21 61 0.256 0.159 7.99
## 7 9-10 LAC 29 53 0.354 0.257 7.91
## 8 16-17 ORL 29 53 0.354 0.261 7.58
## 9 12-13 HOU 45 37 0.549 0.634 -7.01
## 10 12-13 OKC 60 22 0.732 0.818 -7.08
## 11 15-16 UTAH 40 42 0.488 0.576 -7.22
## 12 17-18 DAL 24 58 0.293 0.382 -7.3
## 13 19-20 DAL 43 32 0.573 0.671 -7.32
## 14 21-22 SAN 34 48 0.415 0.504 -7.36
## 15 9-10 SAS 50 32 0.610 0.700 -7.43
## 16 7-8 UTAH 54 28 0.659 0.751 -7.59
## 17 20-21 TOR 27 45 0.375 0.483 -7.74
## 18 6-7 CHI 49 33 0.598 0.702 -8.6
## 19 6-7 SAS 58 24 0.707 0.814 -8.74
## 20 21-22 UTAH 49 33 0.598 0.711 -9.31
## 21 13-14 MIN 40 42 0.488 0.603 -9.43
## 22 7-8 TOR 41 41 0.5 0.619 -9.75
## 23 11-12 PHI 35 31 0.530 0.682 -10.0
## 24 21-22 BOS 51 31 0.622 0.752 -10.7
RMSE grouped by Year.
X4_22_data2 %>%
group_by(Year) %>%
summarise(RMSE = Metrics::rmse(W, ExpectedWins))
## # A tibble: 18 × 2
## Year RMSE
## <chr> <dbl>
## 1 10-11 3.62
## 2 11-12 3.26
## 3 12-13 3.29
## 4 13-14 3.44
## 5 14-15 3.12
## 6 15-16 3.44
## 7 16-17 3.26
## 8 17-18 3.57
## 9 18-19 2.82
## 10 19-20 2.95
## 11 20-21 3.80
## 12 21-22 4.46
## 13 4-5 3.07
## 14 5-6 3.84
## 15 6-7 3.91
## 16 7-8 4.42
## 17 8-9 1.82
## 18 9-10 3.48
Overall RMSE.
X4_22_data2 %>%
summarise(RMSE = Metrics::rmse(W, ExpectedWins))
## # A tibble: 1 × 1
## RMSE
## <dbl>
## 1 3.47