predict.wbm: Predictions and simulations from within-between models

View source: R/utils.R

predict.wbmR Documentation

Predictions and simulations from within-between models

Description

These methods facilitate fairly straightforward predictions and simulations from wbm models.

Usage

## S3 method for class 'wbm'
predict(
  object,
  newdata = NULL,
  se.fit = FALSE,
  raw = FALSE,
  use.re.var = FALSE,
  re.form = NULL,
  type = c("link", "response"),
  allow.new.levels = TRUE,
  na.action = na.pass,
  ...
)

## S3 method for class 'wbm'
simulate(
  object,
  nsim = 1,
  seed = NULL,
  use.u = FALSE,
  newdata = NULL,
  raw = FALSE,
  newparams = NULL,
  re.form = NA,
  type = c("link", "response"),
  allow.new.levels = FALSE,
  na.action = na.pass,
  ...
)

Arguments

object

a fitted model object

newdata

data frame for which to evaluate predictions.

se.fit

Return a list containing predictions and fixed-effect standard errors? With boot = TRUE, returns bootstrap draws in fit instead.

raw

Is newdata a merMod model frame or panel_data? TRUE indicates a merMod-style newdata, with all of the extra columns created by wbm.

use.re.var

Unsupported; must be FALSE. Random-effect variance is not added to fixed-effect standard errors.

re.form

(formula, NULL, or NA) specify which random effects to condition on when predicting. If NULL, include all random effects; if NA or ~0, include no random effects.

type

character string - either "link", the default, or "response" indicating the type of prediction object returned.

allow.new.levels

logical if new levels (or NA values) in newdata are allowed. If FALSE (default), such new values in newdata will trigger an error; if TRUE, then the prediction will use the unconditional (population-level) values for data with previously unobserved levels (or NAs).

na.action

function determining what should be done with missing values for fixed effects in newdata. The default is to predict NA: see na.pass.

...

When boot and se.fit are TRUE, any additional arguments are passed to lme4::bootMer().

nsim

positive integer scalar - the number of responses to simulate.

seed

an optional seed to be used in set.seed immediately before the simulation so as to generate a reproducible sample.

use.u

(logical) if TRUE, generate a simulation conditional on the current random-effects estimates; if FALSE generate new Normally distributed random-effects values. (Redundant with re.form, which is preferred: TRUE corresponds to re.form = NULL (condition on all random effects), while FALSE corresponds to re.form = ~0 (condition on none of the random effects).)

newparams

new parameters to use in evaluating predictions, specified as in the start parameter for lmer or glmer – a list with components theta and beta and (for LMMs or GLMMs that estimate a scale parameter) sigma

Details

With no newdata, predictions use the fitted model's design matrix. re.form = NA or ~0 excludes random effects; the default includes them.

With raw = FALSE, supply panel histories containing the original predictor variables and the entity identifier. The outcome is not required unless it also occurs as a predictor (for example, a lagged outcome). Within-person means and detrending are calculated from the supplied histories, using the variable roles, factor coding, basis parameters, and scaling saved at fitting. Predictions are returned in the input row order. Include the wave variable for models with time effects, detrending, lags, or leads. Do not use a hypothetical plotting grid as a panel history: its means are arbitrary.

With raw = TRUE, supply prepared model-scale rows, including imean(...) and processed interaction columns. These columns are passed through unchanged. This argument is distinct from interaction.style = "raw" when fitting. For models fitted with an offset argument, prepared rows must include .offset; panel histories may instead contain the original offset variables.

Interaction plots

interactions::interact_plot() and interactions::cat_plot() use a separate hypothetical-grid path through jtools::make_predictions(). With an interactions version supporting plot.scale, the default plot.scale = "within" displays numeric within-person predictors as deviations from each person's observed mean, in original units even for standardized fits. Zero means at the person's mean. Default predictor ranges use estimation-sample deviations; numeric within-person moderators default to zero and plus/minus one pooled within-person SD (or their observed values when there are only two). Constant predictors/moderators retain their original units or categories. The outcome remains on its modeled response scale, not a within-person deviation. outcome.scale = "link" instead uses the model's link scale.

Use plot.scale = "original" for raw predictor levels. Plot captions report reference person means, and values implying deviations outside the observed within-person range trigger warnings. These marginal range checks do not establish joint support or rule out other forms of extrapolation. User-supplied numeric predictor/moderator values and at use the selected display scale; imean(...) overrides always use original units. Custom labels are preserved. Plot data must be the original fitting data or a subset with panel identifiers; excluded estimation rows are dropped and subsets retain fitted person means. Use at for hypothetical values, not modified plotting data.

Person means and interaction-product means are held at their observation-weighted averages over the estimation sample, while focal predictors vary. This describes conditional predictions, not population-average effects or forecasts for a particular entity. Use at = list("imean(x)" = value) (and, when needed, "imean(x:z)") to set different reference means. Time-invariant moderators multiply the centered within-person term; their product means are not independently held fixed as those moderators change. For a within-varying factor, its reference means are the means of its indicator columns. Direct calls to jtools::make_predictions() (and older interactions versions without plot.scale) retain original-scale inputs. plot.scale is a plotting option, not an argument to predict() or an estimation option.

Automatic plotting grids currently support scalar numeric/factor terms, including two- and three-way interactions. Scalar transformations such as log(x) require plot.scale = "original"; a deviation of log(x) is not a deviation of x. Detrended, lagged/lead, and basis-expansion models require complete panel histories for prediction or explicitly prepared rows via make_predictions(..., new_data = ..., raw = TRUE). Partial residuals, custom plotting covariance estimates, and prediction intervals are not supported. Updated interactions versions provide S3 methods for sim_slopes(), johnson_neyman(), and probe_interaction(). Numeric focal predictors use exact design-matrix contrasts, not refitting or recentering. Slopes share the plots' units and reference settings. Tests are fixed-effect and link-scale: residual-df t approximations for Gaussian mixed models, normal approximations for generalized models and GEE, with an explicit df override. They are not Satterthwaite/Kenward-Roger tests. Nonlinear/factor focal predictors, predictor-dependent offsets, rank-deficient fits, custom covariance estimates, response-scale derivatives for non-identity links, and FDR-adjusted Johnson-Neyman inference are not supported. These plotting and slope conventions do not change the fitted estimator. Models saved by older panelr versions must be refitted to use the new panel-history and automatic-grid paths; prepared-row predictions remain available.

Analytic se.fit values describe fixed-effect uncertainty, conditional on the preprocessing/reference quantities; response-scale standard errors use the delta method. Plot bands are confidence intervals, not intervals for individual outcomes. use.re.var = TRUE is rejected because the previous variance-addition calculation did not yield valid prediction intervals.

Examples

data("WageData")
wages <- panel_data(WageData, id = id, wave = t)
model <- wbm(lwage ~ lag(union) + wks, data = wages)
# With no newdata, predictions use the fitted design.
predict(model)

panelr documentation built on Nov. 7, 2026, 1:07 a.m.