NBA Home-court Advantage Analysis

Approach

In this assignment , I selected a NBA dataset from FiveThirtyEight that contains information about NBA games, including the teams, game location, points scored, and game results. I plan to use this dataset to explore home-court advantage in the NBA.

My plan is to load data into R and select variables related to the team, opponent, game location, points scored, and whether the team won or lost. I will clean and rename variables as needed and compare games results from teams playing at home versus away.

Data Source: https://raw.githubusercontent.com/reneewatson15/NBA-home-court-advantage-/refs/heads/main/nbaallelo.csv

https://data.fivethirtyeight.com

Codebase

nba <- read.csv("https://raw.githubusercontent.com/reneewatson15/NBA-home-court-advantage-/refs/heads/main/nbaallelo.csv")

str(nba)
## 'data.frame':    126314 obs. of  23 variables:
##  $ gameorder    : int  1 1 2 2 3 3 4 4 5 5 ...
##  $ game_id      : chr  "194611010TRH" "194611010TRH" "194611020CHS" "194611020CHS" ...
##  $ lg_id        : chr  "NBA" "NBA" "NBA" "NBA" ...
##  $ X_iscopy     : int  0 1 0 1 0 1 1 0 1 0 ...
##  $ year_id      : int  1947 1947 1947 1947 1947 1947 1947 1947 1947 1947 ...
##  $ date_game    : chr  "11/1/1946" "11/1/1946" "11/2/1946" "11/2/1946" ...
##  $ seasongame   : int  1 1 1 2 1 1 1 1 1 1 ...
##  $ is_playoffs  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ team_id      : chr  "TRH" "NYK" "CHS" "NYK" ...
##  $ fran_id      : chr  "Huskies" "Knicks" "Stags" "Knicks" ...
##  $ pts          : int  66 68 63 47 33 50 53 59 51 56 ...
##  $ elo_i        : num  1300 1300 1300 1307 1300 ...
##  $ elo_n        : num  1293 1307 1310 1297 1280 ...
##  $ win_equiv    : num  40.3 41.7 42 40.7 38.9 ...
##  $ opp_id       : chr  "NYK" "TRH" "NYK" "CHS" ...
##  $ opp_fran     : chr  "Knicks" "Huskies" "Knicks" "Stags" ...
##  $ opp_pts      : int  68 66 47 63 50 33 59 53 56 51 ...
##  $ opp_elo_i    : num  1300 1300 1307 1300 1300 ...
##  $ opp_elo_n    : num  1307 1293 1297 1310 1320 ...
##  $ game_location: chr  "H" "A" "H" "A" ...
##  $ game_result  : chr  "L" "W" "W" "L" ...
##  $ forecast     : num  0.64 0.36 0.631 0.369 0.64 ...
##  $ notes        : chr  "" "" "" "" ...

For this analysis, I selected variables related to the season, teams, game location, points scored, and game result.

nba_subset <- nba[c("year_id", "date_game", "team_id", "opp_id", "pts", "opp_pts", "game_location", "game_result")]

head(nba_subset)
##   year_id date_game team_id opp_id pts opp_pts game_location game_result
## 1    1947 11/1/1946     TRH    NYK  66      68             H           L
## 2    1947 11/1/1946     NYK    TRH  68      66             A           W
## 3    1947 11/2/1946     CHS    NYK  63      47             H           W
## 4    1947 11/2/1946     NYK    CHS  47      63             A           L
## 5    1947 11/2/1946     DTF    WSC  33      50             H           L
## 6    1947 11/2/1946     WSC    DTF  50      33             A           W

I changed the names of the variables.

names(nba_subset) <- c("season", "game_date", "team", "opponent", "team_points", "opponent_points", "game_location", "game_result")

head(nba_subset)
##   season game_date team opponent team_points opponent_points game_location
## 1   1947 11/1/1946  TRH      NYK          66              68             H
## 2   1947 11/1/1946  NYK      TRH          68              66             A
## 3   1947 11/2/1946  CHS      NYK          63              47             H
## 4   1947 11/2/1946  NYK      CHS          47              63             A
## 5   1947 11/2/1946  DTF      WSC          33              50             H
## 6   1947 11/2/1946  WSC      DTF          50              33             A
##   game_result
## 1           L
## 2           W
## 3           W
## 4           L
## 5           L
## 6           W

Analysis

To examine home-court advantage, I selected games where the team was playing at home.

home_games <- nba_subset[nba_subset$game_location == "H", ]

head(home_games)
##    season game_date team opponent team_points opponent_points game_location
## 1    1947 11/1/1946  TRH      NYK          66              68             H
## 3    1947 11/2/1946  CHS      NYK          63              47             H
## 5    1947 11/2/1946  DTF      WSC          33              50             H
## 8    1947 11/2/1946  PRO      BOS          59              53             H
## 10   1947 11/2/1946  STB      PIT          56              51             H
## 12   1947 11/3/1946  CLR      TRH          71              60             H
##    game_result
## 1            L
## 3            W
## 5            L
## 8            W
## 10           W
## 12           W
home_results <- table(home_games$game_result)

home_results
## 
##     L     W 
## 23833 39305
barplot(home_results,
        main = "NBA Home Game Results",
        xlab = "Game Result",
        ylab = "Number of Games")

In the plot, ā€œLā€ represents a loss and ā€œWā€ represents a win. Home teams won more games than they lost.

home_win_percentage <- mean(home_games$game_result == "W") * 100

round(home_win_percentage, 1)
## [1] 62.3

The results show that NBA teams won 62.3% of games when playing at home.

Conclusion

The results of this analysis show that NBA teams won 62.3% of their games when playing at home, suggesting that home-court advantage is present in the historical data. To extend this analysis, I could compare home winning percentages across different NBA seasons or individual teams. I could also use more recent data to determine whether home-court advantage has changed over time.