id_post_pred-idealstan-method: Posterior Prediction for 'idealstan' objects

id_post_pred,idealstan-methodR Documentation

Posterior Prediction for idealstan objects

Description

This function will draw from the posterior distribution, whether in terms of the outcome (prediction) or to produce the log-likelihood values.

This function can also produce either distribution of the outcomes (i.e., predictions) or the log-likelihood values of the posterior (set option type to 'log_lik'. For more information, see the package vignette How to Evaluate Models.

You can then use functions such as id_plot_ppc() to see how well the model does returning the correct number of categories in the score/vote matrix. Also see help("posterior_predict", package = "rstanarm")

Usage

## S4 method for signature 'idealstan'
id_post_pred(
  object,
  newdata = NULL,
  draws = 100,
  output = NULL,
  type = "predict",
  covar = "person",
  sample_scores = NULL,
  item_subset = NULL,
  pred_outcome = NULL,
  use_cores = 1,
  use_chain = NULL,
  skip_cov = FALSE,
  ...
)

Arguments

object

A fitted idealstan object

newdata

Optional: pass a data frame that must have all of the predictors that were given to the id_make function. Used to generate predictions from person or item covariates on to items.

draws

The number of draws to use from the total number of posterior draws (default is 100). Set to "all" to use all draws in the chains. For reproducibility, you can also pass a vector of specific draws to use.

output

Which outcomes to predict: 'observed' (the outcome, predicted for every observation as if it were observed), 'missing' (1 if an observation is predicted to be missing and 0 if not, for models with missing-data inflation) or 'all' (the outcome, with the predicted missing values of models with missing-data inflation coded as in the data, as an extra category; discrete outcomes only). The default (NULL) is 'all' for binary and ordinal outcomes and 'observed' for the other outcomes.

type

Whether to produce posterior predictive values ('predict', the default), the posterior expected (average) values ('epred'), or log-likelihood values ('log_lik'). See the How to Evaluate Models vignette for more info. The expected value is the mean of the outcome: the probability of a 1 for binary models, the expected category (1 = lowest) for ordinal models, and the mean count, number of successes or value for the other models. For models with missing-data inflation it is multiplied by the probability that the outcome is observed, and the log-likelihood includes the probability of the outcome being observed or missing.

covar

What kind of covariates to include as part of the prediction – either "person" (the default) or "items" if you included predictors for item discriminations.

sample_scores

In addition to reducing the number of posterior draws used to calculate the posterior predictive distribution, which will reduce computational overhead. Only available for calculating predictive distributions, not log-likelihood values.

item_subset

Whether to calculate marginal effects for only a subset of items. Should be item IDs that match the item_id column passed to the id_make function.

pred_outcome

In the case of ordinal responses, the number of the category to predict. Defaults to top category.

use_cores

Number of cores to use for computing the predictions of the items in parallel. Defaults to 1 (no parallelization). The work runs in parallel through the future package: with more than one core and no plan set, a future::multisession plan with that many workers (separate R sessions, which work everywhere, including Windows, RStudio and Positron). To use another backend, set it with future::plan() before calling, for example future::plan(future::multicore, workers = 4); that plan is then used whatever the number of cores.

use_chain

ID of MCMC chain to use rather than all chains (the default).

skip_cov

Whether to skip adding in hierarchical person-level covariates. Defaults to FALSE.

...

Any other arguments passed on to posterior_predict (currently none available)

Value

An object of class id_pred_obj (a list of matrices of posterior-predicted draws, one per model type). If type="log_lik", a draws-by-observations matrix of class log_lik with a chain_order attribute for use with derive_chain.

Examples



sim <- id_sim_gen()
est <- id_estimate(sim, model_type=1, fixtype='vb_full',
                   use_method="pathfinder", nchains=2, ncores=2)
# posterior predictive distribution (100 draws)
ppc <- id_post_pred(est, draws=50)
# log-likelihood matrix (for loo)
ll <- id_post_pred(est, type='log_lik')



idealstan documentation built on Dec. 2, 2026, 5:07 p.m.