sampling_parallel: Sample from a Stan model in parallel

Description Usage Arguments Value References Examples

View source: R/sampling_parallel.R

Description

Sample from a Stan model in parallel (on Unix-like machines). This is simply a wrapper for the code on the rstan wiki to make it simple to sample from Stan models without writing lots of code each time.

Usage

1
2
sampling_parallel(object, data, chains = 4L, cores = 2L, rng_seed = NULL,
  ...)

Arguments

object

An object of class stanmodel. E.g., an object returned by stan_model.

data

An object of class list containing data to be passed to stan.

chains

Positive integer giving the number of chains to pass to stan.

cores

Positive integer giving the number of cores to use. Passed to the mc.cores argument in mclapply.

rng_seed

An optional value to pass to set.seed.

...

Anything else to pass to stan.

Value

An S4 object of class stanfit as returned from stan. The multiple chains have been joined into a single model object.

References

https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
## Not run: 
schools_code <- "data {
  int<lower=0> J; // number of schools
  real y[J]; // estimated treatment effects
  real<lower=0> sigma[J]; // s.e. of effect estimates
}
parameters {
  real mu;
  real<lower=0> tau;
  real eta[J];
}
transformed parameters {
  real theta[J];
  for (j in 1:J)
    theta[j] <- mu + tau * eta[j];
}
model {
  eta ~ normal(0, 1);
  y ~ normal(theta, sigma);
}"

schools_dat <- list(
  J = 8,
  y = c(28,  8, -3,  7, -1,  1, 18, 12),
  sigma = c(15, 10, 16, 11,  9, 11, 10, 18))

sm <- rstan::stan_model(model_code = schools_code)
fit <- sampling_parallel(sm, schools_dat, cores = 4L)
fit

## End(Not run)

seananderson/stanhelpers documentation built on May 29, 2019, 4:26 p.m.