Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

stanfit2array-method

Create array, matrix, or data.frame objects from samples in a stanfit object


Description

The samples (without warmup) included in a stanfit object can be coerced to an array, matrix, or data.frame. Methods are also provided for checking and setting names and dimnames.

Usage

## S3 method for class 'stanfit'
as.array(x, ...) 
  ## S3 method for class 'stanfit'
as.matrix(x, ...)
  ## S3 method for class 'stanfit'
as.data.frame(x, ...)
  ## S3 method for class 'stanfit'
is.array(x) 
  ## S3 method for class 'stanfit'
dim(x)
  ## S3 method for class 'stanfit'
dimnames(x)
  ## S3 method for class 'stanfit'
names(x)
  ## S3 replacement method for class 'stanfit'
names(x) <- value

Arguments

x

An object of S4 class stanfit.

...

Additional parameters that can be passed to extract for extracting samples from x. For now, pars is the only additional parameter supported.

value

For the names replacement method, a character vector to set/replace the parameter names in x.

Details

as.array and as.matrix can be applied to a stanfit object to coerce the samples without warmup to array or matrix. The as.data.frame method first calls as.matrix and then coerces this matrix to a data.frame.

The array has three named dimensions: iterations, chains, parameters. For as.matrix, all chains are combined, leaving a matrix of iterations by parameters.

Value

as.array, as.matrix, and as.data.frame return an array, matrix, and data.frame, respectively.

dim and dimnames return the dim and dimnames of the array object that could be created, while names returns the third element of the dimnames, which are the names of the margins of the posterior distribution. The names assignment method allows for assigning more interpretable names to them.

is.array returns TRUE for stanfit objects that include samples; otherwise FALSE.

When the stanfit object does not contain samples, empty objects are returned from as.array, as.matrix, as.data.frame, dim, dimnames, and names.

See Also

S4 class stanfit and its method extract

Examples

## Not run: 
ex_model_code <- '
  parameters {
    real alpha[2,3];
    real beta[2]; 
  } 
  model {
    for (i in 1:2) for (j in 1:3) 
      alpha[i, j] ~ normal(0, 1); 
    for (i in 1:2) 
      beta[i] ~ normal(0, 2); 
    # beta ~ normal(0, 2) // vectorized version
  } 
'

## fit the model 
fit <- stan(model_code = ex_model_code, chains = 4) 

dim(fit)
dimnames(fit)
is.array(fit) 
a <- as.array(fit)
m <- as.matrix(fit)
d <- as.data.frame(fit)

## End(Not run)

rstan

R Interface to Stan

v2.21.2
GPL (>= 3)
Authors
Jiqiang Guo [aut], Jonah Gabry [aut], Ben Goodrich [cre, aut], Sebastian Weber [aut], Daniel Lee [ctb], Krzysztof Sakrejda [ctb], Modrak Martin [ctb], Trustees of Columbia University [cph], Oleg Sklyar [cph] (R/cxxfunplus.R), The R Core Team [cph] (R/pairs.R, R/dynGet.R), Jens Oehlschlaegel-Akiyoshi [cph] (R/pairs.R), John Maddock [cph] (gamma.hpp), Paul Bristow [cph] (gamma.hpp), Nikhar Agrawal [cph] (gamma.hpp), Christopher Kormanyos [cph] (gamma.hpp), Bronder Steve [ctb]
Initial release
2020-07-27

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.