0
$\begingroup$

BACKGROUND

Hello, I am relatively new to R. I am trying to execute a regression of winning percentage against a certain set of statistics. I have had experience with regression when the data is simplified and tidy already, but I do not have much experience in fixing my data to allow me to run the proper regression.

Data

I have added the dataset I am using. Here is the link: https://www.kaggle.com/nathanlauga/nba-games You will be directed to the website that contains the games.csv download file.

Code

The first thing I did was make this into a data frame and replace all the Team I.D.'s with their real abbreviated names.

head(NBA_Games)
NBA.df <- data.frame(NBA_Games)

head(NBA.df)

NBA.df[NBA.df == "1610612737"] <- "ATL"
NBA.df[NBA.df == "1610612738"] <- "BOS"
NBA.df[NBA.df == "1610612740"] <- "NO"
NBA.df[NBA.df == "1610612741"] <- "CHI"
NBA.df[NBA.df == "1610612742"] <- "DAL"
NBA.df[NBA.df == "1610612743"] <- "DEN"
NBA.df[NBA.df == "1610612745"] <- "HOU"
NBA.df[NBA.df == "1610612746"] <- "LAC"
NBA.df[NBA.df == "1610612747"] <- "LAL"
NBA.df[NBA.df == "1610612748"] <- "MIA"
NBA.df[NBA.df == "1610612749"] <- "MIL"
NBA.df[NBA.df == "1610612750"] <- "MIN"
NBA.df[NBA.df == "1610612751"] <- "BKN"
NBA.df[NBA.df == "1610612752"] <- "NYK"
NBA.df[NBA.df == "1610612753"] <- "ORL"
NBA.df[NBA.df == "1610612754"] <- "IND"
NBA.df[NBA.df == "1610612755"] <- "PHI"
NBA.df[NBA.df == "1610612756"] <- "PHX"
NBA.df[NBA.df == "1610612757"] <- "POR"
NBA.df[NBA.df == "1610612758"] <- "SAC"
NBA.df[NBA.df == "1610612759"] <- "SAS"
NBA.df[NBA.df == "1610612760"] <- "OKC"
NBA.df[NBA.df == "1610612761"] <- "TOR"
NBA.df[NBA.df == "1610612762"] <- "UTA"
NBA.df[NBA.df == "1610612763"] <- "MEM"
NBA.df[NBA.df == "1610612764"] <- "WAS"
NBA.df[NBA.df == "1610612765"] <- "DET"
NBA.df[NBA.df == "1610612766"] <- "CHA"
NBA.df[NBA.df == "1610612739"] <- "CLE"
NBA.df[NBA.df == "1610612744"] <- "GSW"

I am aware that there may have been an easier way to do that and any advice on that would be helpful, but that is not the true problem I am having.

Problem

If you notice from the data set, it is split into home team stats and away team stats. The only stat that records wins is in the last column of the data frame, named "HOME_TEAM_WINS". It records a 1 if they did and a 0 if they did not win. Now, this is where I do not really know what to do. I want my Response (Y) variable in my regression to be winning percentage, but it is in binary form at the moment. I would like to think that there is a way to create a new variable that would allow me to run it against the other in-game statistics.

Also, what would the easiest way to factor this and split it up by each team's winning percentage?

I hope this makes sense. Any help on how to do this or any tips about how to think of this from a statistical perspective will be greatly appreciated.

$\endgroup$
0

1 Answer 1

1
$\begingroup$

You have multiple options to model the data using regressions. Basically you should do two things: aggregate the data and then use the most appropriate regression model. It is up to you to think about how to summarize the data and fit the models. But here are some some ideas.

This is a summary of the winning proportions by home team

games2 <- games |>
    group_by(TEAM_ID_home) |>
    summarise(win_prop = mean(HOME_TEAM_WINS),
              observations = n())

Since the response variable is binary, perhaps you could explore a logistic regression model using the original data frame. This model explores if the proportion of winning at home is more frequently associated with any team (fm1) or season (fm2):

summary(fm1 <- glm(HOME_TEAM_WINS ~ factor(TEAM_ID_home), data = games, family = binomial))

summary(fm2 <- glm(HOME_TEAM_WINS ~ factor(SEASON), data = games, family = binomial))

You can count grouping by season and team and then create a new data-frame with all the predictors you are interested summarized by the grouped variables.

games3 <- games |>
    group_by(SEASON, TEAM_ID_home) |>
    summarise(win_count = sum(HOME_TEAM_WINS),
              observations = n(),
              pts_home = sum(PTS_home, na.rm = T))

A Poisson regression may be useful to fit this kind of data. Including an "offset" for the number of observations is necessary to model the outcome as he proportion of winning.

summary(fm3 <- glm(win_count ~ factor(TEAM_ID_home), data = games3, offset = log(observations), family = poisson))

$\endgroup$
1
  • $\begingroup$ Jose, thank you so much for your time. It is wildly appreciated $\endgroup$
    – LoveMYMAth
    Commented Nov 22, 2021 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.