README.md

gsim

gsim is a DSL for statistical simulations. It seeks to:

gsim is designed to work with the magrittr pipe %>% to create clean and readable pipelines of objects transformations. To install the package:

install.packages("devtools")
devtools::install_github("lionel-/gsim")

Setup data

# Fetch Radon dataset from Stan's repo
radon_data <- radon_data()

# Create the list of data, including design matrices
radon_data <- radon_data %>%
  define(
    y, county,
    X = design(1, x),
    U = design(1, u, .unjoin = county),
    N = nrow(.),
    J = n_distinct(county),
    P_X = ncol(X),
    P_U = ncol(U)
  )

# Get example model code
radon_model <- radon_model(language = "stan", variant = "centered")

# Fit the model
stanfit <- rstan::stan(model_code = radon_model, data = radon_data)

dplyr-like verbs

You can work directly from a Stan or Jags object with gsim functions. They will be converted to an object of class sims which is simply a list of simulations arrays with some additional information.

Note that unlike the method in dplyr, summarise() does not discard the other variables: sims lists can contain both sims_array and normal objects, with the only constraint to keep all the sims_array objects consistent (i.e., same number of simulations).

sims <- stanfit %>%
  select(sigma, contains("Beta")) %>%
  mutate(Beta_res = Beta - cols_mean(Beta)) %>%
  summarise(
    post_sd = sd(Beta_res),
    post_mean = mean(Beta_res)
  )

sims
#> 100 simulations
#>
#>       sigma: [1:1]
#>        Beta: [1:85, 1:2]
#>  sigma_Beta: [1:2]
#>  Sigma_Beta: [1:2, 1:2]
#>    Beta_res: [1:85, 1:2]
#>
#> Other objects: post_sd, post_mean


lionel-/gsim documentation built on May 21, 2019, 6:41 a.m.