Skip to content

Commit 86e1163

Browse files
committed
remove unnecessary pipes and combine filters
1 parent 1202646 commit 86e1163

File tree

4 files changed

+38
-27
lines changed

4 files changed

+38
-27
lines changed

backfill_corrections/delphiBackfillCorrection/R/beta_prior_estimation.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ objective <- function(theta, x, prob, ...) {
5353
#' @param model_save_dir directory containing trained models
5454
#'
5555
#' @importFrom stats nlm predict
56-
#' @importFrom dplyr %>% filter
56+
#' @importFrom dplyr filter
5757
#' @importFrom quantgen quantile_lasso
5858
#'
5959
est_priors <- function(train_data, prior_test_data, geo, value_type, dw, taus,
@@ -63,8 +63,8 @@ est_priors <- function(train_data, prior_test_data, geo, value_type, dw, taus,
6363
model_save_dir, start=c(0, log(10)),
6464
base_pseudo_denom=1000, base_pseudo_num=10,
6565
train_models = TRUE, make_predictions = TRUE) {
66-
sub_train_data <- train_data %>% filter(train_data[[dw]] == 1)
67-
sub_test_data <- prior_test_data %>% filter(prior_test_data[[dw]] == 1)
66+
sub_train_data <- filter(train_data, train_data[[dw]] == 1)
67+
sub_test_data <- filter(prior_test_data, prior_test_data[[dw]] == 1)
6868
if (nrow(sub_test_data) == 0) {
6969
pseudo_denom <- base_pseudo_denom
7070
pseudo_num <- base_pseudo_num

backfill_corrections/delphiBackfillCorrection/R/main.R

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,17 @@ run_backfill <- function(df, params,
112112
)
113113
}
114114
combined_df <- add_params_for_dates(combined_df, refd_col, lag_col)
115-
combined_df <- combined_df %>% filter(lag < params$ref_lag)
115+
combined_df <- filter(combined_df, lag < params$ref_lag)
116116

117-
geo_train_data <- combined_df %>%
118-
filter(issue_date < params$training_end_date) %>%
119-
filter(target_date <= params$training_end_date) %>%
120-
filter(target_date > params$training_start_date) %>%
117+
geo_train_data <- filter(combined_df,
118+
issue_date < params$training_end_date,
119+
target_date <= params$training_end_date,
120+
target_date > params$training_start_date,
121+
) %>%
121122
drop_na()
122-
geo_test_data <- combined_df %>%
123-
filter(issue_date %in% params$test_dates) %>%
123+
geo_test_data <- filter(combined_df,
124+
issue_date %in% params$test_dates
125+
) %>%
124126
drop_na()
125127

126128
if (nrow(geo_test_data) == 0) {
@@ -134,9 +136,10 @@ run_backfill <- function(df, params,
134136

135137
if (value_type == "fraction") {
136138
# Use beta prior approach to adjust fractions
137-
geo_prior_test_data = combined_df %>%
138-
filter(issue_date > min(params$test_dates) - 7) %>%
139-
filter(issue_date <= max(params$test_dates))
139+
geo_prior_test_data = filter(combined_df,
140+
issue_date > min(params$test_dates) - 7,
141+
issue_date <= max(params$test_dates)
142+
)
140143
updated_data <- frac_adj(geo_train_data, geo_test_data, geo_prior_test_data,
141144
indicator = indicator, signal = signal,
142145
geo_level = geo_level, signal_suffix = signal_suffix,

backfill_corrections/delphiBackfillCorrection/R/model.R

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#' @param geo_train_data training data for a certain location
66
#' @param geo_test_data testing data for a certain location
77
#'
8+
#' @importFrom dplyr filter
9+
#'
810
#' @export
911
data_filteration <- function(test_lag, geo_train_data, geo_test_data, lag_pad) {
1012
if (test_lag <= 14){
@@ -20,12 +22,14 @@ data_filteration <- function(test_lag, geo_train_data, geo_test_data, lag_pad) {
2022
test_lag_pad1=8
2123
test_lag_pad2=9
2224
}
23-
train_data = geo_train_data %>%
24-
filter(lag >= test_lag - test_lag_pad ) %>%
25-
filter(lag <= test_lag + test_lag_pad )
26-
test_data = geo_test_data %>%
27-
filter(lag >= test_lag - test_lag_pad1 ) %>%
28-
filter(lag <= test_lag + test_lag_pad2)
25+
train_data = filter(geo_train_data,
26+
lag >= test_lag - test_lag_pad,
27+
lag <= test_lag + test_lag_pad
28+
)
29+
test_data = filter(geo_test_data,
30+
lag >= test_lag - test_lag_pad1,
31+
lag <= test_lag + test_lag_pad2
32+
)
2933

3034
return (list(train_data, test_data))
3135
}

backfill_corrections/delphiBackfillCorrection/R/preprocessing.R

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ fill_rows <- function(df, refd_col, lag_col, min_refd, max_refd, ref_lag) {
2727
# +30 to have values for calculating 7-day averages
2828
lags <- min(df[[lag_col]]): (ref_lag + 30)
2929
refds <- seq(min_refd, max_refd, by="day") # Full list reference date
30-
row_inds_df <- as.data.frame(crossing(refds, lags)) %>%
31-
setNames(c(refd_col, lag_col))
30+
row_inds_df <- setNames(
31+
as.data.frame(crossing(refds, lags)),
32+
c(refd_col, lag_col)
33+
)
3234
df_new = merge(x=df, y=row_inds_df,
3335
by=c(refd_col, lag_col), all.y=TRUE)
3436
return (df_new)
@@ -54,13 +56,14 @@ fill_missing_updates <- function(df, value_col, refd_col, lag_col) {
5456
if (any(diff(pivot_df[[lag_col]]) != 1)) {
5557
stop("Risk exists in forward filling")
5658
}
57-
pivot_df <- pivot_df %>% fill(everything(), .direction="down")
59+
pivot_df <- fill(pivot_df, everything(), .direction="down")
5860

5961
# Fill NAs with 0s
6062
pivot_df[is.na(pivot_df)] <- 0
6163

62-
backfill_df <- pivot_df %>%
63-
pivot_longer(-lag_col, values_to="value_raw", names_to=refd_col)
64+
backfill_df <- pivot_longer(pivot_df,
65+
-lag_col, values_to="value_raw", names_to=refd_col
66+
)
6467
backfill_df[[refd_col]] = as.Date(backfill_df[[refd_col]])
6568

6669
return (as.data.frame(backfill_df))
@@ -80,8 +83,9 @@ get_7dav <- function(pivot_df, refd_col) {
8083
if (col == refd_col) next
8184
pivot_df[, col] <- rollmeanr(pivot_df[, col], 7, align="right", fill=NA)
8285
}
83-
backfill_df <- pivot_df %>%
84-
pivot_longer(-refd_col, values_to="value_raw", names_to="issue_date")
86+
backfill_df <- pivot_longer(pivot_df,
87+
-refd_col, values_to="value_raw", names_to="issue_date"
88+
)
8589
backfill_df[[refd_col]] = as.Date(backfill_df[[refd_col]])
8690
backfill_df[["issue_date"]] = as.Date(backfill_df[["issue_date"]])
8791
return (as.data.frame(backfill_df))
@@ -205,7 +209,7 @@ add_7davs_and_target <- function(df, value_col, refd_col, lag_col, ref_lag) {
205209
backfill_df$log_7dav_slope = backfill_df$log_value_7dav - backfill_df$log_value_prev_7dav
206210

207211
# Remove invalid rows
208-
backfill_df <- backfill_df %>% drop_na(c(lag_col))
212+
backfill_df <- drop_na(backfill_df, c(lag_col))
209213

210214
return (as.data.frame(backfill_df))
211215
}

0 commit comments

Comments
 (0)