library(tidyverse)
library(nbastatR)
library(stringr)
library(corrplot)
library(reactable)
library(reactablefmtr)
library(dplyr)

Sys.setenv("VROOM_CONNECTION_SIZE" = 131072 * 2)

data pull

teams <- nba_teams() %>%
  filter( isNonNBATeam == 0,
          yearPlayedLast == 2023,
          idConference > 0)
league_id <- teams$idTeam
get_shots <- function(x) {
  df <- teams_shots(
    team_ids = league_id,
    seasons = x,
    season_types = "Regular Season"
  )
  return(df)
}
seasons <- 2015:2024

# Empty list to store the results
results_list <- list()

# Loop through each season and call the function
for (season in seasons) {
  results_list[[as.character(season)]] <- get_shots(season)
}
## Atlanta Hawks 2014-15 shot data
## Boston Celtics 2014-15 shot data
## Cleveland Cavaliers 2014-15 shot data
## New Orleans Pelicans 2014-15 shot data
## Chicago Bulls 2014-15 shot data
## Dallas Mavericks 2014-15 shot data
## Denver Nuggets 2014-15 shot data
## Golden State Warriors 2014-15 shot data
## Houston Rockets 2014-15 shot data
## Los Angeles Clippers 2014-15 shot data
## Los Angeles Lakers 2014-15 shot data
## Miami Heat 2014-15 shot data
## Milwaukee Bucks 2014-15 shot data
## Minnesota Timberwolves 2014-15 shot data
## Brooklyn Nets 2014-15 shot data
## New York Knicks 2014-15 shot data
## Orlando Magic 2014-15 shot data
## Indiana Pacers 2014-15 shot data
## Philadelphia 76ers 2014-15 shot data
## Phoenix Suns 2014-15 shot data
## Portland Trail Blazers 2014-15 shot data
## Sacramento Kings 2014-15 shot data
## San Antonio Spurs 2014-15 shot data
## Oklahoma City Thunder 2014-15 shot data
## Toronto Raptors 2014-15 shot data
## Utah Jazz 2014-15 shot data
## Memphis Grizzlies 2014-15 shot data
## Washington Wizards 2014-15 shot data
## Detroit Pistons 2014-15 shot data
## Charlotte Hornets 2014-15 shot data
## Atlanta Hawks 2015-16 shot data
## Boston Celtics 2015-16 shot data
## Cleveland Cavaliers 2015-16 shot data
## New Orleans Pelicans 2015-16 shot data
## Chicago Bulls 2015-16 shot data
## Dallas Mavericks 2015-16 shot data
## Denver Nuggets 2015-16 shot data
## Golden State Warriors 2015-16 shot data
## Houston Rockets 2015-16 shot data
## Los Angeles Clippers 2015-16 shot data
## Los Angeles Lakers 2015-16 shot data
## Miami Heat 2015-16 shot data
## Milwaukee Bucks 2015-16 shot data
## Minnesota Timberwolves 2015-16 shot data
## Brooklyn Nets 2015-16 shot data
## New York Knicks 2015-16 shot data
## Orlando Magic 2015-16 shot data
## Indiana Pacers 2015-16 shot data
## Philadelphia 76ers 2015-16 shot data
## Phoenix Suns 2015-16 shot data
## Portland Trail Blazers 2015-16 shot data
## Sacramento Kings 2015-16 shot data
## San Antonio Spurs 2015-16 shot data
## Oklahoma City Thunder 2015-16 shot data
## Toronto Raptors 2015-16 shot data
## Utah Jazz 2015-16 shot data
## Memphis Grizzlies 2015-16 shot data
## Washington Wizards 2015-16 shot data
## Detroit Pistons 2015-16 shot data
## Charlotte Hornets 2015-16 shot data
## Atlanta Hawks 2016-17 shot data
## Boston Celtics 2016-17 shot data
## Cleveland Cavaliers 2016-17 shot data
## New Orleans Pelicans 2016-17 shot data
## Chicago Bulls 2016-17 shot data
## Dallas Mavericks 2016-17 shot data
## Denver Nuggets 2016-17 shot data
## Golden State Warriors 2016-17 shot data
## Houston Rockets 2016-17 shot data
## Los Angeles Clippers 2016-17 shot data
## Los Angeles Lakers 2016-17 shot data
## Miami Heat 2016-17 shot data
## Milwaukee Bucks 2016-17 shot data
## Minnesota Timberwolves 2016-17 shot data
## Brooklyn Nets 2016-17 shot data
## New York Knicks 2016-17 shot data
## Orlando Magic 2016-17 shot data
## Indiana Pacers 2016-17 shot data
## Philadelphia 76ers 2016-17 shot data
## Phoenix Suns 2016-17 shot data
## Portland Trail Blazers 2016-17 shot data
## Sacramento Kings 2016-17 shot data
## San Antonio Spurs 2016-17 shot data
## Oklahoma City Thunder 2016-17 shot data
## Toronto Raptors 2016-17 shot data
## Utah Jazz 2016-17 shot data
## Memphis Grizzlies 2016-17 shot data
## Washington Wizards 2016-17 shot data
## Detroit Pistons 2016-17 shot data
## Charlotte Hornets 2016-17 shot data
## Atlanta Hawks 2017-18 shot data
## Boston Celtics 2017-18 shot data
## Cleveland Cavaliers 2017-18 shot data
## New Orleans Pelicans 2017-18 shot data
## Chicago Bulls 2017-18 shot data
## Dallas Mavericks 2017-18 shot data
## Denver Nuggets 2017-18 shot data
## Golden State Warriors 2017-18 shot data
## Houston Rockets 2017-18 shot data
## Los Angeles Clippers 2017-18 shot data
## Los Angeles Lakers 2017-18 shot data
## Miami Heat 2017-18 shot data
## Milwaukee Bucks 2017-18 shot data
## Minnesota Timberwolves 2017-18 shot data
## Brooklyn Nets 2017-18 shot data
## New York Knicks 2017-18 shot data
## Orlando Magic 2017-18 shot data
## Indiana Pacers 2017-18 shot data
## Philadelphia 76ers 2017-18 shot data
## Phoenix Suns 2017-18 shot data
## Portland Trail Blazers 2017-18 shot data
## Sacramento Kings 2017-18 shot data
## San Antonio Spurs 2017-18 shot data
## Oklahoma City Thunder 2017-18 shot data
## Toronto Raptors 2017-18 shot data
## Utah Jazz 2017-18 shot data
## Memphis Grizzlies 2017-18 shot data
## Washington Wizards 2017-18 shot data
## Detroit Pistons 2017-18 shot data
## Charlotte Hornets 2017-18 shot data
## Atlanta Hawks 2018-19 shot data
## Boston Celtics 2018-19 shot data
## Cleveland Cavaliers 2018-19 shot data
## New Orleans Pelicans 2018-19 shot data
## Chicago Bulls 2018-19 shot data
## Dallas Mavericks 2018-19 shot data
## Denver Nuggets 2018-19 shot data
## Golden State Warriors 2018-19 shot data
## Houston Rockets 2018-19 shot data
## Los Angeles Clippers 2018-19 shot data
## Los Angeles Lakers 2018-19 shot data
## Miami Heat 2018-19 shot data
## Milwaukee Bucks 2018-19 shot data
## Minnesota Timberwolves 2018-19 shot data
## Brooklyn Nets 2018-19 shot data
## New York Knicks 2018-19 shot data
## Orlando Magic 2018-19 shot data
## Indiana Pacers 2018-19 shot data
## Philadelphia 76ers 2018-19 shot data
## Phoenix Suns 2018-19 shot data
## Portland Trail Blazers 2018-19 shot data
## Sacramento Kings 2018-19 shot data
## San Antonio Spurs 2018-19 shot data
## Oklahoma City Thunder 2018-19 shot data
## Toronto Raptors 2018-19 shot data
## Utah Jazz 2018-19 shot data
## Memphis Grizzlies 2018-19 shot data
## Washington Wizards 2018-19 shot data
## Detroit Pistons 2018-19 shot data
## Charlotte Hornets 2018-19 shot data
## Atlanta Hawks 2019-20 shot data
## Boston Celtics 2019-20 shot data
## Cleveland Cavaliers 2019-20 shot data
## New Orleans Pelicans 2019-20 shot data
## Chicago Bulls 2019-20 shot data
## Dallas Mavericks 2019-20 shot data
## Denver Nuggets 2019-20 shot data
## Golden State Warriors 2019-20 shot data
## Houston Rockets 2019-20 shot data
## Los Angeles Clippers 2019-20 shot data
## Los Angeles Lakers 2019-20 shot data
## Miami Heat 2019-20 shot data
## Milwaukee Bucks 2019-20 shot data
## Minnesota Timberwolves 2019-20 shot data
## Brooklyn Nets 2019-20 shot data
## New York Knicks 2019-20 shot data
## Orlando Magic 2019-20 shot data
## Indiana Pacers 2019-20 shot data
## Philadelphia 76ers 2019-20 shot data
## Phoenix Suns 2019-20 shot data
## Portland Trail Blazers 2019-20 shot data
## Sacramento Kings 2019-20 shot data
## San Antonio Spurs 2019-20 shot data
## Oklahoma City Thunder 2019-20 shot data
## Toronto Raptors 2019-20 shot data
## Utah Jazz 2019-20 shot data
## Memphis Grizzlies 2019-20 shot data
## Washington Wizards 2019-20 shot data
## Detroit Pistons 2019-20 shot data
## Charlotte Hornets 2019-20 shot data
## Atlanta Hawks 2020-21 shot data
## Boston Celtics 2020-21 shot data
## Cleveland Cavaliers 2020-21 shot data
## New Orleans Pelicans 2020-21 shot data
## Chicago Bulls 2020-21 shot data
## Dallas Mavericks 2020-21 shot data
## Denver Nuggets 2020-21 shot data
## Golden State Warriors 2020-21 shot data
## Houston Rockets 2020-21 shot data
## Los Angeles Clippers 2020-21 shot data
## Los Angeles Lakers 2020-21 shot data
## Miami Heat 2020-21 shot data
## Milwaukee Bucks 2020-21 shot data
## Minnesota Timberwolves 2020-21 shot data
## Brooklyn Nets 2020-21 shot data
## New York Knicks 2020-21 shot data
## Orlando Magic 2020-21 shot data
## Indiana Pacers 2020-21 shot data
## Philadelphia 76ers 2020-21 shot data
## Phoenix Suns 2020-21 shot data
## Portland Trail Blazers 2020-21 shot data
## Sacramento Kings 2020-21 shot data
## San Antonio Spurs 2020-21 shot data
## Oklahoma City Thunder 2020-21 shot data
## Toronto Raptors 2020-21 shot data
## Utah Jazz 2020-21 shot data
## Memphis Grizzlies 2020-21 shot data
## Washington Wizards 2020-21 shot data
## Detroit Pistons 2020-21 shot data
## Charlotte Hornets 2020-21 shot data
## Atlanta Hawks 2021-22 shot data
## Boston Celtics 2021-22 shot data
## Cleveland Cavaliers 2021-22 shot data
## New Orleans Pelicans 2021-22 shot data
## Chicago Bulls 2021-22 shot data
## Dallas Mavericks 2021-22 shot data
## Denver Nuggets 2021-22 shot data
## Golden State Warriors 2021-22 shot data
## Houston Rockets 2021-22 shot data
## Los Angeles Clippers 2021-22 shot data
## Los Angeles Lakers 2021-22 shot data
## Miami Heat 2021-22 shot data
## Milwaukee Bucks 2021-22 shot data
## Minnesota Timberwolves 2021-22 shot data
## Brooklyn Nets 2021-22 shot data
## New York Knicks 2021-22 shot data
## Orlando Magic 2021-22 shot data
## Indiana Pacers 2021-22 shot data
## Philadelphia 76ers 2021-22 shot data
## Phoenix Suns 2021-22 shot data
## Portland Trail Blazers 2021-22 shot data
## Sacramento Kings 2021-22 shot data
## San Antonio Spurs 2021-22 shot data
## Oklahoma City Thunder 2021-22 shot data
## Toronto Raptors 2021-22 shot data
## Utah Jazz 2021-22 shot data
## Memphis Grizzlies 2021-22 shot data
## Washington Wizards 2021-22 shot data
## Detroit Pistons 2021-22 shot data
## Charlotte Hornets 2021-22 shot data
## Atlanta Hawks 2022-23 shot data
## Boston Celtics 2022-23 shot data
## Cleveland Cavaliers 2022-23 shot data
## New Orleans Pelicans 2022-23 shot data
## Chicago Bulls 2022-23 shot data
## Dallas Mavericks 2022-23 shot data
## Denver Nuggets 2022-23 shot data
## Golden State Warriors 2022-23 shot data
## Houston Rockets 2022-23 shot data
## Los Angeles Clippers 2022-23 shot data
## Los Angeles Lakers 2022-23 shot data
## Miami Heat 2022-23 shot data
## Milwaukee Bucks 2022-23 shot data
## Minnesota Timberwolves 2022-23 shot data
## Brooklyn Nets 2022-23 shot data
## New York Knicks 2022-23 shot data
## Orlando Magic 2022-23 shot data
## Indiana Pacers 2022-23 shot data
## Philadelphia 76ers 2022-23 shot data
## Phoenix Suns 2022-23 shot data
## Portland Trail Blazers 2022-23 shot data
## Sacramento Kings 2022-23 shot data
## San Antonio Spurs 2022-23 shot data
## Oklahoma City Thunder 2022-23 shot data
## Toronto Raptors 2022-23 shot data
## Utah Jazz 2022-23 shot data
## Memphis Grizzlies 2022-23 shot data
## Washington Wizards 2022-23 shot data
## Detroit Pistons 2022-23 shot data
## Charlotte Hornets 2022-23 shot data
## Atlanta Hawks 2023-24 shot data
## Boston Celtics 2023-24 shot data
## Cleveland Cavaliers 2023-24 shot data
## New Orleans Pelicans 2023-24 shot data
## Chicago Bulls 2023-24 shot data
## Dallas Mavericks 2023-24 shot data
## Denver Nuggets 2023-24 shot data
## Golden State Warriors 2023-24 shot data
## Houston Rockets 2023-24 shot data
## Los Angeles Clippers 2023-24 shot data
## Los Angeles Lakers 2023-24 shot data
## Miami Heat 2023-24 shot data
## Milwaukee Bucks 2023-24 shot data
## Minnesota Timberwolves 2023-24 shot data
## Brooklyn Nets 2023-24 shot data
## New York Knicks 2023-24 shot data
## Orlando Magic 2023-24 shot data
## Indiana Pacers 2023-24 shot data
## Philadelphia 76ers 2023-24 shot data
## Phoenix Suns 2023-24 shot data
## Portland Trail Blazers 2023-24 shot data
## Sacramento Kings 2023-24 shot data
## San Antonio Spurs 2023-24 shot data
## Oklahoma City Thunder 2023-24 shot data
## Toronto Raptors 2023-24 shot data
## Utah Jazz 2023-24 shot data
## Memphis Grizzlies 2023-24 shot data
## Washington Wizards 2023-24 shot data
## Detroit Pistons 2023-24 shot data
## Charlotte Hornets 2023-24 shot data
# Combine the results into a single data frame if needed
combined_df <- do.call(rbind, results_list)
combined_df$dateGame <- as.Date(combined_df$dateGame, format = "%Y%m%d")
combined_3pt <- combined_df %>%
  filter(typeShot == "3PT Field Goal",
         typeAction != "Tip Shot",
         zoneBasic != "Mid-Range",
         zoneBasic != "In The Paint (Non-RA)", 
         zoneBasic != "Restricted Area",
         zoneBasic != "Backcourt")

data manipulation

clean_df <- combined_3pt %>%
  mutate(Type =
           case_when(
             str_detect(typeAction, "Jump Shot") ~ "Set",
             str_detect(typeAction, "Jump Bank Shot") ~ "Set",
             str_detect(typeAction, "Hook Shot") ~ "Set",
             str_detect(typeAction, "Putback Layup Shot") ~ "Set",
             str_detect(typeAction, "Fadeaway Jump Shot") ~ "Fadeaway",
             str_detect(typeAction, "Turnaround Fadeaway shot") ~ "Fadeaway",
             str_detect(typeAction, "Turnaround Fadeaway Bank Jump Shot") ~ "Fadeaway",
             str_detect(typeAction, "Turnaround Bank shot") ~ "Fadeaway",
             str_detect(typeAction, "Turnaround Bank Hook shot") ~ "Fadeaway",
             str_detect(typeAction, "Fadeaway Bank shot") ~ "Fadeaway",
             str_detect(typeAction, "Turnaround Jump Shot") ~ "Fadeaway",
             str_detect(typeAction, "Turnaround Hook Shot") ~ "Fadeaway",
             str_detect(typeAction, "Running Bank shot") ~ "Movement",
             str_detect(typeAction, "Running Jump Shot") ~ "Movement",
             str_detect(typeAction, "Floating Jump shot") ~ "Movement",
             str_detect(typeAction, "Driving Layup Shot") ~ "Movement",
             str_detect(typeAction, "Driving Bank Shot") ~ "Movement",
             str_detect(typeAction, "Driving Bank shot") ~ "Movement",
             str_detect(typeAction, "Driving Bank Hook Shot") ~ "Movement",
             str_detect(typeAction, "Driving Hook Shot") ~ "Movement",
             str_detect(typeAction, "Driving Floating Jump Shot") ~ "Movement",
             str_detect(typeAction, "Driving Floating Bank Jump Shot") ~ "Movement",
             str_detect(typeAction, "Layup Shot") ~ "Movement",
             str_detect(typeAction, "Tip Shot") ~ "Movement",
             str_detect(typeAction, "Running Hook Shot") ~ "Movement",
             str_detect(typeAction, "Driving Jump shot") ~ "Fadeaway",
             str_detect(typeAction, "Pullup Jump shot") ~ "Pullup",
             str_detect(typeAction, "Running Pull-up Jump Shot") ~ "Pullup",
             str_detect(typeAction, "Pullup Bank shot") ~ "Pullup",
             str_detect(typeAction, "Step Back Jump shot") ~ "Step Back",
             str_detect(typeAction, "Step Back Bank Jump shot") ~ "Step Back",
             TRUE ~ "other"
           )) %>%
  mutate(Result = case_when(
    str_detect(typeEvent, "Made Shot") ~ 1,
    str_detect(typeEvent, "Missed Shot") ~ 0,
    TRUE ~ NA_real_
  ))
game_logs <- clean_df %>%
  group_by(idPlayer, namePlayer, idGame, zoneBasic, Type, dateGame) %>%
  summarize(
    makes = sum(Result),
    attempts = n()
  ) %>%
  ungroup()

cor_date <- clean_df %>%
  group_by(dateGame, zoneBasic, Type) %>%
  summarize(
    makes = sum(Result),
    attempts = n()
  ) %>%
  ungroup()

percents <- game_logs %>%
  filter(dateGame > '2022-10-01') %>%
  group_by(zoneBasic, Type) %>%
  summarize(
    total_makes = sum(makes),
    total_attempts = sum(attempts)
  ) %>%
  mutate(
    percent = total_makes / total_attempts
  ) %>%
  ungroup()  %>%
  filter(total_attempts > 50)
percents_player <- game_logs %>%
  filter(dateGame > '2022-10-01') %>%
  group_by(zoneBasic, Type, idPlayer, namePlayer) %>%
  summarize(
    total_makes = sum(makes),
    total_attempts = sum(attempts)
  ) %>%
  mutate(
    percent = total_makes / total_attempts
  ) %>%
  ungroup() %>%
  filter(total_attempts > 10) %>%
  filter(zoneBasic == "Above the Break 3")
percents_player$total_attempts <- as.numeric(percents_player$total_attempts)
corners <- game_logs %>%
  filter(dateGame > '2022-10-01') %>%
  group_by(zoneBasic, idPlayer, namePlayer) %>%
  summarize(
    total_makes = sum(makes),
    total_attempts = sum(attempts)
  ) %>%
  mutate(
    percent = total_makes / total_attempts
  ) %>%
  ungroup() %>%
  filter(zoneBasic != "Above the Break 3")
corner_league <- game_logs %>%
  filter(dateGame > '2022-10-01') %>%
  group_by(zoneBasic) %>%
  summarize(
    total_makes = sum(makes),
    total_attempts = sum(attempts)
  ) %>%
  mutate(
    percent = total_makes / total_attempts
  ) %>%
  ungroup() %>%
  filter(zoneBasic != "Above the Break 3")
corners$total_attempts <- as.numeric(corners$total_attempts)

correlation

game_logs2 <- game_logs %>%
  separate(dateGame, into = c("year", "month", "day"), sep = '-', remove = FALSE)
percents_player2 <- game_logs2 %>%
  group_by(zoneBasic, Type, month, year) %>%
  summarize(
    total_makes = sum(makes),
    total_attempts = sum(attempts)
  ) %>%
  mutate(
    percent = total_makes / total_attempts
  ) %>%
  ungroup() %>%
  select(zoneBasic, Type, percent, month, year) %>%
  pivot_wider(
    names_from = c(zoneBasic, Type),
    values_from = percent
  )
replace_with_mean <- function(x) {
  mean_val <- mean(x, na.rm = TRUE)  # Compute the mean of the column
  x[is.na(x)] <- mean_val  # Replace NA values with the mean
  return(x)
}

# Replace NA values with the mean of each column
data_cleaned <- percents_player2 %>%
  mutate_at(vars(starts_with("Right Corner 3_")), replace_with_mean) %>%
  mutate_at(vars(starts_with("Left Corner 3_")), replace_with_mean) %>%
  select(-c(month, year))

corr_J <- cor(as.matrix(data_cleaned))

anchor function - regular season

above_break_pull_up <- percents[[3, 5]]
above_break_stepback <- percents[[5, 5]]
above_break_set <- percents[[4, 5]]
corner_left <- corner_league[[1, 4]]
corner_right <- corner_league[[2,4]]

# function for anchored shooting but does not adjust by other 3pt shots
shooting <- function(x) {
  df <- percents_player %>%
    filter(namePlayer == x)
  
  df <- df %>%
    mutate( anchored = case_when(
      str_detect(Type, "Pullup") ~ ((total_makes + (100 * above_break_pull_up))/ (100 + total_attempts)),
      str_detect(Type, "Step Back") ~ ((total_makes + (100 * above_break_stepback))/ (100 + total_attempts)),
      str_detect(Type, "Set") ~ ((total_makes + (100 * above_break_set))/ (100 + total_attempts))
  )) 
  df$zoneBasic <- paste(df$zoneBasic, df$Type)
  df <- df %>%
    select(zoneBasic,
           idPlayer,
           namePlayer,
           total_makes,
           total_attempts,
           percent,
           anchored)


  df_2 <- corners %>%
    filter(namePlayer == x)
  
  df_2 <- df_2 %>%
    mutate(anchored = case_when(
      str_detect(zoneBasic, "Left Corner 3") ~ ((total_makes + (100 * corner_left))/ (100 + total_attempts)),
      str_detect(zoneBasic, "Right Corner 3") ~ ((total_makes + (100 * corner_right))/ (100 + total_attempts))
    ))
  
  df_3 <- rbind(df, df_2)
  return(df_3)
}

shooting("LeBron James")
## # A tibble: 5 × 7
##   zoneBasic      idPlayer namePlayer total_makes total_attempts percent anchored
##   <chr>             <dbl> <chr>            <dbl>          <dbl>   <dbl>    <dbl>
## 1 Above the Bre…     2544 LeBron Ja…          56            151   0.371    0.358
## 2 Above the Bre…     2544 LeBron Ja…         169            443   0.381    0.378
## 3 Above the Bre…     2544 LeBron Ja…          13             66   0.197    0.292
## 4 Left Corner 3      2544 LeBron Ja…          10             34   0.294    0.362
## 5 Right Corner 3     2544 LeBron Ja…          20             42   0.476    0.415

function for adjusted anchors

shooting_adj <- function(x) {
  df <- percents_player %>%
    filter(namePlayer == x)
  
  df <- df %>%
    mutate( anchored = case_when(
      str_detect(Type, "Pullup") ~ ((total_makes + (100 * above_break_pull_up))/ (100 + total_attempts)),
      str_detect(Type, "Step Back") ~ ((total_makes + (100 * above_break_stepback))/ (100 + total_attempts)),
      str_detect(Type, "Set") ~ ((total_makes + (100 * above_break_set))/ (100 + total_attempts))
  )) 
  df$zoneBasic <- paste(df$zoneBasic, df$Type)
  df <- df %>%
    select(zoneBasic,
           idPlayer,
           namePlayer,
           total_makes,
           total_attempts,
           percent,
           anchored)


  df_2 <- corners %>%
    filter(namePlayer == x)
  
  df_2 <- df_2 %>%
    mutate(anchored = case_when(
      str_detect(zoneBasic, "Left Corner 3") ~ ((total_makes + (100 * corner_left))/ (100 + total_attempts)),
      str_detect(zoneBasic, "Right Corner 3") ~ ((total_makes + (100 * corner_right))/ (100 + total_attempts))
    ))
  
  df_3 <- rbind(df, df_2)
  df_3 <- df_3 %>%
    mutate(adjusted = 
             ifelse(zoneBasic == "Above the Break 3 Pullup",
                    ( (anchored * 100) + (50 * df_3$anchored[df_3$zoneBasic == "Above the Break 3 Step Back"]) + (50 * df_3$anchored[df_3$zoneBasic == "Above the Break 3 Set"]) ) / 200,
             ifelse(zoneBasic == "Above the Break 3 Step Back",
                    ( (anchored * 100) + (50 * df_3$anchored[df_3$zoneBasic == "Above the Break 3 Pullup"]) + (50 * df_3$anchored[df_3$zoneBasic == "Above the Break 3 Set"]) ) / 200,
             ifelse(zoneBasic == "Above the Break 3 Set",
                    ( (anchored * 140) + (30 * df_3$anchored[df_3$zoneBasic == "Above the Break 3 Step Back"]) + (30 * df_3$anchored[df_3$zoneBasic == "Above the Break 3 Pullup"]) ) / 200,
             ifelse(zoneBasic == "Right Corner 3",
                    ( (anchored * 100) + (65 * df_3$anchored[df_3$zoneBasic == "Left Corner 3"]) + (35 * df_3$anchored[df_3$zoneBasic == "Above the Break 3 Set"]) ) / 200,
                    ( (anchored * 100) + (65 * df_3$anchored[df_3$zoneBasic == "Right Corner 3"]) + (35 * df_3$anchored[df_3$zoneBasic == "Above the Break 3 Set"]) ) / 200
             )))))
  df_3 <- df_3 %>%
    select(namePlayer,
           zoneBasic,
           total_makes,
           total_attempts,
           percent,
           anchored,
           adjusted) %>%
    mutate_at(vars(c(percent, anchored, adjusted)), round, digits = 4)
  return(df_3)
}

shooting_adj("Nikola Jokic")
## # A tibble: 5 × 7
##   namePlayer   zoneBasic    total_makes total_attempts percent anchored adjusted
##   <chr>        <chr>              <dbl>          <dbl>   <dbl>    <dbl>    <dbl>
## 1 Nikola Jokic Above the B…          14             28   0.5      0.374    0.372
## 2 Nikola Jokic Above the B…         113            306   0.369    0.368    0.369
## 3 Nikola Jokic Above the B…           9             20   0.45     0.371    0.371
## 4 Nikola Jokic Left Corner…           2              7   0.286    0.379    0.375
## 5 Nikola Jokic Right Corne…           1              7   0.143    0.374    0.374

playoff adjusted anchors

above_break_pull_up_p <- .3317
above_break_stepback_p <- .3213
above_break_set_p <- .3577
corner_left_p <- .3788
corner_right_p <- .3766

shooting_adj_pf <- function(x) {
  df <- percents_player %>%
    filter(namePlayer == x)
  
  df <- df %>%
    mutate( anchored = case_when(
      str_detect(Type, "Pullup") ~ ((total_makes + (100 * above_break_pull_up_p))/ (100 + total_attempts)),
      str_detect(Type, "Step Back") ~ ((total_makes + (100 * above_break_stepback_p))/ (100 + total_attempts)),
      str_detect(Type, "Set") ~ ((total_makes + (100 * above_break_set_p))/ (100 + total_attempts))
  )) 
  df$zoneBasic <- paste(df$zoneBasic, df$Type)
  df <- df %>%
    select(zoneBasic,
           idPlayer,
           namePlayer,
           total_makes,
           total_attempts,
           percent,
           anchored)


  df_2 <- corners %>%
    filter(namePlayer == x)
  
  df_2 <- df_2 %>%
    mutate(anchored = case_when(
      str_detect(zoneBasic, "Left Corner 3") ~ ((total_makes + (100 * corner_left_p))/ (100 + total_attempts)),
      str_detect(zoneBasic, "Right Corner 3") ~ ((total_makes + (100 * corner_right_p))/ (100 + total_attempts))
    ))
  
  df_3 <- rbind(df, df_2)
  df_3 <- df_3 %>%
    mutate(adjusted = 
             ifelse(zoneBasic == "Above the Break 3 Pullup",
                    ( (anchored * 100) + (50 * df_3$anchored[df_3$zoneBasic == "Above the Break 3 Step Back"]) + (50 * df_3$anchored[df_3$zoneBasic == "Above the Break 3 Set"]) ) / 200,
             ifelse(zoneBasic == "Above the Break 3 Step Back",
                    ( (anchored * 100) + (50 * df_3$anchored[df_3$zoneBasic == "Above the Break 3 Pullup"]) + (50 * df_3$anchored[df_3$zoneBasic == "Above the Break 3 Set"]) ) / 200,
             ifelse(zoneBasic == "Above the Break 3 Set",
                    ( (anchored * 140) + (30 * df_3$anchored[df_3$zoneBasic == "Above the Break 3 Step Back"]) + (30 * df_3$anchored[df_3$zoneBasic == "Above the Break 3 Pullup"]) ) / 200,
             ifelse(zoneBasic == "Right Corner 3",
                    ( (anchored * 100) + (65 * df_3$anchored[df_3$zoneBasic == "Left Corner 3"]) + (35 * df_3$anchored[df_3$zoneBasic == "Above the Break 3 Set"]) ) / 200,
                    ( (anchored * 100) + (65 * df_3$anchored[df_3$zoneBasic == "Right Corner 3"]) + (35 * df_3$anchored[df_3$zoneBasic == "Above the Break 3 Set"]) ) / 200
             )))))
  df_3 <- df_3 %>%
    select(namePlayer,
           zoneBasic,
           total_makes,
           total_attempts,
           percent,
           anchored,
           adjusted) %>%
    mutate_at(vars(c(percent, anchored, adjusted)), round, digits = 4)
  return(df_3)
}

shooting_adj_pf("Nikola Jokic")
## # A tibble: 5 × 7
##   namePlayer   zoneBasic    total_makes total_attempts percent anchored adjusted
##   <chr>        <chr>              <dbl>          <dbl>   <dbl>    <dbl>    <dbl>
## 1 Nikola Jokic Above the B…          14             28   0.5      0.368    0.362
## 2 Nikola Jokic Above the B…         113            306   0.369    0.366    0.363
## 3 Nikola Jokic Above the B…           9             20   0.45     0.343    0.355
## 4 Nikola Jokic Left Corner…           2              7   0.286    0.373    0.368
## 5 Nikola Jokic Right Corne…           1              7   0.143    0.361    0.366
shooting_adj("LeBron James") %>%
  reactable(theme = espn()) %>%
  add_title("LeBron James adjusted 3pt shooting - Regular Season")

LeBron James adjusted 3pt shooting - Regular Season

shooting_adj_pf("LeBron James") %>%
  reactable(theme = espn()) %>%
  add_title("LeBron James adjusted 3pt shooting - Playoffs")

LeBron James adjusted 3pt shooting - Playoffs