Skip to content

Commit 2bac1b5

Browse files
committed
Fixes for compatibility with interactions package
Predictions with interactions in the model looks like it was broken badly
1 parent 5bc0954 commit 2bac1b5

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

R/utils.R

+12-7
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ predict.wbm <- function(object, newdata = NULL, se.fit = FALSE,
7878
allow.new.levels = TRUE, na.action = na.pass, ...) {
7979

8080
# Need to re-process data when raw is FALSE
81-
if (!is.null(newdata) & raw == FALSE) {
81+
if (!is.null(newdata) && raw == FALSE) {
8282
newdata <- process_nonraw_newdata(object, newdata, re.form)
8383
}
8484

85-
if (raw == TRUE & !is.null(newdata)) {
85+
if (raw == TRUE && !is.null(newdata)) {
8686
newdata <- process_raw_newdata(object, newdata)
8787
}
8888

@@ -156,12 +156,12 @@ process_nonraw_newdata <- function(object, newdata, re.form) {
156156
}
157157

158158
process_raw_newdata <- function(object, newdata) {
159-
ints <- attr(object@frame, "interactions")
159+
ints <- object@call_info$interactions
160160
if (!is.null(ints)) {
161161
ints <- gsub(":", "*", ints)
162162
ints <- gsub("(^.*)(?=\\*)", "`\\1`", ints, perl = TRUE)
163163
ints <- gsub("(?<=\\*)(.*$)", "`\\1`", ints, perl = TRUE)
164-
demean <- attr(object@frame, "interaction.style") == "double-demean"
164+
demean <- object@call_info$interaction.style == "double-demean"
165165
if (object@call_info$model %in% c("between", "contextual", "random")) {
166166
demean <- FALSE
167167
}
@@ -397,14 +397,19 @@ make_predictions.wbm <- function(model, pred, pred.values = NULL, at = NULL,
397397
boot = FALSE, sims = 1000, progress = "txt", set.offset = NULL,
398398
new_data = NULL, return.orig.data = FALSE, partial.residuals = FALSE,
399399
message = TRUE, raw = TRUE, ...) {
400-
400+
401401
# Check if user provided own new_data
402402
if (is.null(new_data)) {
403+
meanvars <- model@call_info$meanvars
404+
if (!is.null(meanvars)) {
405+
meanvarlist <- lapply(data[meanvars], mean, na.rm = TRUE)
406+
at <- c(at, meanvarlist)
407+
}
403408
# Get the data ready with make_new_data()
404409
pm <- jtools::make_new_data(model, pred, pred.values = pred.values, at = at,
405410
data = data, center = center, set.offset = set.offset)
406411
} else {pm <- new_data}
407-
412+
408413
resp <- jtools::get_response_name(model)
409414
link_or_lm <- ifelse(family(model)$link == "identity",
410415
yes = "response", no = "link")
@@ -416,7 +421,7 @@ make_predictions.wbm <- function(model, pred, pred.values = NULL, at = NULL,
416421
}
417422

418423
# Do the predictions using built-in prediction method if robust is FALSE
419-
if (interval == FALSE & is.null(model.offset(model.frame(model)))) {
424+
if (interval == FALSE && is.null(model.offset(model.frame(model)))) {
420425
predicted <- as.data.frame(predict(model, newdata = pm,
421426
type = link_or_lm,
422427
re.form = re.form,

R/wb_lmer.R

+1
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ wbm <- function(formula, data, id = NULL, wave = NULL,
364364
num_distinct = prepped$num_distinct,
365365
varying = c(pf$varying, e$within_ints), constants = pf$constants,
366366
meanvars = pf$meanvars, model = model,
367+
interactions = c(e$within_ints, ints),
367368
max_wave = prepped$maxwave, min_wave = prepped$minwave,
368369
ints = ints, pvals = pvals, pR2 = pR2, env = the_env,
369370
mf_form = prepped$mf_form,

R/wb_utils.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ wb_formula_parser <- function(formula, dv, data, force.constants = TRUE) {
233233
out <- list(conds = conds, allvars = allvars, varying = varying,
234234
constants = constants, v_info = v_info,
235235
data = data, wint_labs = wint_labs, cint_labs = cint_labs,
236-
bint_labs = bint_labs, ranefs = ranef_forms)
236+
bint_labs = bint_labs, ranefs = ranef_forms,
237+
meanvars = v_info$meanvar)
237238
return(out)
238239

239240
}

0 commit comments

Comments
 (0)