Description Usage Arguments Examples
Returns extracted samples from a Stan model in a data frame format. This makes it easier to work with the output using, for example, the dplyr or ggplot2 packages.
1 | extract_df(x, output = c("list", "wide_df", "long_df"), sep = "_")
|
x |
A sampled Stan model |
output |
Should the samples be returned as a list of data frames
( |
sep |
A separator between the variable name and indices when naming
columns. E.g. an underscore would result in |
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 | 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(sm, schools_dat, iter = 100, chains = 1)
str(rstan::extract(fit))
str(extract_df(fit))
head(extract_df(fit, "wide_df"))
head(extract_df(fit, "long_df"))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.