From 2593575ea6128c2fcdb91183c2313b95a94788be Mon Sep 17 00:00:00 2001 From: Fonti Kar Date: Fri, 6 Oct 2023 16:41:58 +1100 Subject: [PATCH] New way of handling data and changing stan data list --- R/{rmot_lm.R => rmot.R} | 5 ++-- R/rmot_models.R | 61 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) rename R/{rmot_lm.R => rmot.R} (70%) create mode 100644 R/rmot_models.R diff --git a/R/rmot_lm.R b/R/rmot.R similarity index 70% rename from R/rmot_lm.R rename to R/rmot.R index 2d6b6b9..16892a1 100644 --- a/R/rmot_lm.R +++ b/R/rmot.R @@ -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) } diff --git a/R/rmot_models.R b/R/rmot_models.R new file mode 100644 index 0000000..aa94057 --- /dev/null +++ b/R/rmot_models.R @@ -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) +}