library(readr)
RawData <- read_csv("NBAGames.csv")
## Rows: 26651 Columns: 21
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): GAME_STATUS_TEXT
## dbl (19): GAME_ID, HOME_TEAM_ID, VISITOR_TEAM_ID, SEASON, TEAM_ID_home, PTS...
## date (1): GAME_DATE_EST
##
## ℹ 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.
CleanedData <- RawData %>% filter(grepl("^2", GAME_ID)) #cleans data by removing preseason, all-star, and playoff games
GameData <- CleanedData[ -c(1:5, 7, 14)]
This is the code for uploading the data. In the raw NBAGames.csv file, there are 21 variables with 26651 observations for each variable. Each observation represents a single game with the data beginning in the 2003-2004 season all the way to midway through the 2022-2023 season. The data is cleaned to remove non-regular season games as those could contribute unwanted noise to the dataset.
The main goal of this data is to analyze the variables over time and see how they changed. Is there a relative change between stats recorded by the winning/losing team and home/away teams. More specifically, I want to analyze if over time, there is a change in the difference between stats in a given game and if it is related to if the home or away team wins. For example, did the margin of victory change over time on average when the home team wins? What about if the away team wins? What about changes in other stats.
Given that my analysis is only interested in the specific stats, which team was victorious, and the season, some of the variables removed. This cleaning of the data can be visualized in the change between RawData and GameData as the removed columns represent the removed variables. This brings the total number of observations from 26651 to 23273.
This leaves 14 variables remaining. 2 categorical variables (SEASON and HOME_TEAM_WINS), plus 12 quantitative variables (PTS, FG_PCT, FT_PCT, FG3_PCT, AST, and REB for both the home and away teams).
Given that my main focus is on analyzing the change in trends over time, I will be using a time series model. When setting up to perform a time series analysis, one thing that is important is that the frequency in each interval is the same. What this means in practice is that the number of observations between each interval would be the same. However, this dataset doesn’t satisfy this condition because it doesn’t have a complete season of info for the 2022-2023 season and the 2011-2012 season was shortened due to a lockout. As a result to have a time-series analysis, each season will be averaged for each variable so there will only be one observation per season.
For a time series model, you can either use an additive model or a
multiplicative model. The general formula for an additive time model is
Observed = Level + Trend + Noise while the general formula
for a multiplicative time model is
Observed = Level * Trend * Noise. The additive model is
used when seasonal variation occurs independent of the trend of the data
while the multiplicative time model is used when seasonal variation
occurs proportionally to the observed trend. Given that across an NBA
season, there shouldn’t be seasonal variation occurring, I will be using
the additive model when performing my time-series analysis on each
variable.
When breaking down an additime time model, it is important to understand what each term means.
Given that I am primarily focused on change in stats over time, the variable that is the most interesting is Trend. For each of the quantitative variables, I will decompose the additive time model and observe over time to determine if a variable appears to be decreasing or increasing between seasons. To specifically observe Trend, I will looking at a simple moving average, eg the average for a variable over the previous n years. A simple moving average smooths out the data and removes noise that may appear. This helps to graphically show the trend in a time-series model.
When using an additive time model and then decomposing it, the only assumption is that the data is stationary. Data remaining stationary means that the dataset doesn’t change significantly over time.
For the purposes of this project, a lack of seasonality is also being assumed for each variability so that an additive time-series model may be used.
Using a time-series model reminds me of using linear regression models. At its core, an additive time-series model is essentially a linear regression model but instead of using variables straight from the dataset like we use in linear regression, an additive time-series model uses the trend of the data to help predict what the output will be.
MeanHomePoints <- favstats(~PTS_home, groups = SEASON, data = GameData) [c("SEASON", "mean")] #creates new dataset with all seasons and the mean value of
HomePoints <- MeanHomePoints$mean #creates vector of average home points in each season in seasonal order
HomePointsTS <- ts(HomePoints, start = 2003) #creates time series vector of average of home points
SMA4HomePointsTS <- SMA(HomePointsTS, n = 4) #creates simple moving average of last 4 seasons
MeanAwayPoints <- favstats(~PTS_away, groups = SEASON, data = GameData) [c("SEASON", "mean")]
AwayPoints <- MeanAwayPoints$mean
AwayPointsTS <- ts(AwayPoints, start = 2003)
SMA4AwayPointsTS <- SMA(AwayPointsTS, n = 4)
ts.plot(SMA4HomePointsTS, SMA4AwayPointsTS, col = c("cornflowerblue", "firebrick1"), xlab = "Season", ylab = "Points Per Game", lwd = 5)
legend("topleft", bty="n", lty=c(1,1), col=c("cornflowerblue","firebrick1"),
legend=c("Home Team", "Away Team"))
Looking at the simple moving average of points per game, there is
clearly an upward trend occuring in the data. The points per game does
plateau initially but beginning around 2015, there is a significant
upward in points per game scored by both the home and away teams.
MeanFgPctHome <- favstats(~FG_PCT_home, groups = SEASON, data = GameData) [c("SEASON", "mean")]
HomeFgPCT <- MeanFgPctHome$mean
HomeFgPctTS <- ts(HomeFgPCT, start = 2003)
SMA4HomeFgPctTS <- SMA(HomeFgPctTS, n = 4)
MeanFgPctAway <- favstats(~FG_PCT_away, groups = SEASON, data = GameData) [c("SEASON", "mean")]
AwayFgPct <- MeanFgPctAway$mean
AwayFgPctTS <- ts(AwayFgPct, start = 2003)
SMA4AwayFgPctTS <- SMA(AwayFgPctTS, n = 4)
ts.plot(SMA4HomeFgPctTS, SMA4AwayFgPctTS, col = c("cornflowerblue", "firebrick1"), xlab = "Season", ylab = "Field Goal Percentage", lwd = 5)
legend("topleft", bty="n", lty=c(1,1), col=c("cornflowerblue","firebrick1"),
legend=c("Home Team", "Away Team"))
Unlike as seen with points per game, the simple mean analysis for field goal percentage doesn’t show as clear of a trend. It appears to have a slight upward trend but overall, it doesn’t seem super strong. As with the points per game chart, the away and home stats appear to mirror each other although they do seem to begin to get closer near the end of the dataset.
MeanFtPctHome <- favstats(~FT_PCT_home, groups = SEASON, data = GameData) [c("SEASON", "mean")]
HomeFtPCT <- MeanFtPctHome$mean
HomeFtPctTS <- ts(HomeFtPCT, start = 2003)
SMA4HomeFtPctTS <- SMA(HomeFtPctTS, n = 4)
MeanFtPctAway <- favstats(~FT_PCT_away, groups = SEASON, data = GameData) [c("SEASON", "mean")]
AwayFtPct <- MeanFtPctAway$mean
AwayFtPctTS <- ts(AwayFtPct, start = 2003)
SMA4AwayFtPctTS <- SMA(AwayFtPctTS, n = 4)
ts.plot(SMA4HomeFtPctTS, SMA4AwayFtPctTS, col = c("cornflowerblue", "firebrick1"), xlab = "Season", ylab = "Free Throw Percentage", lwd = 5)
legend("topleft", bty="n", lty=c(1,1), col=c("cornflowerblue","firebrick1"),
legend=c("Home Team", "Away Team"))
There is a clear upward trend in free throw percentage over the course of this data. Interestingly, there does appear to be a league wide dip for 3-4 between 2010 and 2015 but steadily increases after that. Curiously, the away team improves at a quicker rate relative to the home team as the away team actually has a higher free throw percentage near the end of the dataset. This indicates that potentially, free throw shooting percentage is less impacted being on the road than other stats.
Mean3FgPctHome <- favstats(~FG3_PCT_home, groups = SEASON, data = GameData) [c("SEASON", "mean")]
Home3FgPCT <- Mean3FgPctHome$mean
Home3FgPctTS <- ts(Home3FgPCT, start = 2003)
SMA4Home3FgPctTS <- SMA(Home3FgPctTS, n = 4)
Mean3FgPctAway <- favstats(~FG3_PCT_away, groups = SEASON, data = GameData) [c("SEASON", "mean")]
Away3FgPct <- Mean3FgPctAway$mean
Away3FgPctTS <- ts(Away3FgPct, start = 2003)
SMA4Away3FgPctTS <- SMA(Away3FgPctTS, n = 4)
ts.plot(SMA4Home3FgPctTS, SMA4Away3FgPctTS, col = c("cornflowerblue", "firebrick1"), xlab = "Season", ylab = "Three Point Field Goal Percentage", lwd = 5)
legend("topleft", bty="n", lty=c(1,1), col=c("cornflowerblue","firebrick1"),
legend=c("Home Team", "Away Team"))
This graph is the most intriguing as the home team and away team percentages don’t mirror each other as well as they do in other statistics. The home team seems to slightly improve percentage over the course of the data but seems relatively stable. There is much more variation within the away team percentages as they dip harder between 2010-2016ish but have a strong upward trend during the end. There is a lot more variability present within the three point field goal percentage than there is compared to normal field goal or free throw percentage.
MeanRebHome <- favstats(~REB_home, groups = SEASON, data = GameData) [c("SEASON", "mean")]
HomeReb <- MeanRebHome$mean
HomeRebTS <- ts(HomeReb, start = 2003)
SMA4HomeRebTS <- SMA(HomeRebTS, n = 4)
MeanRebAway <- favstats(~REB_away, groups = SEASON, data = GameData) [c("SEASON", "mean")]
AwayReb <- MeanRebAway$mean
AwayRebTS <- ts(AwayReb, start = 2003)
SMA4AwayRebTS <- SMA(AwayRebTS, n = 4)
ts.plot(SMA4HomeRebTS, SMA4AwayRebTS, col = c("cornflowerblue", "firebrick1"), xlab = "Season", ylab = "Rebounds Per Game", lwd = 5)
legend("topleft", bty="n", lty=c(1,1), col=c("cornflowerblue","firebrick1"),
legend=c("Home Team", "Away Team"))
There appears to be a steady increase in rebounds per game by both the home and away teams as the lines seem to relatively mirror each other. The home team appears to constantly grab 1-2 rebounds more per game on average over the course of the dataset. Interestingly however, there appears to be more total rebounds at the end of the dataset potentially indicating a faster pace of play.
MeanAstHome <- favstats(~AST_home, groups = SEASON, data = GameData) [c("SEASON", "mean")]
HomeAst <- MeanAstHome$mean
HomeAstTS <- ts(HomeAst, start = 2003)
SMA4HomeAstTS <- SMA(HomeAstTS, n = 4)
MeanAstAway <- favstats(~AST_away, groups = SEASON, data = GameData) [c("SEASON", "mean")]
AwayAst <- MeanAstAway$mean
AwayAstTS <- ts(AwayAst, start = 2003)
SMA4AwayAstTS <- SMA(AwayAstTS, n = 4)
ts.plot(SMA4HomeAstTS, SMA4AwayAstTS, col = c("cornflowerblue", "firebrick1"), xlab = "Season", ylab = "Assists Per Game", lwd = 5)
legend("topleft", bty="n", lty=c(1,1), col=c("cornflowerblue","firebrick1"),
legend=c("Home Team", "Away Team"))
The number of assists constantly increases throughout the course of the data. It is relatively stable to begin the dataset but after 2011, both the home and away teams get more assists per game on average. There is a slight dip in 2011, however that could potentially be attributed to the influence of the lockout. The away team seems to be increasing assists at a faster rate as while the home team still gets more assists per game than the away team, the gap is visibly smaller at the end of the study.
The one commonality throughout all the stats is that over the course of the data, they all appeared to go up. This indicates a faster pace of play throughout the league as a whole. Interestingly, the number of total rebounds increased between both teams over the course of game. Counterintuitively however, the field goal percentage also increased for both home and away teams. This doesn’t make sense as despite teams making a higher percentage of their overall shots, teams got more rebounds, which can only be gained from shots being missed. This indicates that more shots were being taken overall, which makes the efficiency increase interesting.
For most of the stats, the home team was “better” (e.g higher percentage or quantity of stats recorded) and the lines for the home and away teams relatively mirrored each other. This makes the three point field goal percentage graph interesting as the percentages change at different rates for the home and away team. This could potentially indicate that the overall skill is increasing so teams are less impacted by being on the road, but their are many underlying factors that could potentially impact it such as travel being optimized or other factors.
One weakness of using this sort of time analysis is that it requires the number of observations to be equal in unit of frequency. Normally, this wouldn’t be an issue except the lockout in 2011 created an outlier season that messes with the stationary nature of the data. If I would do this project again, I would use a model that could correct for non-stationary data.
Another goal if I could redo this project would be to perform multivariate analysis. All of these graphs and conclusions are univariate analysis. It doesn’t say anything about the impact one variable may have on another and if their changes are related.
A strength of this model, particularly the simple moving average graphs, is that it helps smooth out the data. Even though the dataset only had 20 observations (a weakness that had to be worked around due to the outlier 2011 lockout season), smoothing out the graph with SMA helped make trends more obvious in graphical form.