Skip to content

Commit

Permalink
Input checking, minor rearrangement, and docs for coh_effectiveness()
Browse files Browse the repository at this point in the history
  • Loading branch information
pratikunterwegs committed Jul 7, 2023
1 parent 8fd7455 commit 08a43d7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
52 changes: 39 additions & 13 deletions R/coh_effectiveness.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,34 @@
#' @return summary: hazards ratio (CI95%), vaccine effectiveness (CI95%),
#' and Schoenfeld test
#' @examples
#' \dontrun{
#' # load example data from package
#' data("cohortdata")
#'
#' # add immunization dates
#' cohortdata$immunization_death <- get_immunization_date(
#' date = cohortdata,
#' data = cohortdata,
#' outcome_date_col = "death_date",
#' outcome_delay = 0,
#' immunisation_delay = 14,
#' immunization_delay = 14,
#' vacc_date_col = c("vaccine_date_1", "vaccine_date_2"),
#' end_cohort = "2021-12-31",
#' take_first = FALSE
#' )
#'
#' # add vaccine status
#' cohortdata$vaccine_status <- set_status(
#' data = cohortdata,
#' col_names = c("immunization_death"),
#' status = c("v", "u")
#' )
#'
#' # add death status
#' cohortdata$death_status <- set_status(
#' data = cohortdata,
#' col_names = c("death_date")
#' )
#'
#' # add time to death
#' cohortdata$time_to_death <- get_time_to_event(
#' data = cohortdata,
#' outcome_date_col = "death_date",
Expand All @@ -50,30 +55,51 @@
#' FALSE
#' )
#'
#' # estimate vaccine effectiveness
#' coh_eff_noconf(
#' cohortdata,
#' "death_status",
#' "time_to_death",
#' "vaccine_status"
#' )
#' }
#' @export
coh_eff_noconf <- function(data,
outcome_status_col,
time_to_event_col,
status_vacc_col,
p_thr = 0.05) {
cx <- survival::coxph(survival::Surv(
data[[time_to_event_col]],
data[[outcome_status_col]]

# input checking
checkmate::assert_data_frame(
data,
min.rows = 1L
)
checkmate::assert_names(
names(data),
must.include = c(outcome_status_col, time_to_event_col, status_vacc_col)
)
checkmate::assert_number(p_thr, lower = 0.0, upper = 1.0)

# cox regression
cx <- survival::coxph(
survival::Surv(
data[[time_to_event_col]],
data[[outcome_status_col]]
) ~ data[[status_vacc_col]]
)
~ data[[status_vacc_col]])

# Test the Proportional Hazards Assumption
test <- survival::cox.zph(cx)
hr <- c(round(exp(stats::coef(cx)), 4)[1])
ci025 <- c(round(exp(stats::confint(cx)), 4)[1])
ci975 <- c(round(exp(stats::confint(cx)), 4)[2])
p <- test$table[5]
p_value <- c(format(p, digits = 3))
hr <- round(exp(stats::coef(cx)), digits = 4)

# extract first and second element as limits
ci025 <- round(exp(stats::confint(cx)), 4)[1]
ci975 <- round(exp(stats::confint(cx)), 4)[2]

# extract from matrix by name
p <- test$table["GLOBAL", "p"]

p_value <- format(p, digits = 3)
if (p < p_thr) {
ph <- "reject"
} else {
Expand Down
13 changes: 9 additions & 4 deletions man/coh_eff_noconf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 08a43d7

Please sign in to comment.