Title: | Facilitate editing of stan models |
---|---|
Description: | Provides functionality for extracting particular bits of stan models as well as removing and adding lines. |
Authors: | Sebastian Funk [aut, cre] |
Maintainer: | Sebastian Funk <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.0 |
Built: | 2024-12-14 04:05:18 UTC |
Source: | https://github.com/epiforecasts/stanedit |
Add a block to a stan model. If that block exists, it will be removed first.
add_block(x, name, lines)
add_block(x, name, lines)
x |
a |
name |
name of the block |
lines |
character vector, lines in the block |
a stanedit
object containing the new block
model_file_name <- system.file(package = "stanedit", "regression.stan") reg <- stanedit(filename = model_file_name) reg <- add_block( reg, "transformed parameters", c("real gamma;", "gamma = alpha * beta;") )
model_file_name <- system.file(package = "stanedit", "regression.stan") reg <- stanedit(filename = model_file_name) reg <- add_block( reg, "transformed parameters", c("real gamma;", "gamma = alpha * beta;") )
Ignores differences in the model name.
## S3 method for class 'stanedit' e1 == e2, ...
## S3 method for class 'stanedit' e1 == e2, ...
e1 |
a |
e2 |
a |
... |
ignored |
TRUE or FALSE, depending on whether the models are equal or not
model_file_name <- system.file(package = "rbi", "PZ.bi") PZ <- stanedit(filename = model_file_name) PZ == PZ # TRUE
model_file_name <- system.file(package = "rbi", "PZ.bi") PZ <- stanedit(filename = model_file_name) PZ == PZ # TRUE
Extracts a subset of lines from the model and assigns new character strings.
## S3 replacement method for class 'stanedit' x[i, ...] <- value
## S3 replacement method for class 'stanedit' x[i, ...] <- value
x |
A stanedit |
i |
A vector of line numbers |
... |
ignored |
value |
A vector of the same length as |
the updated stanedit
object
model_file_name <- system.file(package = "rbi", "PZ.bi") PZ <- stanedit(filename = model_file_name) PZ[3:4] <- c("const e = 0.4", "const m_l = 0.05")
model_file_name <- system.file(package = "rbi", "PZ.bi") PZ <- stanedit(filename = model_file_name) PZ[3:4] <- c("const e = 0.4", "const m_l = 0.05")
Extracts a subset of lines from the model.
## S3 method for class 'stanedit' x[i, ...]
## S3 method for class 'stanedit' x[i, ...]
x |
A stanedit |
i |
A vector of line numbers |
... |
ignored |
a character string of the extracted model lines(s)
model_file_name <- system.file(package = "rbi", "PZ.bi") PZ <- stanedit(filename = model_file_name) PZ[3:4]
model_file_name <- system.file(package = "rbi", "PZ.bi") PZ <- stanedit(filename = model_file_name) PZ[3:4]
Returns the contents of a block in a stan model as a character vector of lines.
get_block(x, name, shell = FALSE)
get_block(x, name, shell = FALSE)
x |
a |
name |
name of the block |
shell |
if TRUE (default:FALSE), will return the shell (i.e., the
definition of the block) as well as content; this is useful, e.g., to see
options passed to a |
a character vector of the lines in the block, or an empty character vector if the block is not found
model_file_name <- system.file(package = "stanedit", "regression.stan") reg <- stanedit(filename = model_file_name) get_block(reg, "parameters")
model_file_name <- system.file(package = "stanedit", "regression.stan") reg <- stanedit(filename = model_file_name) get_block(reg, "parameters")
Returns the contents of a block in a stan model as a character vector of lines.
get_declaration(x, name)
get_declaration(x, name)
x |
a |
name |
name of the variable |
a character vector of length 1 with the line where the variable is declared, or an empty character vector if no declaration is found
model_file_name <- system.file(package = "stanedit", "regression.stan") reg <- stanedit(filename = model_file_name) get_declaration(reg, "alpha")
model_file_name <- system.file(package = "stanedit", "regression.stan") reg <- stanedit(filename = model_file_name) get_declaration(reg, "alpha")
Extract the variables declared in a stan model
get_vars(x, block)
get_vars(x, block)
x |
a |
block |
character (optional), the block or blocks in which to search; by default the whole model will be searched |
a character vector of variable names
model_file_name <- system.file(package = "stanedit", "regression.stan") reg <- stanedit(filename = model_file_name) get_vars(reg, "parameters")
model_file_name <- system.file(package = "stanedit", "regression.stan") reg <- stanedit(filename = model_file_name) get_vars(reg, "parameters")
Inserts one or more lines into a libbi model. If one of before
or
after
is given, the line(s) will be inserted before or after a given
line number or block name, respectively. If one of at_beginning of
or at_end_of
is given, the lines will be inserted at the
beginning/end of the block, respectively.
insert_lines(x, lines, before, after, at_beginning_of, at_end_of)
insert_lines(x, lines, before, after, at_beginning_of, at_end_of)
x |
a |
lines |
vector or line(s) |
before |
line number before which to insert line(s) |
after |
line number after which to insert line(s) |
at_beginning_of |
block at the beginning of which to insert lines(s) |
at_end_of |
block at the end of which to insert lines(s) |
the updated stanedit
object
model_file_name <- system.file(package = "stanedit", "regression.stan") reg <- stanedit(filename = model_file_name) insert_lines(reg, lines = "alpha ~ std_normal();", after = 12)
model_file_name <- system.file(package = "stanedit", "regression.stan") reg <- stanedit(filename = model_file_name) insert_lines(reg, lines = "alpha ~ std_normal();", after = 12)
Move stan code elsewere
move(x, lines, ...)
move(x, lines, ...)
x |
a |
lines |
vector of line numbers to move |
... |
arguments passed to |
a character vector of variable names
model_file_name <- system.file(package = "stanedit", "regression.stan") reg <- stanedit(filename = model_file_name) move(reg, find_declaration(reg, "alpha"), at_end_of = "data")
model_file_name <- system.file(package = "stanedit", "regression.stan") reg <- stanedit(filename = model_file_name) move(reg, find_declaration(reg, "alpha"), at_end_of = "data")
Removes one or more lines in a libbi model.
remove_lines(x, what)
remove_lines(x, what)
x |
a |
what |
either a vector of line number(s) to remove, or a vector of blocks to remove (e.g., "parameters") |
the updated stanedit
object
model_file_name <- system.file(package = "stanedit", "regression.stan") reg <- stanedit(filename = model_file_name) reg <- remove_lines(reg, "model")
model_file_name <- system.file(package = "stanedit", "regression.stan") reg <- stanedit(filename = model_file_name) reg <- remove_lines(reg, "model")
stanedit
prepares stan model code for programmatically editing.
stanedit(object, filename)
stanedit(object, filename)
object |
either a |
filename |
name of a file containing model code, if |
A {stanedit}
object containing clean model code. This will
have some formatting applied to it for further processing so line numbers
may not correspond to the original model, and comments will be removed.
model_file_name <- system.file(package = "stanedit", "regression.stan") reg <- stanedit(filename = model_file_name) reg
model_file_name <- system.file(package = "stanedit", "regression.stan") reg <- stanedit(filename = model_file_name) reg
Ignores differences in the model name.
## S3 method for class 'stanedit' e1 != e2, ...
## S3 method for class 'stanedit' e1 != e2, ...
e1 |
a |
e2 |
a |
... |
ignored |
TRUE or FALSE, depending on whether the models are equal or not
model_file_name <- system.file(package = "rbi", "PZ.bi") PZ <- stanedit(filename = model_file_name) PZ != PZ # FALSE
model_file_name <- system.file(package = "rbi", "PZ.bi") PZ <- stanedit(filename = model_file_name) PZ != PZ # FALSE
Writes a bi model to a file given by filename
. The extension '.bi'
will be added if necessary.
write_model(x, filename)
write_model(x, filename)
x |
a |
filename |
name of the file to be written |
the return value of the writeLines
call.
model_file_name <- system.file(package = "stanedit", "regression.stan") reg <- stanedit(filename = model_file_name) new_file_name <- tempfile("reg", fileext = ".stan") write_model(reg, new_file_name)
model_file_name <- system.file(package = "stanedit", "regression.stan") reg <- stanedit(filename = model_file_name) new_file_name <- tempfile("reg", fileext = ".stan") write_model(reg, new_file_name)