Skip to content

Commit

Permalink
New way of handling data and changing stan data list
Browse files Browse the repository at this point in the history
  • Loading branch information
fontikar committed Oct 6, 2023
1 parent 8cbe705 commit 2593575
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
5 changes: 2 additions & 3 deletions R/rmot_lm.R → R/rmot.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
#' @examples
#' mtcars
#' rmot_lm(mtcars$mpg, mtcars$disp)
rmot_lm <- function(x, y, ...) {
standata <- list(x = x, y = y, N = length(y))
out <- rstan::sampling(stanmodels$lm, data = standata, ...)
rmot <- function(x, y, ...) {
out <- rstan::sampling(stanmodels$model, data = standata, ...)
return(out)
}
61 changes: 61 additions & 0 deletions R/rmot_models.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Set list structures for different models
# An example for lm

rmot_lm <- function(){
list(X = NULL,
Y = NULL,
N = NULL,
model = "linear")
}


# Need a mechanism to select models
# rmot_config(model = "linear")

rmot_config <- function(model=NULL){
output <- switch(model,
linear = rmot_lm())

class(output) <- "rmot_object"

return(output)
}

# Need a mechanism to take user data and assign to slots in list
rmot_assign_data <- function(model_template, field, data){
purrr::assign_in(model_template, field, data)
}


rmot_assign_data <- function(model_template, ...){
# Grab user expressions
user_code <- rlang::enexprs(..., .check_assign = TRUE)

# Evaluate the RHS of expressions (the values)
data <- purrr::map(user_code,
eval)

# Grab the names
fields <- names(user_code)

for(i in fields){
model_template <- purrr::list_modify(model_template, !!!data[i])
}

return(model_template)
}




list_rename = function(data, ...) {
mapping = sapply(
rlang::enquos(...),
rlang::as_name
)
new_names = stats::setNames(nm=names(data))
# `new_name = old_name` for consistency with `dplyr::rename`
new_names[mapping] = names(mapping)
# for `old_name = new_name` use: `new_names[names(mapping)] = mapping`
stats::setNames(data, new_names)
}

0 comments on commit 2593575

Please sign in to comment.