| 1 |
#' @templateVar class pyears |
| 2 |
#' @template title_desc_tidy |
| 3 |
#' |
| 4 |
#' @param x A `pyears` object returned from [survival::pyears()]. |
| 5 |
#' @template param_unused_dots |
| 6 |
#' |
| 7 |
#' @evalRd return_tidy( |
| 8 |
#' "pyears", |
| 9 |
#' n = "number of subjects contributing time", |
| 10 |
#' event = "observed number of events", |
| 11 |
#' "expected" |
| 12 |
#' ) |
| 13 |
#' |
| 14 |
#' @details `expected` is only present in the output when if a `ratetable` |
| 15 |
#' term is present. |
| 16 |
#' |
| 17 |
#' If the `data.frame = TRUE` argument is supplied to `pyears`, |
| 18 |
#' this is simply the contents of `x$data`. |
| 19 |
#' |
| 20 |
#' @examplesIf rlang::is_installed("survival") |
| 21 |
#' |
| 22 |
#' # load libraries for models and data |
| 23 |
#' library(survival) |
| 24 |
#' |
| 25 |
#' # generate and format data |
| 26 |
#' temp.yr <- tcut(mgus$dxyr, 55:92, labels = as.character(55:91)) |
| 27 |
#' temp.age <- tcut(mgus$age, 34:101, labels = as.character(34:100)) |
| 28 |
#' ptime <- ifelse(is.na(mgus$pctime), mgus$futime, mgus$pctime) |
| 29 |
#' pstat <- ifelse(is.na(mgus$pctime), 0, 1) |
| 30 |
#' pfit <- pyears(Surv(ptime / 365.25, pstat) ~ temp.yr + temp.age + sex, mgus, |
| 31 |
#' data.frame = TRUE |
| 32 |
#' ) |
| 33 |
#' |
| 34 |
#' # summarize model fit with tidiers |
| 35 |
#' tidy(pfit) |
| 36 |
#' glance(pfit) |
| 37 |
#' |
| 38 |
#' # if data.frame argument is not given, different information is present in |
| 39 |
#' # output |
| 40 |
#' pfit2 <- pyears(Surv(ptime / 365.25, pstat) ~ temp.yr + temp.age + sex, mgus) |
| 41 |
#' |
| 42 |
#' tidy(pfit2) |
| 43 |
#' glance(pfit2) |
| 44 |
#' |
| 45 |
#' @aliases pyears_tidiers |
| 46 |
#' @export |
| 47 |
#' @seealso [tidy()], [survival::pyears()] |
| 48 |
#' @family pyears tidiers |
| 49 |
#' @family survival tidiers |
| 50 |
#' |
| 51 |
tidy.pyears <- function(x, ...) { |
| 52 |
if (is.null(x$data)) { |
| 53 |
ret <- purrr::compact(unclass(x)[c("pyears", "n", "event", "expected")]) |
| 54 |
} else { |
| 55 |
ret <- x$data |
| 56 |
} |
| 57 |
as_tibble(as.data.frame(ret)) # allow vector recycling! |
| 58 |
} |
| 59 |
|
| 60 |
#' @templateVar class pyears |
| 61 |
#' @template title_desc_glance |
| 62 |
#' |
| 63 |
#' @inherit tidy.pyears params examples |
| 64 |
#' |
| 65 |
#' @evalRd return_glance( |
| 66 |
#' total = "total number of person-years tabulated", |
| 67 |
#' offtable = "total number of person-years off table", |
| 68 |
#' "nobs" |
| 69 |
#' ) |
| 70 |
#' |
| 71 |
#' @export |
| 72 |
#' @seealso [glance()], [survival::pyears()] |
| 73 |
#' @family pyears tidiers |
| 74 |
#' @family survival tidiers |
| 75 |
glance.pyears <- function(x, ...) { |
| 76 |
if (is.null(x$data)) { |
| 77 |
tibble( |
| 78 |
total = sum(x$pyears), |
| 79 |
offtable = x$offtable, |
| 80 |
nobs = stats::nobs(x) |
| 81 |
) |
| 82 |
} else { |
| 83 |
tibble( |
| 84 |
total = sum(x$data$pyears), |
| 85 |
offtable = x$offtable, |
| 86 |
nobs = stats::nobs(x) |
| 87 |
) |
| 88 |
} |
| 89 |
} |