When add_n() and add_p() are run after tbl_survfit(), the formula
and data of the original survival::survfit() call are evaluated again
to count observations or compare the curves. They are evaluated where
tbl_survfit() was called, so the functions fail when the data frame is
not available there, for example when the model was fit inside a function
or a loop. Two ways to avoid the error:
Let
tbl_survfit()build the models by passing it the data frame. This works when the models are stratified. Instead ofsurvfit(Surv(ttdeath, death) ~ trt, trial) |> tbl_survfit(times = c(12, 24))use
trial |> tbl_survfit(y = Surv(ttdeath, death), include = trt, times = c(12, 24))Fit the model with the data frame inlined in the call, so that the
survfitobject carries its own data:fit <- do.call(survfit, list(Surv(ttdeath, death) ~ 1, data = trial)) tbl_survfit(fit, times = c(12, 24))
See also
Other survival tables:
add_n_survfit,
add_nevent_survfit,
add_p_survfit,
tbl_survfit()