Skip to content

Commit 1202646

Browse files
committed
remove rlang pronouns for speed
1 parent 96f2ecf commit 1202646

File tree

7 files changed

+33
-46
lines changed

7 files changed

+33
-46
lines changed

backfill_corrections/delphiBackfillCorrection/DESCRIPTION

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ Imports:
2525
tidyr,
2626
zoo,
2727
utils,
28-
rlang,
2928
parallel
3029
Suggests:
3130
knitr (>= 1.15),

backfill_corrections/delphiBackfillCorrection/NAMESPACE

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ importFrom(dplyr,filter)
3333
importFrom(dplyr,group_by)
3434
importFrom(dplyr,group_split)
3535
importFrom(dplyr,if_else)
36-
importFrom(dplyr,mutate)
3736
importFrom(dplyr,pull)
38-
importFrom(dplyr,rename)
3937
importFrom(dplyr,select)
4038
importFrom(dplyr,starts_with)
4139
importFrom(dplyr,summarize)
@@ -50,9 +48,6 @@ importFrom(lubridate,year)
5048
importFrom(parallel,detectCores)
5149
importFrom(quantgen,quantile_lasso)
5250
importFrom(readr,write_csv)
53-
importFrom(rlang,":=")
54-
importFrom(rlang,.data)
55-
importFrom(rlang,.env)
5651
importFrom(stats,coef)
5752
importFrom(stats,nlm)
5853
importFrom(stats,pbeta)

backfill_corrections/delphiBackfillCorrection/R/io.R

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,15 @@ read_data <- function(input_file) {
1313
#' Make sure data contains a `geo_value` field
1414
#'
1515
#' @template df-template
16-
#'
17-
#' @importFrom dplyr rename select
18-
#' @importFrom rlang .data
1916
fips_to_geovalue <- function(df) {
2017
if ( !("geo_value" %in% colnames(df)) ) {
2118
if ( !("fips" %in% colnames(df)) ) {
2219
stop("Either `fips` or `geo_value` field must be available")
2320
}
24-
df <- rename(df, geo_value = .data$fips)
21+
df$geo_value <- df$fips
2522
}
2623
if ( "fips" %in% colnames(df) ) {
27-
df <- select(df, -.data$fips)
24+
df$fips <- NULL
2825
}
2926
return(df)
3027
}

backfill_corrections/delphiBackfillCorrection/R/main.R

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@
99
#' @template indicator-template
1010
#' @template signal-template
1111
#'
12-
#' @importFrom dplyr %>% filter select group_by summarize across everything group_split ungroup
12+
#' @importFrom dplyr %>% filter group_by summarize across everything group_split ungroup
1313
#' @importFrom tidyr drop_na
14-
#' @importFrom rlang .data .env
1514
#'
1615
#' @export
1716
run_backfill <- function(df, params,
1817
refd_col = "time_value", lag_col = "lag", issued_col = "issue_date",
1918
signal_suffixes = c(""), indicator = "", signal = "") {
20-
df <- filter(df, .data$lag < params$ref_lag + 30) # a rough filtration to save memory
19+
df <- filter(df, lag < params$ref_lag + 30) # a rough filtration to save memory
2120

2221
geo_levels <- params$geo_levels
2322
if ("state" %in% geo_levels) {
@@ -34,16 +33,17 @@ run_backfill <- function(df, params,
3433
# Aggregate counties up to state level
3534
agg_cols <- c("geo_value", issued_col, refd_col, lag_col)
3635
# Sum all non-agg columns. Summarized columns keep original names
36+
df$geo_value <- df$state_id
37+
df$state_id <- NULL
3738
df <- df %>%
38-
select(-.data$geo_value, geo_value = .data$state_id) %>%
3939
group_by(across(agg_cols)) %>%
4040
summarize(across(everything(), sum)) %>%
4141
ungroup()
4242
}
4343
if (geo_level == "county") {
4444
# Keep only 200 most populous (within the US) counties
4545
top_200_geos <- get_populous_counties()
46-
df <- filter(df, .data$geo_value %in% top_200_geos)
46+
df <- filter(df, geo_value %in% top_200_geos)
4747
}
4848

4949
test_data_list <- list()
@@ -58,7 +58,7 @@ run_backfill <- function(df, params,
5858
}
5959

6060
msg_ts("Splitting data into geo groups")
61-
group_dfs <- group_split(df, .data$geo_value)
61+
group_dfs <- group_split(df, geo_value)
6262

6363
# Build model for each location
6464
for (subdf in group_dfs) {
@@ -112,15 +112,15 @@ 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(.data$lag < params$ref_lag)
115+
combined_df <- combined_df %>% filter(lag < params$ref_lag)
116116

117117
geo_train_data <- combined_df %>%
118-
filter(.data$issue_date < params$training_end_date) %>%
119-
filter(.data$target_date <= params$training_end_date) %>%
120-
filter(.data$target_date > params$training_start_date) %>%
118+
filter(issue_date < params$training_end_date) %>%
119+
filter(target_date <= params$training_end_date) %>%
120+
filter(target_date > params$training_start_date) %>%
121121
drop_na()
122122
geo_test_data <- combined_df %>%
123-
filter(.data$issue_date %in% params$test_dates) %>%
123+
filter(issue_date %in% params$test_dates) %>%
124124
drop_na()
125125

126126
if (nrow(geo_test_data) == 0) {
@@ -135,8 +135,8 @@ run_backfill <- function(df, params,
135135
if (value_type == "fraction") {
136136
# Use beta prior approach to adjust fractions
137137
geo_prior_test_data = combined_df %>%
138-
filter(.data$issue_date > min(params$test_dates) - 7) %>%
139-
filter(.data$issue_date <= max(params$test_dates))
138+
filter(issue_date > min(params$test_dates) - 7) %>%
139+
filter(issue_date <= max(params$test_dates))
140140
updated_data <- frac_adj(geo_train_data, geo_test_data, geo_prior_test_data,
141141
indicator = indicator, signal = signal,
142142
geo_level = geo_level, signal_suffix = signal_suffix,
@@ -236,9 +236,8 @@ run_backfill <- function(df, params,
236236
#' @template lag_col-template
237237
#' @template issued_col-template
238238
#'
239-
#' @importFrom dplyr bind_rows mutate %>%
239+
#' @importFrom dplyr bind_rows %>%
240240
#' @importFrom parallel detectCores
241-
#' @importFrom rlang .data :=
242241
#' @importFrom stringr str_interp
243242
#'
244243
#' @export
@@ -251,7 +250,7 @@ main <- function(params,
251250

252251
indicators_subset <- INDICATORS_AND_SIGNALS
253252
if (params$indicators != "all") {
254-
indicators_subset <- filter(indicators_subset, .data$indicator == params$indicators)
253+
indicators_subset <- filter(indicators_subset, indicator == params$indicators)
255254
}
256255
if (nrow(indicators_subset) == 0) {
257256
stop("no indicators to process")
@@ -307,14 +306,12 @@ main <- function(params,
307306
input_data <- lapply(
308307
files_list,
309308
function(file) {
310-
read_data(file) %>%
311-
fips_to_geovalue() %>%
312-
mutate(
313-
# Use `glue` syntax to construct a new field by variable,
314-
# from https://stackoverflow.com/a/26003971/14401472
315-
"{refd_col}" := as.Date(.data[[refd_col]], "%Y-%m-%d"),
316-
"{issued_col}" := as.Date(.data[[issued_col]], "%Y-%m-%d")
317-
)
309+
df <- read_data(file) %>%
310+
fips_to_geovalue()
311+
df[[refd_col]] <- as.Date(df[[refd_col]], "%Y-%m-%d")
312+
df[[issued_col]] <- as.Date(df[[issued_col]], "%Y-%m-%d")
313+
314+
return(df)
318315
}
319316
) %>%
320317
bind_rows()

backfill_corrections/delphiBackfillCorrection/R/model.R

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
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 rlang .data .env
9-
#'
108
#' @export
119
data_filteration <- function(test_lag, geo_train_data, geo_test_data, lag_pad) {
1210
if (test_lag <= 14){
@@ -23,11 +21,11 @@ data_filteration <- function(test_lag, geo_train_data, geo_test_data, lag_pad) {
2321
test_lag_pad2=9
2422
}
2523
train_data = geo_train_data %>%
26-
filter(.data$lag >= .env$test_lag - .env$test_lag_pad ) %>%
27-
filter(.data$lag <= .env$test_lag + .env$test_lag_pad )
24+
filter(lag >= test_lag - test_lag_pad ) %>%
25+
filter(lag <= test_lag + test_lag_pad )
2826
test_data = geo_test_data %>%
29-
filter(.data$lag >= .env$test_lag - .env$test_lag_pad1 ) %>%
30-
filter(.data$lag <= .env$test_lag + .env$test_lag_pad2)
27+
filter(lag >= test_lag - test_lag_pad1 ) %>%
28+
filter(lag <= test_lag + test_lag_pad2)
3129

3230
return (list(train_data, test_data))
3331
}

backfill_corrections/delphiBackfillCorrection/R/utils.R

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,16 @@ training_days_check <- function(issue_date, training_days) {
203203
#' Subset list of counties to those included in the 200 most populous in the US
204204
#'
205205
#' @importFrom dplyr select %>% arrange desc pull
206-
#' @importFrom rlang .data
207206
#' @importFrom utils head
208207
#' @import covidcast
209208
get_populous_counties <- function() {
210209
return(
211210
covidcast::county_census %>%
212-
dplyr::select(pop = .data$POPESTIMATE2019, fips = .data$FIPS) %>%
211+
dplyr::select(pop = POPESTIMATE2019, fips = FIPS) %>%
213212
# Drop megacounties (states)
214-
filter(!endsWith(.data$fips, "000")) %>%
215-
arrange(desc(.data$pop)) %>%
216-
pull(.data$fips) %>%
213+
filter(!endsWith(fips, "000")) %>%
214+
arrange(desc(pop)) %>%
215+
pull(fips) %>%
217216
head(n=200)
218217
)
219218
}

backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/test-model.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
context("Testing the helper functions for modeling")
22

3+
library(dplyr)
4+
35
# Constants
46
indicator <- "chng"
57
signal <- "outpatient"

0 commit comments

Comments
 (0)