Skip to content

Instantly share code, notes, and snippets.

@goldingn
Last active May 17, 2018 06:32
Show Gist options
  • Save goldingn/5edeb15082ac6e1525351c48bc31ada3 to your computer and use it in GitHub Desktop.
Save goldingn/5edeb15082ac6e1525351c48bc31ada3 to your computer and use it in GitHub Desktop.
hack to parse a list of initial values for use in greta
to_free <- function (node, data) {
lower <- node$lower
upper <- node$upper
fun <- switch(node$constraint,
none = function (x) x,
high = function (x) log(x - lower),
low = function (x) log(upper - x),
both = function(x) qlogis((y - lower) / (upper - lower)))
fun(data)
}
parse_inits <- function (inits, model) {
# line up greta array names with tf names
unlist_tf <- greta::.internals$utils$samplers$unlist_tf
greta_names <- names(model$dag$parameters_example)
tf_names <- vapply(names(inits),
function(name) model$dag$tf_name(get(name)$node),
"")
order <- match(greta_names, tf_names)
# reorder the inits, and transform them to the free scale
inits <- inits[order]
inits <- lapply(inits, as.array)
nodes <- model$dag$node_list[match(tf_names, model$dag$node_tf_names)]
inits <- mapply(to_free, nodes, inits)
unlist_tf(inits)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment