Skip to content

Commit d5284f8

Browse files
committed
update to internal functions
1 parent 08cab3c commit d5284f8

3 files changed

Lines changed: 55 additions & 11 deletions

File tree

R/calc_projections.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ score_dst_pts_allowed = function(data_result, pts_bracket, is_actual = FALSE) {
210210
}, ppg, ppg_sd)
211211
df$dst_pts_allowed[!na_idx] = vapply(game_l, sum, numeric(1L))
212212
} else {
213-
df$dst_pts_allowed[!na_idx] = score_pts_bracket(df$dst_pts_allowed, pts_bracket)
213+
df$dst_pts_allowed[!na_idx] = score_pts_bracket(df$dst_pts_allowed[!na_idx], pts_bracket)
214214
}
215215
df$dst_pts_allowed
216216
}

R/helper_funcs.R

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,36 +277,44 @@ actual_points_scoring = function(season = NULL,
277277
stat_type = nflf_stat_type,
278278
season_type = season_type
279279
)
280+
280281
nflf_stat = nflf_stat %>%
281282
dplyr::filter(grepl(sub("+", "|", !!season_type, fixed = TRUE), .data$season_type))
282283

283-
284-
285284
# Loading pbp and filtering for season_type
286285
nflf_pbp = nflfastR::load_pbp(
287286
season = season,
288287
file_type = "rds"
289288
)
290289
nflf_pbp = nflf_pbp %>%
291-
dplyr::filter(grepl(sub("+", "|", !!season_type, fixed = TRUE), .data$season_type))
290+
dplyr::filter(grepl(sub("+", "|", !!season_type, fixed = TRUE), .data$season_type)) %>%
291+
dplyr::mutate(home_team = rename_vec(home_team, unlist(team_corrections)),
292+
away_team = rename_vec(away_team, unlist(team_corrections)))
292293

293294

294295

295296
# Adding week if summary level = "season"
296297
if(isTRUE(summary_level %in% c("season"))) {
297298
nflf_stat$week = 0L
298299
nflf_pbp$week = 0L
300+
} else {
301+
nflf_stat$opponent_team = rename_vec(nflf_stat$opponent_team, unlist(team_corrections))
299302
}
300303

301-
# Updating columns names to match our standard column names
304+
# browser()
305+
302306

307+
# Updating columns names to match our standard column names
303308
if(isTRUE(stat_type == "player")) {
304309
names(nflf_stat) = rename_vec(names(nflf_stat), nflfastr_player_cols)
310+
nflf_stat$recent_team = rename_vec(nflf_stat$recent_team, unlist(team_corrections))
311+
305312
} else {
306313
# names(nflfastr_player_cols) = gsub("idp", "dst", names(nflfastr_player_cols), fixed = TRUE)
307314
nflfastr_player_cols = gsub("idp", "dst", nflfastr_player_cols, fixed = TRUE)
308315
names(nflf_stat) = rename_vec(names(nflf_stat), nflfastr_player_cols)
309316

317+
nflf_stat$team = rename_vec(nflf_stat$team, unlist(team_corrections))
310318
nflf_stat$gsis_id = nflf_stat$team
311319
nflf_stat$pos = "DST"
312320

@@ -366,6 +374,7 @@ actual_points_scoring = function(season = NULL,
366374
rec_100_yds = as.integer(rec_yds >= 100, na.rm = TRUE),
367375
rec_150_yds = as.integer(rec_yds >= 150, na.rm = TRUE),
368376
rec_200_yds = as.integer(rec_yds >= 200, na.rm = TRUE),
377+
pos = ifelse(pos == "SPEC", "K", pos)
369378
)
370379

371380
data_result = split.data.frame(nflf_stat, nflf_stat$pos)
@@ -388,10 +397,27 @@ actual_points_scoring = function(season = NULL,
388397
.groups = "drop")
389398

390399
actual_allowed = dplyr::bind_rows(
391-
dplyr::select(temp_scores, season_year = season, week, team = home_team, dst_pts_allowed = total_away_score),
392-
dplyr::select(temp_scores, season_year = season, week, team = away_team, dst_pts_allowed = total_home_score)
400+
dplyr::select(
401+
temp_scores, season_year = season, week, team = home_team,
402+
team_pts_scored = total_home_score, opp_pts_scored = total_away_score,
403+
dst_pts_allowed = total_away_score
404+
),
405+
dplyr::select(
406+
temp_scores, season_year = season, week, team = away_team,
407+
team_pts_scored = total_away_score, opp_pts_scored = total_home_score,
408+
dst_pts_allowed = total_home_score
409+
)
393410
)
394411

412+
if(summary_level == "season") {
413+
actual_allowed = actual_allowed %>%
414+
dplyr::group_by(season_year, week, team) %>%
415+
dplyr::summarise(team_pts_scored = sum(team_pts_scored, na.rm = TRUE),
416+
opp_pts_scored = sum(opp_pts_scored, na.rm = TRUE),
417+
dst_pts_allowed = sum(dst_pts_allowed, na.rm = TRUE),
418+
.groups = "drop")
419+
}
420+
395421
data_result[["DST"]] = data_result[["DST"]] %>%
396422
dplyr::left_join(actual_allowed, c("season_year", "week", "team")) %>%
397423
dplyr::select(-gsis_id, -pos)
@@ -402,20 +428,37 @@ actual_points_scoring = function(season = NULL,
402428
if(isTRUE(stat_type == "dst")) {
403429
scoring_rules = scoring_rules[c("ret", "dst", "pts_bracket")]
404430
}
405-
406-
407431
data_result[] = source_points(data_result, scoring_rules, return_data_result = TRUE, is_actual = TRUE)
408432

409433
nflf_out_df = dplyr::bind_rows(data_result)
410434

411435

436+
412437
if(isFALSE(rename_colums)) {
413438
switch_back_names = setNames(names(nflfastr_player_cols), nflfastr_player_cols)
414439

415440
names(nflf_out_df) = rename_vec(names(nflf_out_df), switch_back_names)
416441
}
417442
nflf_out_df
418443
}
444+
# devtools::load_all()
445+
# df_out = actual_points_scoring(
446+
# season = 2019,
447+
# summary_level = c("season"),
448+
# stat_type = c("player", "dst", "team"),
449+
# season_type = c("REG", "POST", "REG+POST"),
450+
# scoring_rules = NULL,
451+
# vor_baseline = NULL,
452+
# rename_colums = TRUE
453+
# )
454+
455+
456+
457+
458+
459+
460+
461+
419462

420463

421464

R/recode_vars.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ pos_corrections = list(Def = "DST", D = "DST", DEF = "DST", "D/ST" = "DST", PK =
55
OLB = "LB", SS = "DB", FB = "RB")
66

77
team_corrections <- list(KCC = "KC", SFO = "SF", TBB = "TB", NEP = "NE", RAM = "LAR",
8-
LA = "LAR", SDC = "SD", ARZ = "ARI", NOR = "NO", GBP = "GB",
8+
LA = "LAR", SDC = "LAC", ARZ = "ARI", NOR = "NO", GBP = "GB",
99
JAX = "JAC", WSH = "WAS", HST = "HOU", CLV = "CLE", BLT = "BAL",
10-
NWE = "NE", NOS = "NO", LVR = "LV")
10+
NWE = "NE", NOS = "NO", LVR = "LV", STL = "LAR", OAK = "LV",
11+
SD = "LAC")
1112

1213

1314
nflTeam.abb <- c("ARI", "ATL", "BAL", "BUF", "CAR", "CHI", "CIN", "CLE",

0 commit comments

Comments
 (0)