Skip to content

Commit 2ec70ba

Browse files
authored
Merge pull request #639 from bbolker/master
migrate formula machinery from lme4 to reformulas
2 parents c683a1e + 2b12b28 commit 2ec70ba

File tree

8 files changed

+26
-25
lines changed

8 files changed

+26
-25
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ Imports:
4848
stats,
4949
survival (>= 2.40.1),
5050
RcppParallel (>= 5.0.1),
51-
utils
51+
utils,
52+
reformulas
5253
Suggests:
5354
biglm,
5455
betareg,

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ importFrom(ggplot2,scale_x_discrete)
229229
importFrom(ggplot2,theme)
230230
importFrom(ggplot2,theme_bw)
231231
importFrom(ggplot2,xlab)
232-
importFrom(lme4,findbars)
232+
importFrom(reformulas,findbars)
233233
importFrom(lme4,fixef)
234234
importFrom(lme4,glFormula)
235235
importFrom(lme4,glmer)

R/jm_data_block.R

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,13 @@ reformulate_lhs <- function(x) {
148148
# Reformulate an expression as the RHS of a model formula
149149
#
150150
# @param x The expression to reformulate
151-
# @param subbars A logical specifying whether to call lme4::subbars
151+
# @param subbars A logical specifying whether to call reformulas::subbars
152152
# on the result
153153
# @return A model formula
154154
reformulate_rhs <- function(x, subbars = FALSE) {
155155
fm <- formula(substitute(~ RHS, list(RHS = x)))
156156
if (subbars) {
157-
lme4::subbars(fm)
157+
reformulas::subbars(fm)
158158
} else {
159159
fm
160160
}
@@ -579,7 +579,7 @@ rename_t_and_cauchy <- function(prior_stuff, has) {
579579
# has_aux: logical specifying whether the glmer submodel
580580
# requires an auxiliary parameter.
581581
handle_y_mod <- function(formula, data, family) {
582-
mf <- stats::model.frame(lme4::subbars(formula), data)
582+
mf <- stats::model.frame(reformulas::subbars(formula), data)
583583
if (!length(formula) == 3L)
584584
stop2("An outcome variable must be specified.")
585585

@@ -647,7 +647,7 @@ make_y_for_stan <- function(formula, model_frame, family) {
647647
# N,K: number of rows (observations) and columns (predictors) in the
648648
# fixed effects model matrix
649649
make_x_for_stan <- function(formula, model_frame) {
650-
x_form <- lme4::nobars(formula)
650+
x_form <- reformulas::nobars(formula)
651651
x <- model.matrix(x_form, model_frame)
652652
has_intercept <- check_for_intercept(x, logical = TRUE)
653653
xtemp <- drop_intercept(x)
@@ -680,7 +680,7 @@ make_x_for_stan <- function(formula, model_frame) {
680680
# grouping factor
681681
# ngrps: a vector with the number of groups for each grouping factor
682682
make_z_for_stan <- function(formula, model_frame) {
683-
bars <- lme4::findbars(formula)
683+
bars <- reformulas::findbars(formula)
684684
if (length(bars) > 2L)
685685
stop2("A maximum of 2 grouping factors are allowed.")
686686
z_parts <- lapply(bars, split_at_bars)
@@ -816,7 +816,7 @@ append_mvmer_famlink <- function(family, is_bernoulli = FALSE) {
816816
# - the formula part (ie. the formula on the LHS of "|"), and
817817
# - the name of the grouping factor (ie. the variable on the RHS of "|")
818818
#
819-
# @param x Random effects part of a model formula, as returned by lme4::findbars
819+
# @param x Random effects part of a model formula, as returned by reformulas::findbars
820820
# @return A named list with the following elements:
821821
# re_form: a formula specifying the random effects structure
822822
# group_var: the name of the grouping factor
@@ -919,13 +919,13 @@ check_id_list <- function(id_var, y_flist) {
919919
#
920920
# @param terms The existing model frame terms object
921921
# @param formula The formula that was used to build the model frame
922-
# (but prior to having called lme4::subbars on it!)
922+
# (but prior to having called reformulas::subbars on it!)
923923
# @param data The data frame that was used to build the model frame
924924
# @return A terms object with predvars.fixed and predvars.random as
925925
# additional attributes
926926
append_predvars_attribute <- function(terms, formula, data) {
927-
fe_form <- lme4::nobars(formula)
928-
re_form <- lme4::subbars(justRE(formula, response = TRUE))
927+
fe_form <- reformulas::nobars(formula)
928+
re_form <- reformulas::subbars(justRE(formula, response = TRUE))
929929
fe_frame <- stats::model.frame(fe_form, data)
930930
re_frame <- stats::model.frame(re_form, data)
931931
fe_terms <- attr(fe_frame, "terms")
@@ -1587,7 +1587,7 @@ parse_assoc_data <- function(x, user_x) {
15871587
if (identical(length(fm), 3L))
15881588
stop(paste0("Formula specified for '", x, "' association structure should not ",
15891589
"include a response."), call. = FALSE)
1590-
if (length(lme4::findbars(fm)))
1590+
if (length(reformulas::findbars(fm)))
15911591
stop(paste0("Formula specified for '", x, "' association structure should only ",
15921592
"include fixed effects."), call. = FALSE)
15931593
if (fm[[2L]] == 1)

R/jm_make_assoc_parts.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,17 @@ make_assoc_parts_for_stan <- function(newdata, y_mod, include_Zt = TRUE) {
190190
# construct model frame using predvars
191191
formula <- use_predvars(y_mod, keep_response = FALSE)
192192
data <- as.data.frame(newdata)
193-
model_frame <- stats::model.frame(lme4::subbars(formula), data)
193+
model_frame <- stats::model.frame(reformulas::subbars(formula), data)
194194

195195
# fe design matrices
196-
x_form <- lme4::nobars(formula)
196+
x_form <- reformulas::nobars(formula)
197197
x <- model.matrix(x_form, model_frame)
198198
xtemp <- drop_intercept(x)
199199
x_bar <- y_mod$x$x_bar
200200
xtemp <- sweep(xtemp, 2, x_bar, FUN = "-")
201201

202202
# re design matrices
203-
bars <- lme4::findbars(formula)
203+
bars <- reformulas::findbars(formula)
204204
if (length(bars) > 2L)
205205
stop2("A maximum of 2 grouping factors are allowed.")
206206
z_parts <- lapply(bars, split_at_bars)
@@ -214,7 +214,7 @@ make_assoc_parts_for_stan <- function(newdata, y_mod, include_Zt = TRUE) {
214214

215215
# optionally add the sparse Zt matrix
216216
if (include_Zt)
217-
ret$Zt <- lme4::mkReTrms(bars, model_frame)$Zt
217+
ret$Zt <- reformulas::mkReTrms(bars, model_frame)$Zt
218218

219219
# add offset values
220220
if ('offset' %in% colnames(newdata))

R/pp_data.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pp_data <-
9797
offset <- model.offset(model.frame(object, m = m))
9898
if (!is.null(newdata) && (!is.null(offset) || !is.null(object$call$offset))) {
9999
if (is.jm(object)) {
100-
form <- lme4::subbars(object$formula[[m]])
100+
form <- reformulas::subbars(object$formula[[m]])
101101
form[2] <- NULL # get rid of response to avoid error that it isn't found in newdata
102102
mf <- stats::model.frame(form, data = newdata)
103103
offset <- model.offset(mf)
@@ -157,7 +157,7 @@ pp_data <-
157157
form <- if (is.null(m)) attr(object$glmod$fr, "formula") else
158158
formula(object, m = m)
159159
L <- length(form)
160-
form[[L]] <- lme4::nobars(form[[L]])
160+
form[[L]] <- reformulas::nobars(form[[L]])
161161
RHS <- formula(substitute(~R, list(R = form[[L]])))
162162
Terms <- terms(object, m = m)
163163
mf <- model.frame(object, m = m)
@@ -225,7 +225,7 @@ pp_data <-
225225
if (length(fit.na.action <- attr(mfnew,"na.action")) > 0) {
226226
newdata <- newdata[-fit.na.action,]
227227
}
228-
ReTrms <- lme4::mkReTrms(lme4::findbars(re.form[[2]]), rfd)
228+
ReTrms <- reformulas::mkReTrms(reformulas::findbars(re.form[[2]]), rfd)
229229
if (!allow.new.levels && any(vapply(ReTrms$flist, anyNA, NA)))
230230
stop("NAs are not allowed in prediction data",
231231
" for grouping variables unless 'allow.new.levels' is TRUE.")

R/stan_clogit.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
#' # not a random variable b/c probabilities add to 1 within strata
9191
#' all.equal(rep(sum(nd$case), nrow(pr)), rowSums(pr))
9292
#' }
93-
#' @importFrom lme4 findbars
93+
#' @importFrom reformulas findbars
9494
stan_clogit <- function(formula, data, subset, na.action = NULL, contrasts = NULL,
9595
...,
9696
strata, prior = normal(autoscale=TRUE),

R/stanmvreg-methods.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ formula.stanmvreg <- function (x, fixed.only = FALSE, random.only = FALSE, m = N
192192
stop2("Could not find formula in stanmvreg object.")
193193
if (fixed.only) {
194194
for (i in 1:M)
195-
form[[i]][[length(form[[i]])]] <- lme4::nobars(form[[i]][[length(form[[i]])]])
195+
form[[i]][[length(form[[i]])]] <- reformulas::nobars(form[[i]][[length(form[[i]])]])
196196
}
197197
if (random.only) {
198198
for (i in 1:M)
@@ -229,7 +229,7 @@ terms.stanmvreg <- function(x, fixed.only = TRUE, random.only = FALSE, m = NULL,
229229
} else if (random.only) {
230230
Terms <- lapply(seq(M), function(i) {
231231
re_form <- formula.stanmvreg(x, random.only = TRUE, m = i)
232-
tt <- terms.formula(lme4::subbars(re_form))
232+
tt <- terms.formula(reformulas::subbars(re_form))
233233
attr(tt, "predvars") <- attr(mvmer_terms[[i]], "predvars.random")
234234
tt
235235
})

R/stanreg-methods.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ terms.stanreg <- function(x, ..., fixed.only = TRUE, random.only = FALSE) {
459459
attr(Terms, "predvars") <- attr(terms(fr), "predvars.fixed")
460460
}
461461
if (random.only) {
462-
Terms <- terms.formula(lme4::subbars(formula.stanreg(x, random.only = TRUE)))
462+
Terms <- terms.formula(reformulas::subbars(formula.stanreg(x, random.only = TRUE)))
463463
attr(Terms, "predvars") <- attr(terms(fr), "predvars.random")
464464
}
465465

@@ -513,7 +513,7 @@ coef_mer <- function(object, ...) {
513513

514514
justRE <- function(f, response = FALSE) {
515515
response <- if (response && length(f) == 3) f[[2]] else NULL
516-
reformulate(paste0("(", vapply(lme4::findbars(f),
516+
reformulate(paste0("(", vapply(reformulas::findbars(f),
517517
function(x) paste(deparse(x, 500L),
518518
collapse = " "),
519519
""), ")"),
@@ -533,7 +533,7 @@ formula_mer <- function (x, fixed.only = FALSE, random.only = FALSE, ...) {
533533
}
534534
if (fixed.only) {
535535
form <- attr(fr, "formula")
536-
form[[length(form)]] <- lme4::nobars(form[[length(form)]])
536+
form[[length(form)]] <- reformulas::nobars(form[[length(form)]])
537537
}
538538
if (random.only)
539539
form <- justRE(form, response = TRUE)

0 commit comments

Comments
 (0)