Methods for the {rstantools} posterior-prediction generics. They return a
draws-by-observation matrix and enable {tidybayes} workflows such as
add_epred_draws(), add_predicted_draws(), and add_linpred_draws().
mcp exports these generics, so they work without {rstantools}. If {rstantools} or
{brms} masks them, their generics dispatch to the same methods.
Usage
# S3 method for class 'mcpfit'
posterior_epred(
object,
newdata = NULL,
draws = NULL,
ndraws = NULL,
re.form = NULL,
re_formula = NULL,
dpar = NULL,
seed = NULL,
rate = FALSE,
...
)
# S3 method for class 'mcpfit'
posterior_predict(
object,
newdata = NULL,
draws = NULL,
ndraws = NULL,
re.form = NULL,
re_formula = NULL,
seed = NULL,
rate = FALSE,
arma = TRUE,
...
)
# S3 method for class 'mcpfit'
posterior_linpred(
object,
transform = FALSE,
newdata = NULL,
draws = NULL,
ndraws = NULL,
re.form = NULL,
re_formula = NULL,
dpar = NULL,
seed = NULL,
...
)
posterior_epred(object, ...)
posterior_predict(object, ...)
posterior_linpred(object, ...)Arguments
- object
An
mcpfitobject.- newdata
A
tibbleor adata.framecontaining predictors in the model.If
NULL(default), the original data is used.Grouping factors must only contain levels from the fitted data. Use
group_effects = FALSEto predict without group-level effects, e.g., for new groups.For models with
ar()orma():fitted(),residuals(),log_lik(), andpredict()condition on the response history by default, sonewdatamust include the response. Forfitted(),predict(), andresiduals(), missing response histories are supported only in the original fitted data, using retained posterior imputations as histories. Predictions are fresh response draws, including at missing rows. Witharma = "replicate",predict()andposterior_predict()generate fresh response series recursively, so theirnewdataneed only contain predictors and required response auxiliaries.log_lik()is unavailable when a missing response enters a later observed history.For models with
y | weights(): Require the weights column except forfitted()andpredict().
- draws, ndraws
Number of posterior draws to return.
drawsfollows the{rstantools}convention;ndrawsis the mcp spelling. Supply at most one.- re.form, re_formula
Group-level effects to include.
NULLincludes all effects andNAexcludes them.- dpar
Distributional parameter for
posterior_epred()andposterior_linpred();NULLuses the expected response.- seed
Optional integer seed for draw selection and posterior prediction.
- rate
Logical scalar. For binomial models, return counts (
rate = FALSE, the default forfitted()andpredict()) or the observed or expected success proportion (rate = TRUE). Predictions and count-scale fitted values require a trials column innewdata. Distributional parameters such asdpar = "mu"evaluate the parameter itself (e.g., success probability) and are unaffected byrate. Other families do not supportrate = TRUE. For rates per unit of exposure in models withoffset(log(exposure)), setexposure = 1innewdata.- ...
Must be empty. Reserved for future use.
- arma
How to include AR and MA effects.
TRUE: Condition on the observed response history, i.e., one-step-ahead evaluation of the GARMA residual recurrence. Requires the response variable innewdata.FALSE: Disregard AR and MA effects. Forfamily = gaussian(),predict()uses onlysigmafor residuals."replicate": Generate fresh response series recursively instead of conditioning on the observed responses. Only for predictions:predict(),posterior_predict(), andpp_check()(where it is the default). Applies equally to prior and posterior draws.
For posterior evaluation of the original data, retained JAGS imputations supply missing GARMA histories. In models with group-level effects, this currently requires all such effects (
group_effects = TRUE).- transform
For
posterior_linpred(), return the inverse-linked parameter on the response scale instead of the linear predictor. For binomial models, this returns the success probability rather than expected counts.
Details
For GARMA models, posterior_predict() conditions on the observed
response history, just like predict(). Use arma = "replicate" in either
method to generate fresh response histories recursively. Missing responses
in the original data are filled with retained imputations only to supply
histories. With arma = TRUE, posterior_predict() draws from the response
distributions whose means posterior_epred() returns, including at missing
rows. These methods require posterior draws. For prior prediction, use
predict() with prior = TRUE; arma selects the same behavior.
For binomial models, posterior_epred() and posterior_predict() (and
corresponding {tidybayes} workflows such as add_epred_draws()) follow
{brms} and {rstantools} conventions by returning values on the outcome
count scale (rate = FALSE), i.e., expected counts \(E[Y] = n\mu\) and
simulated counts in \(\{0, \dots, n\}\), matching fitted() and predict().
Use rate = TRUE for proportions. To obtain the success probability
parameter \(\mu\) on the \([0, 1]\) scale regardless of trial counts, pass
dpar = "mu". In contrast, posterior_linpred(transform = TRUE) returns the
inverse-linked parameter directly, evaluating success probabilities \(\mu\)
in \([0, 1]\) rather than counts.
