I would like to run a loop for Cox models that adjust for different subsets of covariates. The desired output should include number of events, person-years, hazards ratios, and 95%CI. If I call coxph directly, tidy_add_n works perfectly, but when I use either reformulate or as.formula in the loop, the function does not create the two columns n_event and exposure but a new column named n which contains only missing value. Any ideas or suggestions to fix this issue or to work around would be very much appreciated. Thank you!
Below are the reproducible codes.
test1 <- list(time = c(4,3,1,1,2,2,3),
status = c(1,1,1,0,1,1,0),
x1 = as.factor(c(0,2,1,1,1,0,0)),
x2 = as.factor(c(0,0,0,0,1,1,1)))
#the function works if I call coxph directly
coxph(Surv(time, status) ~ x1 + x2, test1) %>%
tidy_and_attach()%>%
tidy_add_reference_rows() %>%
tidy_add_estimate_to_reference_rows() %>%
tidy_add_header_rows() %>%
tidy_add_n()
#tidy_add_n does not work when I use reformulate or as.formula.
surv <- "Surv(time, status)"
c("x1","x2")%>%
reformulate(response = surv)%>%
coxph(data = test1)%>%
tidy_and_attach()%>%
tidy_add_reference_rows() %>%
tidy_add_estimate_to_reference_rows() %>%
tidy_add_header_rows() %>%
tidy_add_n()