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.

# S3 method for stapreg
as.matrix(x, ..., pars = NULL, regex_pars = NULL,
  include_X = FALSE)

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

# S3 method for 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.

include_X

logical to include latent exposure covariate in samples

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

Examples

# \donttest{ if (!exists("example_model")) example(example_model)
#> #> exmpl_> ## following lines make example run faster #> exmpl_> distdata <- subset(homog_longitudinal_bef_data[,c("subj_ID","measure_ID","class","dist")], #> exmpl_+ subj_ID<=10) #> #> exmpl_> timedata <- subset(homog_longitudinal_bef_data[,c("subj_ID","measure_ID","class","time")], #> exmpl_+ subj_ID<=10) #> #> exmpl_> timedata$time <- as.numeric(timedata$time) #> #> exmpl_> subjdata <- subset(homog_longitudinal_subject_data,subj_ID<=10) #> #> exmpl_> example_model <- #> exmpl_+ stap_glmer(y_bern ~ centered_income + sex + centered_age + stap(Coffee_Shop) + (1|subj_ID), #> exmpl_+ family = gaussian(), #> exmpl_+ subject_data = subjdata, #> exmpl_+ distance_data = distdata, #> exmpl_+ time_data = timedata, #> exmpl_+ subject_ID = 'subj_ID', #> exmpl_+ group_ID = 'measure_ID', #> exmpl_+ prior_intercept = normal(location = 25, scale = 4, autoscale = FALSE), #> exmpl_+ prior = normal(location = 0, scale = 4, autoscale = FALSE), #> exmpl_+ prior_stap = normal(location = 0, scale = 4), #> exmpl_+ prior_theta = list(Coffee_Shop = list(spatial = log_normal(location = 1, #> exmpl_+ scale = 1), #> exmpl_+ temporal = log_normal(location = 1, #> exmpl_+ scale = 1))), #> exmpl_+ max_distance = 3, max_time = 50, #> exmpl_+ # chains, cores, and iter set to make the example small and fast #> exmpl_+ chains = 1, iter = 25, cores = 1) #> #> SAMPLING FOR MODEL 'stap_continuous' NOW (CHAIN 1). #> Chain 1: #> Chain 1: Gradient evaluation took 0.000364 seconds #> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 3.64 seconds. #> Chain 1: Adjust your expectations accordingly! #> Chain 1: #> Chain 1: #> Chain 1: WARNING: No variance estimation is #> Chain 1: performed for num_warmup < 20 #> Chain 1: #> Chain 1: Iteration: 1 / 25 [ 4%] (Warmup) #> Chain 1: Iteration: 2 / 25 [ 8%] (Warmup) #> Chain 1: Iteration: 4 / 25 [ 16%] (Warmup) #> Chain 1: Iteration: 6 / 25 [ 24%] (Warmup) #> Chain 1: Iteration: 8 / 25 [ 32%] (Warmup) #> Chain 1: Iteration: 10 / 25 [ 40%] (Warmup) #> Chain 1: Iteration: 12 / 25 [ 48%] (Warmup) #> Chain 1: Iteration: 13 / 25 [ 52%] (Sampling) #> Chain 1: Iteration: 14 / 25 [ 56%] (Sampling) #> Chain 1: Iteration: 16 / 25 [ 64%] (Sampling) #> Chain 1: Iteration: 18 / 25 [ 72%] (Sampling) #> Chain 1: Iteration: 20 / 25 [ 80%] (Sampling) #> Chain 1: Iteration: 22 / 25 [ 88%] (Sampling) #> Chain 1: Iteration: 24 / 25 [ 96%] (Sampling) #> Chain 1: Iteration: 25 / 25 [100%] (Sampling) #> Chain 1: #> Chain 1: Elapsed Time: 0.357462 seconds (Warm-up) #> Chain 1: 1.02111 seconds (Sampling) #> Chain 1: 1.37857 seconds (Total) #> Chain 1:
#> Warning: The largest R-hat is 1.47, indicating chains have not mixed. #> Running the chains for more iterations may help. See #> http://mc-stan.org/misc/warnings.html#r-hat
#> Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable. #> Running the chains for more iterations may help. See #> http://mc-stan.org/misc/warnings.html#bulk-ess
#> Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable. #> Running the chains for more iterations may help. See #> http://mc-stan.org/misc/warnings.html#tail-ess
#> Warning: Markov chains did not converge! Do not analyze results!
# Extract posterior sample after MCMC draws <- as.matrix(example_model) print(dim(draws))
#> [1] 13 19
# 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)"]))
#> [1] 0.2950538
print(example_model$coefficients[["(Intercept)"]])
#> [1] 0.2950538
# The as.array method keeps the chains separate draws_array <- as.array(example_model) print(dim(draws_array)) # iterations x chains x parameters
#> [1] 13 1 19
# }