Skip to content

Commit b783544

Browse files
consistent air format
1 parent ace1b4e commit b783544

50 files changed

Lines changed: 1465 additions & 622 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ simulations
2020
^Meta$
2121
^data-raw$
2222
^cache$
23+
^[.]?air[.]toml$

.lintr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
linters:
22
linters_with_defaults(
33
line_length_linter = line_length_linter(120),
4-
object_usage_linter = NULL
4+
object_usage_linter = NULL,
5+
indentation_linter = indentation_linter(2)
56
)

R/between-within.R

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ h_df_bw_calc <- function(object) {
8080
#' @keywords internal
8181
h_df_min_bw <- function(bw_calc, is_coef_involved) {
8282
assert_list(bw_calc)
83-
assert_names(names(bw_calc), identical.to = c("coefs_between_within", "ddf_between", "ddf_within"))
83+
assert_names(
84+
names(bw_calc),
85+
identical.to = c("coefs_between_within", "ddf_between", "ddf_within")
86+
)
8487
assert_logical(is_coef_involved, len = length(bw_calc$coefs_between_within))
8588
assert_true(sum(is_coef_involved) > 0)
8689

@@ -122,7 +125,12 @@ h_df_1d_bw <- function(object, contrast) {
122125
#' @keywords internal
123126
h_df_md_bw <- function(object, contrast) {
124127
assert_class(object, "mmrm")
125-
assert_matrix(contrast, mode = "numeric", any.missing = FALSE, ncols = length(component(object, "beta_est")))
128+
assert_matrix(
129+
contrast,
130+
mode = "numeric",
131+
any.missing = FALSE,
132+
ncols = length(component(object, "beta_est"))
133+
)
126134

127135
bw_calc <- h_df_bw_calc(object)
128136
is_coef_involved <- apply(X = contrast != 0, MARGIN = 2L, FUN = any)

R/cov_struct.R

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
#' }
2727
#'
2828
#' @keywords internal
29-
COV_TYPES <- local({ # nolint
29+
COV_TYPES <- local({
30+
# nolint
3031
type <- function(name, abbr, habbr, heterogeneous, spatial) {
3132
args <- as.list(match.call()[-1])
3233
do.call(data.frame, args)
@@ -160,8 +161,9 @@ COV_TYPES <- local({ # nolint
160161
#' @name covariance_types
161162
#' @export
162163
cov_types <- function(
163-
form = c("name", "abbr", "habbr"),
164-
filter = c("heterogeneous", "spatial")) {
164+
form = c("name", "abbr", "habbr"),
165+
filter = c("heterogeneous", "spatial")
166+
) {
165167
form <- match.arg(form, several.ok = TRUE)
166168
filter <- if (missing(filter)) c() else match.arg(filter, several.ok = TRUE)
167169
df <- COV_TYPES[form][rowSums(!COV_TYPES[filter]) == 0, ]
@@ -231,11 +233,16 @@ tmb_cov_type <- function(cov) {
231233
#' @family covariance types
232234
#' @export
233235
cov_struct <- function(
234-
type = cov_types(), visits, subject, group = character(),
235-
heterogeneous = FALSE) {
236+
type = cov_types(),
237+
visits,
238+
subject,
239+
group = character(),
240+
heterogeneous = FALSE
241+
) {
236242
# if heterogeneous isn't provided, derive from provided type
237243
if (missing(heterogeneous)) {
238-
heterogeneous <- switch(type,
244+
heterogeneous <- switch(
245+
type,
239246
toeph = ,
240247
ar1h = ,
241248
adh = ,
@@ -398,7 +405,8 @@ print.cov_struct <- function(x, ...) {
398405
#'
399406
#' @family covariance types
400407
#' @export
401-
as.cov_struct <- function(x, ...) { # nolint
408+
as.cov_struct <- function(x, ...) {
409+
# nolint
402410
UseMethod("as.cov_struct")
403411
}
404412

R/interop-emmeans.R

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ NULL
2626
#' @seealso See [emmeans::recover_data()] for background.
2727
#' @keywords internal
2828
#' @noRd
29-
recover_data.mmrm <- function(object, ...) { # nolint
29+
recover_data.mmrm <- function(object, ...) {
30+
# nolint
3031
fun_call <- stats::getCall(object)
3132
# subject_var is excluded because it should not contain fixed effect.
3233
# visit_var is not excluded because emmeans can provide marginal mean
@@ -35,7 +36,8 @@ recover_data.mmrm <- function(object, ...) { # nolint
3536
object,
3637
include = c(
3738
if (!object$formula_parts$is_spatial) "visit_var" else NULL,
38-
"response_var", "group_var"
39+
"response_var",
40+
"group_var"
3941
)
4042
)
4143
model_terms <- stats::delete.response(stats::terms(model_frame))
@@ -53,12 +55,19 @@ recover_data.mmrm <- function(object, ...) { # nolint
5355
#' @seealso See [emmeans::emm_basis()] for background.
5456
#' @keywords internal
5557
#' @noRd
56-
emm_basis.mmrm <- function(object, # nolint
57-
trms,
58-
xlev,
59-
grid,
60-
...) {
61-
model_frame <- stats::model.frame(trms, grid, na.action = stats::na.pass, xlev = xlev)
58+
emm_basis.mmrm <- function(
59+
object, # nolint
60+
trms,
61+
xlev,
62+
grid,
63+
...
64+
) {
65+
model_frame <- stats::model.frame(
66+
trms,
67+
grid,
68+
na.action = stats::na.pass,
69+
xlev = xlev
70+
)
6271
contrasts <- component(object, "contrasts")
6372
model_mat <- stats::model.matrix(trms, model_frame, contrasts.arg = contrasts)
6473
beta_hat <- component(object, "beta_est")
@@ -72,7 +81,8 @@ emm_basis.mmrm <- function(object, # nolint
7281
object,
7382
include = c(
7483
if (!object$formula_parts$is_spatial) "visit_var" else NULL,
75-
"response_var", "group_var"
84+
"response_var",
85+
"group_var"
7686
)
7787
),
7888
contrasts.arg = contrasts

R/kenwardroger.R

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ h_get_kr_comp <- function(tmb_data, theta) {
3636
#' @keywords internal
3737
h_df_md_kr <- function(object, contrast) {
3838
assert_class(object, "mmrm")
39-
assert_matrix(contrast, mode = "numeric", any.missing = FALSE, ncols = length(component(object, "beta_est")))
39+
assert_matrix(
40+
contrast,
41+
mode = "numeric",
42+
any.missing = FALSE,
43+
ncols = length(component(object, "beta_est"))
44+
)
4045
if (component(object, "reml") != 1) {
4146
stop("Kenward-Roger is only for REML")
4247
}
@@ -159,8 +164,16 @@ h_var_adj <- function(v, w, p, q, r, linear = FALSE) {
159164
theta_per_group <- nrow(q) / nrow(p)
160165
n_groups <- n_theta / theta_per_group
161166
assert_matrix(p, nrows = n_theta * n_visits)
162-
assert_matrix(q, nrows = theta_per_group^2 * n_groups * n_visits, ncols = n_visits)
163-
assert_matrix(r, nrows = theta_per_group^2 * n_groups * n_visits, ncols = n_visits)
167+
assert_matrix(
168+
q,
169+
nrows = theta_per_group^2 * n_groups * n_visits,
170+
ncols = n_visits
171+
)
172+
assert_matrix(
173+
r,
174+
nrows = theta_per_group^2 * n_groups * n_visits,
175+
ncols = n_visits
176+
)
164177
if (linear) {
165178
r <- matrix(0, nrow = nrow(r), ncol = ncol(r))
166179
}
@@ -175,15 +188,30 @@ h_var_adj <- function(v, w, p, q, r, linear = FALSE) {
175188
jid <- (j - 1) * n_beta + 1
176189
ii <- i - (gi - 1) * theta_per_group
177190
jj <- j - (gi - 1) * theta_per_group
178-
ijid <- ((ii - 1) * theta_per_group + jj - 1) * n_beta + (gi - 1) * n_beta * theta_per_group^2 + 1
191+
ijid <- ((ii - 1) * theta_per_group + jj - 1) *
192+
n_beta +
193+
(gi - 1) * n_beta * theta_per_group^2 +
194+
1
179195
if (gi != gj) {
180-
ret <- ret + 2 * w[i, j] * v %*% (-p[iid:(iid + n_beta - 1), ] %*% v %*% p[jid:(jid + n_beta - 1), ]) %*% v
196+
ret <- ret +
197+
2 *
198+
w[i, j] *
199+
v %*%
200+
(-p[iid:(iid + n_beta - 1), ] %*%
201+
v %*%
202+
p[jid:(jid + n_beta - 1), ]) %*%
203+
v
181204
} else {
182-
ret <- ret + 2 * w[i, j] * v %*% (
183-
q[ijid:(ijid + n_beta - 1), ] -
184-
p[iid:(iid + n_beta - 1), ] %*% v %*% p[jid:(jid + n_beta - 1), ] -
185-
1 / 4 * r[ijid:(ijid + n_beta - 1), ]
186-
) %*% v
205+
ret <- ret +
206+
2 *
207+
w[i, j] *
208+
v %*%
209+
(q[ijid:(ijid + n_beta - 1), ] -
210+
p[iid:(iid + n_beta - 1), ] %*%
211+
v %*%
212+
p[jid:(jid + n_beta - 1), ] -
213+
1 / 4 * r[ijid:(ijid + n_beta - 1), ]) %*%
214+
v
187215
}
188216
}
189217
}

R/mmrm-methods.R

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@ h_coef_table <- function(object) {
5858
colnames(coef_table),
5959
identical.to = c("est", "se", "df", "t_stat", "p_val")
6060
)
61-
colnames(coef_table) <- c("Estimate", "Std. Error", "df", "t value", "Pr(>|t|)")
61+
colnames(coef_table) <- c(
62+
"Estimate",
63+
"Std. Error",
64+
"df",
65+
"t value",
66+
"Pr(>|t|)"
67+
)
6268

6369
coef_aliased <- component(object, "beta_aliased")
6470
if (any(coef_aliased)) {
@@ -89,11 +95,20 @@ summary.mmrm <- function(object, ...) {
8995
)
9096
coefficients <- h_coef_table(object)
9197
call <- stats::getCall(object)
92-
components <- component(object, c(
93-
"cov_type", "reml", "n_groups", "n_theta",
94-
"n_subjects", "n_timepoints", "n_obs",
95-
"beta_vcov", "varcor"
96-
))
98+
components <- component(
99+
object,
100+
c(
101+
"cov_type",
102+
"reml",
103+
"n_groups",
104+
"n_theta",
105+
"n_subjects",
106+
"n_timepoints",
107+
"n_obs",
108+
"beta_vcov",
109+
"varcor"
110+
)
111+
)
97112
components$method <- object$method
98113
components$vcov <- object$vcov
99114
structure(
@@ -129,8 +144,15 @@ h_print_call <- function(call, n_obs, n_subjects, n_timepoints) {
129144
}
130145
if (!is.null(call$data)) {
131146
cat(
132-
"Data: ", deparse(call$data), "(used", n_obs, "observations from",
133-
n_subjects, "subjects with maximum", n_timepoints, "timepoints)",
147+
"Data: ",
148+
deparse(call$data),
149+
"(used",
150+
n_obs,
151+
"observations from",
152+
n_subjects,
153+
"subjects with maximum",
154+
n_timepoints,
155+
"timepoints)",
134156
fill = TRUE
135157
)
136158
}
@@ -152,7 +174,8 @@ h_print_cov <- function(cov_type, n_theta, n_groups) {
152174
assert_string(cov_type)
153175
assert_count(n_theta, positive = TRUE)
154176
assert_count(n_groups, positive = TRUE)
155-
cov_definition <- switch(cov_type,
177+
cov_definition <- switch(
178+
cov_type,
156179
us = "unstructured",
157180
toep = "Toeplitz",
158181
toeph = "heterogeneous Toeplitz",
@@ -182,8 +205,7 @@ h_print_cov <- function(cov_type, n_theta, n_groups) {
182205
#' @param digits (`number`)\cr number of decimal places used with [round()].
183206
#'
184207
#' @keywords internal
185-
h_print_aic_list <- function(aic_list,
186-
digits = 1) {
208+
h_print_aic_list <- function(aic_list, digits = 1) {
187209
diag_vals <- round(unlist(aic_list), digits)
188210
diag_vals <- format(diag_vals)
189211
print(diag_vals, quote = FALSE)
@@ -192,10 +214,12 @@ h_print_aic_list <- function(aic_list,
192214
#' @describeIn mmrm_methods prints the MMRM fit summary.
193215
#' @exportS3Method
194216
#' @keywords internal
195-
print.summary.mmrm <- function(x,
196-
digits = max(3, getOption("digits") - 3),
197-
signif.stars = getOption("show.signif.stars"), # nolint
198-
...) {
217+
print.summary.mmrm <- function(
218+
x,
219+
digits = max(3, getOption("digits") - 3),
220+
signif.stars = getOption("show.signif.stars"), # nolint
221+
...
222+
) {
199223
cat("mmrm fit\n\n")
200224
h_print_call(x$call, x$n_obs, x$n_subjects, x$n_timepoints)
201225
h_print_cov(x$cov_type, x$n_theta, x$n_groups)
@@ -209,7 +233,12 @@ print.summary.mmrm <- function(x,
209233
cat("\n")
210234
cat("Coefficients: ")
211235
if (x$n_singular_coefs > 0) {
212-
cat("(", x$n_singular_coefs, " not defined because of singularities)", sep = "")
236+
cat(
237+
"(",
238+
x$n_singular_coefs,
239+
" not defined because of singularities)",
240+
sep = ""
241+
)
213242
}
214243
cat("\n")
215244
stats::printCoefmat(
@@ -248,10 +277,15 @@ confint.mmrm <- function(object, parm, level = 0.95, ...) {
248277
check_subset(parm, pnames),
249278
check_integerish(parm, lower = 1L, upper = length(cf))
250279
)
251-
if (is.numeric(parm)) parm <- pnames[parm]
280+
if (is.numeric(parm)) {
281+
parm <- pnames[parm]
282+
}
252283
assert_number(level, lower = 0, upper = 1)
253284
a <- (1 - level) / 2
254-
pct <- paste(format(100 * c(a, 1 - a), trim = TRUE, scientific = FALSE, digits = 3), "%")
285+
pct <- paste(
286+
format(100 * c(a, 1 - a), trim = TRUE, scientific = FALSE, digits = 3),
287+
"%"
288+
)
255289
coef_table <- h_coef_table(object)
256290
df <- coef_table[parm, "df"]
257291
ses <- coef_table[parm, "Std. Error"]
@@ -263,7 +297,6 @@ confint.mmrm <- function(object, parm, level = 0.95, ...) {
263297
}
264298

265299

266-
267300
#' Analysis of Variance for `mmrm` Fits
268301
#'
269302
#' If supplied only one model fit, the function will calculate and return the
@@ -443,16 +476,13 @@ confint.mmrm <- function(object, parm, level = 0.95, ...) {
443476
#' # If a model was created with a different data set, refit = TRUE is needed.
444477
#' anova(fit_sex_ar1, fit_sex_race_toeph_sub, fit_interaction_us, refit = TRUE)
445478
anova.mmrm <- function(object, ..., test = TRUE, refit = FALSE) {
446-
447479
assert_class(object, "mmrm")
448480

449481
fits <- list(object, ...)
450482

451483
if (length(fits) == 1L) {
452484
out <- h_anova_single_mmrm_model(object)
453-
454485
} else {
455-
456486
# Ensure all objects in ... are mmrm fits.
457487
lapply(fits[-1L], assert_class, classes = "mmrm", .var.name = "...")
458488

@@ -498,7 +528,6 @@ anova.mmrm <- function(object, ..., test = TRUE, refit = FALSE) {
498528
)
499529

500530
if (test) {
501-
502531
h_assert_lrt_suitability(fits, refit, dfs = out$df, is_reml = out$REML)
503532

504533
model_indices_except_last <- out$Model[-length(fits)]
@@ -508,7 +537,8 @@ anova.mmrm <- function(object, ..., test = TRUE, refit = FALSE) {
508537
# first element to be NA because a pair of models is the previous row's
509538
# model plus the current row's model.
510539
out$test <-
511-
c(NA_character_,
540+
c(
541+
NA_character_,
512542
paste(model_indices_except_last, "vs", model_indices_except_first)
513543
)
514544

@@ -524,7 +554,6 @@ anova.mmrm <- function(object, ..., test = TRUE, refit = FALSE) {
524554

525555
fit_calls <- lapply(fits, component, "call")
526556
out$call <- vapply(fit_calls, deparse1, FUN.VALUE = character(1L))
527-
528557
}
529558

530559
class(out) <- union("anova.mmrm", class(out))

0 commit comments

Comments
 (0)