Debian Codegraph @

R/cmprsk.R

raw
1 #' @templateVar class cmprsk
2 #' @template title_desc_tidy
3 #'
4 #' @param x A `crr` object returned from [cmprsk::crr()].
5 #' @template param_confint
6 #' @template param_exponentiate
7 #' @template param_unused_dots
8 #'
9 #' @evalRd return_tidy(
10 #' "estimate",
11 #' "std.error",
12 #' "statistic",
13 #' "p.value",
14 #' "conf.low",
15 #' "conf.high"
16 #' )
17 #'
18 #' @examplesIf rlang::is_installed("cmprsk")
19 #'
20 #' library(cmprsk)
21 #'
22 #' # time to loco-regional failure (lrf)
23 #' lrf_time <- rexp(100)
24 #' lrf_event <- sample(0:2, 100, replace = TRUE)
25 #' trt <- sample(0:1, 100, replace = TRUE)
26 #' strt <- sample(1:2, 100, replace = TRUE)
27 #'
28 #' # fit model
29 #' x <- crr(lrf_time, lrf_event, cbind(trt, strt))
30 #'
31 #' # summarize model fit with tidiers
32 #' tidy(x, conf.int = TRUE)
33 #' glance(x)
34 #'
35 #' @aliases cmprsk_tidiers
36 #' @export
37 #' @seealso [tidy()], [cmprsk::crr()]
38 #' @family cmprsk tidiers
39 tidy.crr <- function(
40 x,
41 exponentiate = FALSE,
42 conf.int = FALSE,
43 conf.level = .95,
44 ...
45 ) {
46 s <- summary(x, conf.int = conf.level)
47 ret <- as_tidy_tibble(
48 s$coef,
49 new_names = c(
50 "estimate",
51 "estimate_exp",
52 "std.error",
53 "statistic",
54 "p.value"
55 )
56 )[, -3]
57
58 if (conf.int) {
59 ci <- as_tidy_tibble(
60 log(s$conf.int),
61 new_names = c("estimate_exp", "estimate_neg_exp", "conf.low", "conf.high")
62 )[, -c(2, 3)]
63 ret <- dplyr::left_join(ret, ci, by = "term")
64 }
65
66 if (exponentiate) {
67 ret <- exponentiate(ret)
68 }
69
70 ret
71 }
72
73
74 #' @templateVar class crr
75 #' @template title_desc_glance
76 #'
77 #' @inherit tidy.crr params examples
78 #'
79 #' @evalRd return_glance(
80 #' "converged",
81 #' "logLik",
82 #' "nobs",
83 #' "df",
84 #' "statistic")
85 #'
86 #' @export
87 #' @seealso [glance()], [cmprsk::crr()]
88 #' @family crr tidiers
89 #' @family cmprsk tidiers
90 glance.crr <- function(x, ...) {
91 s <- summary(x)
92 as_glance_tibble(
93 converged = x$converged,
94 logLik = x$loglik,
95 nobs = x$n,
96 df = s$logtest["df"],
97 statistic = s$logtest["test"],
98 na_types = "lriir"
99 )
100 }