Skip to content

Instantly share code, notes, and snippets.

@solmos
Created May 9, 2022 14:27
Show Gist options
  • Save solmos/7bcf24440da6283b0c1f8669e1a860a7 to your computer and use it in GitHub Desktop.
Save solmos/7bcf24440da6283b0c1f8669e1a860a7 to your computer and use it in GitHub Desktop.
parseFormula <- function(event_name, vars) {
m_formula <- paste0(
## Otavio/Anna change this since you are not using age as time scale!
"Surv(age_start, age_end, ", event_name, ") ~ ",
paste(vars, collapse = " + ")
)
as.formula(m_formula)
}
fitCox <- function(data, event_name, expo, vars) {
stopifnot(length(event_name) == 1 & length(expo) == 1)
all_vars <- c(expo, vars)
m_formula <- parseFormula(event_name, all_vars)
m <- coxph(m_formula, data = data)
m_aic <- AIC(m)
m_bic <- BIC(m)
m_coefs <- broom::tidy(m, exponentiate = TRUE, conf.int = TRUE) %>%
mutate(
outcome = event_name,
expo = paste(expo, collapse = ", "),
covariates = paste(vars, collapse = ", "),
aic = m_aic,
bic = m_bic
)
gc()
m_coefs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment