-
Notifications
You must be signed in to change notification settings - Fork 1
/
proj_prep.R
121 lines (95 loc) · 2.75 KB
/
proj_prep.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#' @title Create a proj_prep object
#'
#' @description given raw projection data, creates a proj_prep object
#'
#' @param proj_list named ('h', 'p') list of data frames, ie output of get_steamer
#' @examples
#'\dontrun{
#' cdf_mv <- mapvizieR(ex_CombinedAssessmentResults,
#' ex_CombinedStudentsBySchool)
#'
#' is.mapvizieR(cdf_mv)
#' }
#' @export
proj_prep <- function(proj_list, ...) UseMethod("proj_prep")
#' @export
proj_prep.default <- function(
proj_list,
verbose = TRUE,
...) {
initial_message <- paste0(
'building a projection_prep object!\n',
sprintf('using the following hitting categories: %s',
paste(user_settings$h, collapse = ', ')
),
'\n',
sprintf('using the following pitching categories: %s',
paste(user_settings$p, collapse = ', ')
),
'\n',
'to change any of these settings, run `set_defaults()`.\n'
)
message(initial_message)
#prep and limit to target stats
pp_filtered <- limit_proj_vars(proj_list)
#zscore
h_zscore <- zscore(
proj_list = pp_filtered,
hit_pitch = 'h',
limit_player_pool = TRUE
)
h_with_zscore <- pp_filtered$h %>%
dplyr::left_join(h_zscore, by = 'mlbid')
p_zscore <- zscore(
proj_list = pp_filtered,
hit_pitch = 'p',
limit_player_pool = TRUE
)
p_with_zscore <- pp_filtered$p %>%
dplyr::left_join(p_zscore, by = 'mlbid')
#find replacement by position
proj_list <- list('h' = h_with_zscore, 'p' = p_with_zscore)
h_replacement <- find_replacement(proj_list, 'h')
p_replacement <- find_replacement(proj_list, 'p')
#zscores with replacement position adjustments
#express as prices
#return
list(
'h' = h_with_zscore, 'p' = p_with_zscore,
'replacement' = c(h_replacement, p_replacement)
)
}
#' What common variables do all projection prep data frames need to have?
#'
#' @return vector with names of common variables
#' @export
common_proj_prep_vars <- function() {
c('mlbid', 'fullname', 'firstname', 'lastname',
'position', 'priority_pos', 'projection_name'
)
}
#' @export
#' @rdname common_proj_prep_vars
common_proj_prep_h_vars <- function() {
c(common_proj_prep_vars(), 'ab')
}
#' @export
#' @rdname common_proj_prep_vars
common_proj_prep_p_vars <- function() {
c(common_proj_prep_vars(), 'ip')
}
#' select relevant raw projection stats
#'
#' @inheritParams proj_prep
#'
#' @return list of data frames with relevant variables selected
#' @export
limit_proj_vars <- function(proj_list) {
h_stats <- c(common_proj_prep_h_vars(), user_settings$h)
h_filtered <- proj_list$h %>%
dplyr::select(one_of(h_stats))
p_stats <- c(common_proj_prep_p_vars(), user_settings$p)
p_filtered <- proj_list$p %>%
dplyr::select(one_of(p_stats))
list('h' = h_filtered, 'p' = p_filtered)
}