Learn R Programming

mcmcsae (version 0.6.0)

residuals-fitted-values: Extract draws of fitted values or residuals from a draws object

Description

For a model created with create_sampler and estimated using MCMCsim, these functions return the posterior draws of fitted values or residuals. In the current implementation the fitted values correspond to the linear predictor and the residuals are computed as the data vector minus the fitted values, regardless of the model's distribution family. For large datasets the returned object can become very large. One may therefore select a subset of draws or chains or use mean.only=TRUE to return a vector of posterior means only.

Usage

# S3 method for draws
fitted(
  object,
  mean.only = FALSE,
  units = NULL,
  chains = seq_len(nchains(object)),
  draws = seq_len(ndraws(object)),
  matrix = FALSE,
  type = c("link", "response"),
  ...
)

# S3 method for draws residuals( object, mean.only = FALSE, units = NULL, chains = seq_len(nchains(object)), draws = seq_len(ndraws(object)), matrix = FALSE, ... )

Arguments

object

a draws object.

mean.only

if TRUE only the vector of posterior means is returned. In that case the subsequent arguments are ignored. Default is FALSE.

units

the data units (by default all) for which fitted values or residuals should be computed.

chains

optionally, a selection of chains.

draws

optionally, a selection of draws per chain.

matrix

whether a matrix should be returned instead of a dc object.

type

the type of fitted values: "link" for fitted values on the linear predictor scale (the default), and "response" for fitted values on the response scale. Returned residuals are always on the response scale.

...

currently not used.

Value

Either a draws component object or a matrix with draws of fitted values or residuals. The residuals are always on the response scale, whereas fitted values can be on the scale of the linear predictor or the response depending on type. If mean.only=TRUE, a vector of posterior means.

Examples

Run this code
# NOT RUN {
ex <- mcmcsae_example(n=50)
sampler <- create_sampler(ex$model, data=ex$dat)
sim <- MCMCsim(sampler, burnin=100, n.iter=300, thin=2, store.all=TRUE)
fitted(sim, mean.only=TRUE)
summary(fitted(sim))
residuals(sim, mean.only=TRUE)
summary(residuals(sim))
bayesplot::mcmc_intervals(as.matrix(subset(residuals(sim), vars=1:20)))
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab