Learn R Programming

rstanarm (version 2.32.1)

posterior_linpred.stanreg: Posterior distribution of the (possibly transformed) linear predictor

Description

Extract the posterior draws of the linear predictor, possibly transformed by the inverse-link function. This function is occasionally useful, but it should be used sparingly: inference and model checking should generally be carried out using the posterior predictive distribution (i.e., using posterior_predict).

Usage

# S3 method for stanreg
posterior_linpred(
  object,
  transform = FALSE,
  newdata = NULL,
  draws = NULL,
  re.form = NULL,
  offset = NULL,
  XZ = FALSE,
  ...
)

# S3 method for stanreg posterior_epred( object, newdata = NULL, draws = NULL, re.form = NULL, offset = NULL, XZ = FALSE, ... )

Value

The default is to return a draws by nrow(newdata)

matrix of simulations from the posterior distribution of the (possibly transformed) linear predictor. The exception is if the argument XZ

is set to TRUE (see the XZ argument description above).

Arguments

object

A fitted model object returned by one of the rstanarm modeling functions. See stanreg-objects.

transform

Should the linear predictor be transformed using the inverse-link function? The default is FALSE. This argument is still allowed but not recommended because the posterior_epred function now provides the equivalent of posterior_linpred(..., transform=TRUE). See Examples.

newdata, draws, re.form, offset

Same as for posterior_predict.

XZ

If TRUE then instead of computing the linear predictor the design matrix X (or cbind(X,Z) for models with group-specific terms) constructed from newdata is returned. The default is FALSE.

...

Currently ignored.

Details

The posterior_linpred function returns the posterior distribution of the linear predictor, while the posterior_epred function returns the posterior distribution of the conditional expectation. In the special case of a Gaussian likelihood with an identity link function, these two concepts are the same. The posterior_epred function is a less noisy way to obtain expectations over the output of posterior_predict.

See Also

posterior_predict to draw from the posterior predictive distribution of the outcome, which is typically preferable.

Examples

Run this code
if (.Platform$OS.type != "windows" || .Platform$r_arch != "i386") {
if (!exists("example_model")) example(example_model)
print(family(example_model))

# linear predictor on log-odds scale
linpred <- posterior_linpred(example_model)
colMeans(linpred)

# probabilities
# same as posterior_linpred(example_model, transform = TRUE)
probs <- posterior_epred(example_model) 
colMeans(probs)

# not conditioning on any group-level parameters
probs2 <- posterior_epred(example_model, re.form = NA)
apply(probs2, 2, median)
}

Run the code above in your browser using DataLab