Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc code simplifications #62

Merged
merged 5 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions R/coh_data_wrangling.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,15 @@ set_event_status <- function(data_set,
col_names = outcome_date_col,
status = c(1, 0)
)

checkmate::assert_character(censoring_date_col,
any.missing = FALSE, min.len = 1, null.ok = TRUE
)
checkmate::assert_names(
names(data_set), must.include = censoring_date_col
)

if (!is.null(censoring_date_col)) {
checkmate::assert_character(censoring_date_col,
any.missing = FALSE, min.len = 1
)
checkmate::assert_names(
names(data_set), must.include = censoring_date_col
)
data_set$outcome_status <- ifelse(
(!is.na(data_set[[censoring_date_col]])) &
(!is.na(data_set[[outcome_date_col]])) &
Expand Down Expand Up @@ -181,15 +183,15 @@ get_time_to_event <- function(data_set,
)

#Checks of censoring_date_col if provided
checkmate::assert_string(censoring_date_col, null.ok = TRUE)
checkmate::assert_names(
colnames(data_set),
must.include = censoring_date_col
)
if (!is.null(censoring_date_col)) {
checkmate::assert_names(
colnames(data_set),
must.include = censoring_date_col
)
checkmate::assert_date(
data_set[[censoring_date_col]]
)
checkmate::assert_string(censoring_date_col)
}

# check immnunization date col if asked
Expand Down
2 changes: 1 addition & 1 deletion R/coh_eff_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extract_surv_model <- function(model, start_cohort, end_cohort) {
tte <- seq(0, as.numeric(days) - 1, by = 1)
res <- summary(model, times = tte, scale = 1)
cols <- lapply(c(2:6, 8:16), function(x) res[x])
tbl <- do.call(data.frame, cols)
tbl <- data.frame(cols)
tbl$date <- tbl$time + start_cohort
return(tbl)
}
Expand Down
29 changes: 14 additions & 15 deletions R/coh_immunization.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,29 +119,28 @@ make_immunization <- function(data_set,
len = 1L
)

checkmate::assert_string(censoring_date_col, null.ok = TRUE)
checkmate::assert_names(
colnames(data_set),
must.include = censoring_date_col
)
# check censoring_date_col if provided
if (!is.null(censoring_date_col)) {
checkmate::assert_names(
colnames(data_set),
must.include = censoring_date_col
)
checkmate::assert_date(
data_set[[censoring_date_col]]
)
checkmate::assert_string(censoring_date_col)
}

# check vacc_name_col if provided
if (!is.null(vacc_name_col)) {
checkmate::assert_names(
names(data_set),
must.include = c(vacc_name_col)
)
checkmate::assert_character(
vacc_name_col,
min.len = length(vacc_date_col)
)
}
checkmate::assert_character(
vacc_name_col,
min.len = length(vacc_date_col),
null.ok = TRUE
)
checkmate::assert_names(
names(data_set),
must.include = c(vacc_name_col)
)

# warn on year of cohort end date date
max_year <- 2100 # a plausible maximum year
Expand Down
47 changes: 22 additions & 25 deletions R/coh_match.R
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ match_cohort <- function(data_set,
)

#Checks of censoring_date_col if provided
checkmate::assert_string(censoring_date_col, null.ok = TRUE)
checkmate::assert_names(
colnames(data_set),
must.include = censoring_date_col
)
if (!is.null(censoring_date_col)) {
checkmate::assert_names(
colnames(data_set),
must.include = censoring_date_col
)
checkmate::assert_date(
data_set[[censoring_date_col]]
)
checkmate::assert_string(censoring_date_col)
}

# `exact` and `nearest` cannot be NULL. At least one must be provided
Expand All @@ -153,26 +153,23 @@ match_cohort <- function(data_set,
)

# checks for `nearest`
if (!is.null(nearest)) {
checkmate::assert_numeric(
nearest,
any.missing = FALSE, min.len = 1, names = "named"
)
checkmate::assert_names(
names(data_set),
must.include = names(nearest)
)
}
# checks for `exact`. Not else, both can be non-NULL
if (!is.null(exact)) {
checkmate::assert_character(exact,
any.missing = FALSE, min.len = 1
)
checkmate::assert_names(
names(data_set),
must.include = exact
)
}
checkmate::assert_numeric(
nearest,
any.missing = FALSE, min.len = 1, names = "named", null.ok = TRUE
)
checkmate::assert_names(
names(data_set),
must.include = names(nearest)
)

# checks for `exact`
checkmate::assert_character(exact,
any.missing = FALSE, min.len = 1, null.ok = TRUE
)
checkmate::assert_names(
names(data_set),
must.include = exact
)

# check date types
checkmate::assert_date(
Expand Down
9 changes: 1 addition & 8 deletions R/coh_match_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@ match_cohort_ <- function(data_set,

#Formula
variables <- c(exact, names(nearest))
formula <- paste0(vacc_status_col, " ~ ")
for (v in seq_along(variables)) {
if (v == 1) {
formula <- paste0(formula, variables[v])
} else {
formula <- paste0(formula, " + ", variables[v])
}
}
formula <- paste(vacc_status_col, "~", paste(variables, collapse = " + "))
formula_eval <- eval(parse(text = formula))

#Matching
Expand Down
File renamed without changes.
Loading