This package facilitates editing of stan model code using R. This includes adding/removing blocks, extracting variable names, and inserting/removing lines.
Development of this package was inspired by the <bi_model>
class in
the rbi package.
The stable version of the package can be installed using
install.packages("stanedit", repos = "https://epiforecasts.r-universe.dev/")
The development version can be installed using pak
pak::pak("epiforecasts/stanedit")
First we load a stan model and prepare it for editing using the functionality in this package.
library("stanedit")
model_file_name <- system.file(package = "stanedit", "regression.stan")
reg <- stanedit(filename = model_file_name)
reg
## data {
## int<lower=0> N;
## vector[N] x;
## vector[N] y;
## }
## parameters {
## real alpha;
## real beta;
## real<lower=0> sigma;
## }
## model {
## y ~ normal(alpha + beta * x, sigma);
## }
Let’s say we want to add standard normal priors for alpha and beta. We can do this using
insert_lines(reg,
lines = c(
"alpha ~ std_normal();",
"beta ~ std_normal();"
),
at_end_of = "model"
)
## data {
## int<lower=0> N;
## vector[N] x;
## vector[N] y;
## }
## parameters {
## real alpha;
## real beta;
## real<lower=0> sigma;
## }
## model {
## y ~ normal(alpha + beta * x, sigma);
## alpha ~ std_normal();
## beta ~ std_normal();
## }
All contributions to this project are gratefully acknowledged using the
allcontributors
package
following the all-contributors
specification. Contributions of any kind are welcome!