Debian Codegraph @

R/survival-coxph.R

raw
1 #' @templateVar class coxph
2 #' @template title_desc_tidy
3 #'
4 #' @param x A `coxph` object returned from [survival::coxph()].
5 #' @template param_confint
6 #' @template param_exponentiate
7 #' @param ... For `tidy()`, additional arguments passed to `summary(x, ...)`.
8 #' Otherwise ignored.
9 #'
10 #' @evalRd return_tidy(
11 #' "estimate",
12 #' "std.error",
13 #' "statistic",
14 #' "p.value"
15 #' )
16 #'
17 #' @examplesIf rlang::is_installed(c("survival", "ggplot2"))
18 #'
19 #' # load libraries for models and data
20 #' library(survival)
21 #'
22 #' # fit model
23 #' cfit <- coxph(Surv(time, status) ~ age + sex, lung)
24 #'
25 #' # summarize model fit with tidiers
26 #' tidy(cfit)
27 #' tidy(cfit, exponentiate = TRUE)
28 #'
29 #' lp <- augment(cfit, lung)
30 #' risks <- augment(cfit, lung, type.predict = "risk")
31 #' expected <- augment(cfit, lung, type.predict = "expected")
32 #'
33 #' glance(cfit)
34 #'
35 #' # also works on clogit models
36 #' resp <- levels(logan$occupation)
37 #' n <- nrow(logan)
38 #' indx <- rep(1:n, length(resp))
39 #' logan2 <- data.frame(
40 #' logan[indx, ],
41 #' id = indx,
42 #' tocc = factor(rep(resp, each = n))
43 #' )
44 #'
45 #' logan2$case <- (logan2$occupation == logan2$tocc)
46 #'
47 #' cl <- clogit(case ~ tocc + tocc:education + strata(id), logan2)
48 #'
49 #' tidy(cl)
50 #' glance(cl)
51 #'
52 #' library(ggplot2)
53 #'
54 #' ggplot(lp, aes(age, .fitted, color = sex)) +
55 #' geom_point()
56 #'
57 #' ggplot(risks, aes(age, .fitted, color = sex)) +
58 #' geom_point()
59 #'
60 #' ggplot(expected, aes(time, .fitted, color = sex)) +
61 #' geom_point()
62 #'
63 #' @aliases coxph_tidiers
64 #' @export
65 #' @seealso [tidy()], [survival::coxph()]
66 #' @family coxph tidiers
67 #' @family survival tidiers
68 tidy.coxph <- function(
69 x,
70 exponentiate = FALSE,
71 conf.int = FALSE,
72 conf.level = .95,
73 ...
74 ) {
75 s <- summary(x, ...)
76 co <- stats::coef(s)
77
78 if (!is.null(x$frail)) {
79 nn <- c("estimate", "std.error", "statistic", "p.value")
80 } else if (isTRUE(s$used.robust)) {
81 nn <- c("estimate", "std.error", "robust.se", "statistic", "p.value")
82 } else {
83 nn <- c("estimate", "std.error", "statistic", "p.value")
84 }
85
86 if (is.null(x$frail) && is.null(x$penalty)) {
87 ret <- as_tidy_tibble(co[, -2, drop = FALSE], new_names = nn)
88 } else {
89 ret <- as_tidy_tibble(co[, -c(3, 5), drop = FALSE], new_names = nn)
90 }
91
92 if (conf.int) {
93 ci <- broom_confint_terms(x, level = conf.level)
94 ret <- dplyr::left_join(ret, ci, by = "term")
95 }
96
97 if (exponentiate) {
98 ret <- exponentiate(ret)
99 }
100
101 as_tibble(ret)
102 }
103
104
105 #' @templateVar class coxph
106 #' @template title_desc_augment
107 #'
108 #' @inherit tidy.coxph params examples
109 #' @template param_data
110 #' @template param_newdata
111 #' @template param_type_residuals
112 #' @template param_type_predict
113 #' @template param_unused_dots
114 #'
115 #' @template augment_NAs
116 #'
117 #' @evalRd return_augment(".se.fit")
118 #'
119 #' @export
120 #' @seealso [augment()], [survival::coxph()]
121 #' @family coxph tidiers
122 #' @family survival tidiers
123 augment.coxph <- function(
124 x,
125 data = model.frame(x),
126 newdata = NULL,
127 type.predict = "lp",
128 type.residuals = "martingale",
129 ...
130 ) {
131 augment_columns(
132 x,
133 data,
134 newdata,
135 type.predict = type.predict,
136 type.residuals = type.residuals
137 )
138 }
139
140 #' @templateVar class coxph
141 #' @template title_desc_glance
142 #'
143 #' @inherit tidy.coxph params examples
144 #'
145 #' @evalRd return_glance(
146 #' "n",
147 #' "nevent",
148 #' "logLik",
149 #' "AIC",
150 #' "BIC",
151 #' "nobs",
152 #' .post = "See survival::coxph.object for additional column descriptions.")
153 #'
154 #' @export
155 #' @seealso [glance()], [survival::coxph()]
156 #' @family coxph tidiers
157 #' @family survival tidiers
158 glance.coxph <- function(x, ...) {
159 s <- summary(x)
160 # including all the test statistics and p-values as separate
161 # columns. Admittedly not perfect but does capture most use cases.
162 as_glance_tibble(
163 n = s$n,
164 nevent = s$nevent,
165 statistic.log = s$logtest[1],
166 p.value.log = s$logtest[3],
167 statistic.sc = s$sctest[1],
168 p.value.sc = s$sctest[3],
169 statistic.wald = s$waldtest[1],
170 p.value.wald = s$waldtest[3],
171 statistic.robust = s$robscore[1],
172 p.value.robust = s$robscore[3],
173 r.squared = s$rsq[1],
174 r.squared.max = s$rsq[2],
175 concordance = s$concordance[1],
176 std.error.concordance = s$concordance[2],
177 logLik = as.numeric(stats::logLik(x)),
178 AIC = stats::AIC(x),
179 BIC = stats::BIC(x),
180 nobs = stats::nobs(x),
181 na_types = "iirrrrrrrrrrrrrrri"
182 )
183 }