andreweatherman / cbbdata

https://cbbdata.aweatherman.com
Other
13 stars 2 forks source link

cbbdata::cbd_torvik_game_prediction #8

Open barjehkopaxum opened 9 months ago

barjehkopaxum commented 9 months ago

Hi! I can't make out if it is possible to output predictions for the game by date, and not by entering individual commands? It would be cool if you could enter "20240111" into the "cbbdata::cbd_torvik_game_prediction" function and get a prediction of the games for all the games of all the teams for that day.

mykzaun commented 8 months ago

@barjehkopaxum not that difficult to put together some basic script that will achieve this for you, for example you could run:

# Load required library or install it if it's not available
if (!requireNamespace("cbbdata", quietly = TRUE)) {
  install.packages("cbbdata")
}

# Generate predictions for today's games
# Load the future games schedule
schedule <- cbd_torvik_season_schedule(year = 2024)

# Convert the 'date' column to Date class and filter for future games
todays_games <- schedule %>%
  dplyr::mutate(date = as.Date(date)) %>%
  filter(date == Sys.Date())

todays_predictions <- list()

for (i in seq_len(nrow(todays_games))) {
  game <- todays_games[i, ]
  game_location <- if (game$neutral) "N" else "H"
  # Format the date to the required format YYYYMMDD
  game_date <- format(as.Date(game$date), "%Y%m%d")

  tryCatch({
    prediction <- cbd_torvik_game_prediction(
      team = game$home,
      opp = game$away,
      date = game_date,
      location = game_location
    )

    # Add the game_id to the prediction tibble/df
    prediction$game_id <- game$game_id

    todays_predictions[[i]] <- prediction
  }, error=function(e) {
    cat("Error in processing prediction for game_id", game$game_id, ":", e$message, "\n")
  })
}

# Combine predictions into a single data frame
todays_predictions <- do.call(rbind, todays_predictions)

# Order the predictions by game_id if needed
todays_predictions <- todays_predictions[order(todays_predictions$game_id), ]