Debian Codegraph @

R/joinerml.R

raw
1 #' @templateVar class mjoint
2 #' @template title_desc_tidy
3 #'
4 #' @param x An `mjoint` object returned from [joineRML::mjoint()].
5 #' @template param_confint
6 #' @param component Character specifying whether to tidy the survival or
7 #' the longitudinal component of the model. Must be either `"survival"` or
8 #' `"longitudinal"`. Defaults to `"survival"`.
9 #' @param boot_se Optionally a `bootSE` object from [joineRML::bootSE()]. If
10 #' specified, calculates confidence intervals via the bootstrap. Defaults to
11 #' `NULL`, in which case standard errors are calculated from the
12 #' empirical information matrix.
13 #' @template param_unused_dots
14 #'
15 #' @evalRd return_tidy(regression = TRUE)
16 #'
17 #' @examplesIf rlang::is_installed("joineRML")
18 #'
19 #' # broom only skips running these examples because the example models take a
20 #' # while to generate—they should run just fine, though!
21 #' \dontrun{
22 #'
23 #'
24 #' # load libraries for models and data
25 #' library(joineRML)
26 #'
27 #' # fit a joint model with bivariate longitudinal outcomes
28 #' data(heart.valve)
29 #'
30 #' hvd <- heart.valve[!is.na(heart.valve$log.grad) &
31 #' !is.na(heart.valve$log.lvmi) &
32 #' heart.valve$num <= 50, ]
33 #'
34 #' fit <- mjoint(
35 #' formLongFixed = list(
36 #' "grad" = log.grad ~ time + sex + hs,
37 #' "lvmi" = log.lvmi ~ time + sex
38 #' ),
39 #' formLongRandom = list(
40 #' "grad" = ~ 1 | num,
41 #' "lvmi" = ~ time | num
42 #' ),
43 #' formSurv = Surv(fuyrs, status) ~ age,
44 #' data = hvd,
45 #' inits = list("gamma" = c(0.11, 1.51, 0.80)),
46 #' timeVar = "time"
47 #' )
48 #'
49 #' # extract the survival fixed effects
50 #' tidy(fit)
51 #'
52 #' # extract the longitudinal fixed effects
53 #' tidy(fit, component = "longitudinal")
54 #'
55 #' # extract the survival fixed effects with confidence intervals
56 #' tidy(fit, ci = TRUE)
57 #'
58 #' # extract the survival fixed effects with confidence intervals based
59 #' # on bootstrapped standard errors
60 #' bSE <- bootSE(fit, nboot = 5, safe.boot = TRUE)
61 #' tidy(fit, boot_se = bSE, ci = TRUE)
62 #'
63 #' # augment original data with fitted longitudinal values and residuals
64 #' hvd2 <- augment(fit)
65 #'
66 #' # extract model statistics
67 #' glance(fit)
68 #' }
69 #'
70 #' @export
71 #' @aliases mjoint_tidiers joinerml_tidiers
72 #' @family mjoint tidiers
73 #' @seealso [tidy()], [joineRML::mjoint()], [joineRML::bootSE()]
74 #'
75 tidy.mjoint <- function(
76 x,
77 component = "survival",
78 conf.int = FALSE,
79 conf.level = 0.95,
80 boot_se = NULL,
81 ...
82 ) {
83 check_ellipses("exponentiate", "tidy", "mjoint", ...)
84
85 component <- rlang::arg_match(component, c("survival", "longitudinal"))
86 if (!is.null(boot_se)) {
87 if (!inherits(x = boot_se, "bootSE")) {
88 cli::cli_abort("{.arg boot_se} must be a {.cls bootSE} object.")
89 }
90 }
91
92 if (is.null(boot_se)) {
93 smr <- summary(x)
94 } else {
95 smr <- summary(x, bootSE = boot_se)
96 }
97
98 if (component == "survival") {
99 out <- data.frame(smr$coefs.surv)
100 } else {
101 out <- data.frame(smr$coefs.long)
102 }
103
104 names(out) <- c("estimate", "std.error", "statistic", "p.value")
105
106 out$term <- rownames(out)
107 rownames(out) <- NULL
108 out <- out[, c(5, 1:4)]
109
110 if (conf.int) {
111 cv <- qnorm(1 - (1 - conf.level) / 2)
112 out$conf.low <- out$estimate - cv * out$std.error
113 out$conf.high <- out$estimate + cv * out$std.error
114 }
115
116 # turn out into a tibble object
117 as_tibble(out)
118 }
119
120 #' @templateVar class mjoint
121 #' @template title_desc_augment
122 #'
123 #' @inherit tidy.mjoint params examples
124 #' @template param_data
125 #'
126 #' @return A [tibble::tibble()] with one row for each original observation
127 #' with addition columns:
128 #' \item{.fitted_j_0}{population-level fitted values for the
129 #' j-th longitudinal process}
130 #' \item{.fitted_j_1}{individuals-level fitted values for the j-th
131 #' longitudinal process}
132 #' \item{.resid_j_0}{population-level residuals for the j-th
133 #' longitudinal process}
134 #' \item{.resid_j_1}{individual-level residuals for the j-th
135 #' longitudinal process}
136 #'
137 #' @details See [joineRML::fitted.mjoint()] and [joineRML::residuals.mjoint()] for
138 #' more information on the difference between population-level and
139 #' individual-level fitted values and residuals.
140 #'
141 #' If fitting a joint model with a single longitudinal process,
142 #' make sure you are using a named `list` to define the formula
143 #' for the fixed and random effects of the longitudinal submodel.
144 #'
145 #' @export
146 augment.mjoint <- function(x, data = x$data, ...) {
147 check_ellipses("newdata", "augment", "mjoint", ...)
148
149 if (is.null(data)) {
150 cli::cli_abort(
151 c(
152 "{.arg data} argument is {.code NULL}.",
153 "i" = "Try specifying {.arg data} manually."
154 )
155 )
156 }
157
158 if (length(data) > 1) {
159 if (!do.call(all.equal, data)) {
160 cli::cli_abort(
161 c(
162 "List of {.arg data} extracted from {.arg x} does not include equal
163 data frames."
164 )
165 )
166 }
167 data <- data[[1]]
168 }
169
170 # longitudinal fitted values
171 fit0 <- fitted(x, level = 0)
172 names(fit0) <- paste0(".fitted_", names(fit0), "_0")
173 fit0 <- do.call(cbind.data.frame, fit0)
174 fit1 <- fitted(x, level = 1)
175 names(fit1) <- paste0(".fitted_", names(fit1), "_1")
176 fit1 <- do.call(cbind.data.frame, fit1)
177
178 # longitudinal residuals
179 res0 <- residuals(x, level = 0)
180 names(res0) <- paste0(".resid_", names(res0), "_0")
181 res0 <- do.call(cbind.data.frame, res0)
182 res1 <- residuals(x, level = 1)
183 names(res1) <- paste0(".resid_", names(res1), "_1")
184 res1 <- do.call(cbind.data.frame, res1)
185
186 as_tibble(bind_cols(data, fit0, fit1, res0, res1))
187 }
188
189 #' @templateVar class mjoint
190 #' @template title_desc_glance
191 #'
192 #' @inherit tidy.mjoint params examples
193 #'
194 #' @evalRd return_glance("AIC", "BIC", "logLik",
195 #' sigma2_j = "The square root of the estimated residual variance for
196 #' the j-th longitudinal process"
197 #' )
198 #'
199 #' @export
200 #' @family mjoint tidiers
201 #' @seealso [glance()], [joineRML::mjoint()]
202 glance.mjoint <- function(x, ...) {
203 smr <- summary(x)
204 out <- as_tibble(t(smr$sigma))
205 out$AIC <- smr$AIC
206 out$BIC <- smr$BIC
207 out$logLik <- smr$logLik
208 out
209 }