Skip to contents

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:

  1. Let tbl_survfit() build the models by passing it the data frame. This works when the models are stratified. Instead of

    survfit(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))

  2. Fit the model with the data frame inlined in the call, so that the survfit object carries its own data:

    fit <- do.call(survfit, list(Surv(ttdeath, death) ~ 1, data = trial))
    tbl_survfit(fit, times = c(12, 24))

See also