as.matrix.stapreg: Extract the posterior sample via matrix

Description Usage Arguments Value See Also Examples

View source: R/as.matrix.stapreg.R

Description

The posterior sample —the post-warmup draws from the posterior distribution— can be extracted from a fitted model object as a matrix, data frame, or array. The as.matrix and as.data.frame methods merge all chains together, whereas the as.array method keeps the chains separate.

Usage

1
2
3
4
5
6
7
8
## S3 method for class 'stapreg'
as.matrix(x, ..., pars = NULL, regex_pars = NULL)

## S3 method for class 'stapreg'
as.array(x, ..., pars = NULL, regex_pars = NULL)

## S3 method for class 'stapreg'
as.data.frame(x, ..., pars = NULL, regex_pars = NULL)

Arguments

x

A fitted model object returned by one of the rstap modeling functions. See stapreg-objects.

...

Ignored.

pars

An optional character vector of parameter names.

regex_pars

An optional character vector of regular expressions to use for parameter selection. regex_pars can be used in place of pars or in addition to pars. Currently, all functions that accept a regex_pars argument ignore it for models fit using optimization.

Value

A matrix, data.frame, or array, the dimensions of which depend on pars and regex_pars, as well as the model and estimation algorithm (see the Description section above).

See Also

stapreg-methods

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
if (!exists("example_model")) example(example_model)
# Extract posterior sample after MCMC
draws <- as.matrix(example_model)
print(dim(draws))

# For example, we can see that the median of the draws for the intercept 
# is the same as the point estimate rstanarm uses
print(median(draws[, "(Intercept)"]))
print(example_model$coefficients[["(Intercept)"]])

# The as.array method keeps the chains separate
draws_array <- as.array(example_model)
print(dim(draws_array)) # iterations x chains x parameters

rstap documentation built on May 1, 2019, 9:21 p.m.