R: RStan - R interface to Stan

rstan-package {rstan} | R Documentation

## RStan — R interface to Stan

### Description

Stan Development Team

RStan is the R interface to the Stan C++ package. RStan provides

- full Bayesian inference using the No-U-Turn sampler (NUTS), a variant of Hamiltonian Monte Carlo (HMC)

- approximate Bayesian inference using automatic differentiation variational inference (ADVI)

- penalized maximum likelihood estimation using L-BFGS optimization

For documentation on the Stan modeling language see the Stan Modeling Language User's Guide and Reference Manual.

### Other R packages from the Stan Development Team

Various related R packages are also available from the Stan Development Team:

Package | Description | Link
bayesplot | ggplot-based plotting library for graphing parameter estimates, MCMC diagnostics, and posterior predictive checks. | bayesplot-package
shinystan | Interactive visual summaries and advanced posterior analysis of MCMC output. | shinystan-package
loo | Out-of-sample predictive performance estimates and model comparison. | loo-package
rstanarm | R formula interface for Bayesian applied regression modeling. | rstanarm-package
rstantools | Tools for developers of R packages interfacing with Stan. | rstantools-package

### Author(s)

Authors: | Jiqiang Guo <guojq28@gmail.com>
| Ben Goodrich <benjamin.goodrich@columbia.edu>
| Jonah Gabry <jsg2201@columbia.edu>

Maintainer: | Ben Goodrich <benjamin.goodrich@columbia.edu>

### See Also

- `stan` for details on fitting models and `stanfit` for information on the fitted model objects.

- https://github.com/stan-dev/rstan/issues/ to submit a bug report or feature request.

- http://discourse.mc-stan.org to ask a question on the Stan-users forum.

### Examples

    ## Not run:

    stanmodelcode <- "
    data {
      int<lower=0> N;
      real y[N];
    }

    parameters {
      real mu;
    }

    model {
      target += normal_lpdf(mu | 0, 10);
      target += normal_lpdf(y  | mu, 1);
    }
    "

    y <- rnorm(20)
    dat <- list(N = 20, y = y);
    fit <- stan(model_code = stanmodelcode, model_name = "example",
                data = dat, iter = 2012, chains = 3, sample_file = 'norm.csv',
                verbose = TRUE)
    print(fit)
    traceplot(fit)

    # extract samples
    e <- extract(fit, permuted = TRUE) # return a list of arrays
    mu <- e$mu

    m <- extract(fit, permuted = FALSE, inc_warmup = FALSE) # return an array
    print(dimnames(m))

    # using as.array directly on stanfit objects
    m2 <- as.array(fit)

    ## End(Not run)

[Package rstan version 2.17.3 Index]