Debian Codegraph @

R/survival-survreg.R

raw
1 #' @templateVar class survreg
2 #' @template title_desc_tidy
3 #'
4 #' @param x An `survreg` object returned from [survival::survreg()].
5 #' @template param_confint
6 #' @template param_unused_dots
7 #'
8 #' @evalRd return_tidy(regression = TRUE)
9 #'
10 #' @examplesIf rlang::is_installed(c("survival", "ggplot2"))
11 #'
12 #' # load libraries for models and data
13 #' library(survival)
14 #'
15 #' # fit model
16 #' sr <- survreg(
17 #' Surv(futime, fustat) ~ ecog.ps + rx,
18 #' ovarian,
19 #' dist = "exponential"
20 #' )
21 #'
22 #' # summarize model fit with tidiers + visualization
23 #' tidy(sr)
24 #' augment(sr, ovarian)
25 #' glance(sr)
26 #'
27 #' # coefficient plot
28 #' td <- tidy(sr, conf.int = TRUE)
29 #'
30 #' library(ggplot2)
31 #'
32 #' ggplot(td, aes(estimate, term)) +
33 #' geom_point() +
34 #' geom_errorbarh(aes(xmin = conf.low, xmax = conf.high), height = 0) +
35 #' geom_vline(xintercept = 0)
36 #'
37 #' @aliases survreg_tidiers
38 #' @export
39 #' @seealso [tidy()], [survival::survreg()]
40 #' @family survreg tidiers
41 #' @family survival tidiers
42 #'
43 tidy.survreg <- function(x, conf.level = .95, conf.int = FALSE, ...) {
44 check_ellipses("exponentiate", "tidy", "survreg", ...)
45
46 s <- summary(x)$table
47 # If the user requested robust SE in the survreg call, don't return naive SE
48 # (The column is not present if robust=FALSE)
49 s <- s[, colnames(s) != "(Naive SE)", drop = FALSE]
50 nn <- c("estimate", "std.error", "statistic", "p.value")
51 ret <- as_tidy_tibble(s, new_names = nn)
52
53 if (conf.int) {
54 ci <- broom_confint_terms(x, level = conf.level)
55 ret <- dplyr::left_join(ret, ci, by = "term")
56 }
57
58 ret
59 }
60
61
62 #' @templateVar class survreg
63 #' @template title_desc_augment
64 #'
65 #' @inherit tidy.survreg params examples
66 #' @template param_data
67 #' @template param_newdata
68 #' @template param_type_residuals
69 #' @template param_type_predict
70 #' @template param_unused_dots
71 #'
72 #' @evalRd return_augment(".se.fit")
73 #'
74 #' @export
75 #' @seealso [augment()], [survival::survreg()]
76 #' @family survreg tidiers
77 #' @family survival tidiers
78 augment.survreg <- function(
79 x,
80 data = model.frame(x),
81 newdata = NULL,
82 type.predict = "response",
83 type.residuals = "response",
84 ...
85 ) {
86 augment_columns(
87 x,
88 data,
89 newdata,
90 type.predict = type.predict,
91 type.residuals = type.residuals
92 )
93 }
94
95
96 #' @templateVar class survreg
97 #' @template title_desc_glance
98 #'
99 #' @inherit tidy.survreg params examples
100 #'
101 #' @evalRd return_glance(
102 #' "iter",
103 #' "df",
104 #' "p.value",
105 #' "logLik",
106 #' "AIC",
107 #' "BIC",
108 #' "df.residual",
109 #' statistic = "Chi-squared statistic.",
110 #' "nobs"
111 #' )
112 #'
113 #' @export
114 #' @seealso [glance()], [survival::survreg()]
115 #' @family survreg tidiers
116 #' @family survival tidiers
117 glance.survreg <- function(x, ...) {
118 as_glance_tibble(
119 iter = x$iter,
120 df = sum(x$df),
121 statistic = 2 * diff(x$loglik),
122 logLik = as.numeric(stats::logLik(x)),
123 AIC = stats::AIC(x),
124 BIC = stats::BIC(x),
125 df.residual = stats::df.residual(x),
126 nobs = stats::nobs(x),
127 p.value = 1 - stats::pchisq(2 * diff(x$loglik), sum(x$df) - x$idf),
128 na_types = "iirrrriir"
129 )
130 }