Skip to content
Snippets Groups Projects
Commit 2ef530c5 authored by dj44vuri's avatar dj44vuri
Browse files

made parallel now an option in simall and simchoice

parent e9a21390
No related branches found
No related tags found
No related merge requests found
......@@ -38,11 +38,12 @@ sim_all <- function(nosim = 2,
estimate = TRUE,
chunks = 1,
utility_transform_type = "simple",
reshape_type = "auto") {
reshape_type = "auto",
mode= c("parallel", "sequential")) {
#################################################
########## Input Validation Test ###############
#################################################
mode <- match.arg(mode)
########### validate the utility function ########
if (missing(u) || !(is.list(u) && any(sapply(u, is.list)))) {
stop(
......@@ -142,7 +143,7 @@ sim_all <- function(nosim = 2,
designname <- stringr::str_remove_all(list.files(designpath, full.names = F), "(.ngd|_|.RDS)") ## Make sure designnames to not contain file ending and "_", as the may cause issues when replace
tictoc::tic()
tictoc::tic("total time for simulation and estimation")
all_designs <- purrr::map(
designfile,
......@@ -157,7 +158,8 @@ sim_all <- function(nosim = 2,
manipulations = manipulations,
estimate = estimate,
chunks = chunks,
utility_transform_type = utility_transform_type
utility_transform_type = utility_transform_type,
mode = mode
) %>% ## iterate simulation over all designs
stats::setNames(designname)
......
......@@ -8,14 +8,15 @@
#' @inheritParams simulate_choices
#' @param chunks The number of chunks determines how often results should be stored on disk as a safety measure to not loose simulations if models have already been estimated. For example, if no_sim is 100 and chunks = 2, the data will be saved on disk after 50 and after 100 runs.
#' @param utility_transform_type How the utility function you entered is transformed to the utility function required for mixl. You can use the classic way (simple) where parameters have to start with "b" and variables with "alt" or the more flexible (but potentially error prone) way (exact) where parameters and variables are matched exactly what how the are called in the dataset and in the bcoeff list. Default is "simple". In the long run, simple will be deleted, as exact should be downwards compatible.
#' @param mode Set to "parallel" if parts should be run in parallel mode
#' @return a list with all information on the run
#' @export
#'
#' @examples \dontrun{ simchoice(designfile="somefile", no_sim=10, respondents=330,
#' mnl_U,u=u[[1]] ,designtype="ngene")}
#'
sim_choice <- function(designfile, no_sim = 10, respondents = 330, u ,designtype = NULL, destype = NULL, bcoeff, decisiongroups=c(0,1), manipulations = list() , estimate, chunks=1, utility_transform_type = "simple" ,mode = "parallel") {
sim_choice <- function(designfile, no_sim = 10, respondents = 330, u ,designtype = NULL, destype = NULL, bcoeff, decisiongroups=c(0,1), manipulations = list() , estimate, chunks=1, utility_transform_type = "simple" ,mode = c("parallel", "sequential")) {
mode <- match.arg(mode)
#################################################
########## Input Validation Test ###############
......@@ -128,8 +129,8 @@ designs_all <- list()
as.data.frame()
switchmap <- function(.x, .f, mode = c("parallel", "sequential"), workers = NULL, ..., .progress = TRUE) {
mode <- match.arg(mode) # Ensure valid mode input
switchmap <- function(.x, .f, mode , workers = NULL, ..., .progress = TRUE) {
switch(
mode,
......@@ -151,7 +152,7 @@ designs_all <- list()
}
sim_data<- 1:no_sim %>% switchmap(~ simulate_choices(datadet, utility = u, setspp=setpp, bcoeff = bcoeff, decisiongroups = decisiongroups, manipulations = manipulations), mode = "parallel")
sim_data<- 1:no_sim %>% switchmap(~ simulate_choices(datadet, utility = u, setspp=setpp, bcoeff = bcoeff, decisiongroups = decisiongroups, manipulations = manipulations), mode = mode)
### start estimation
......@@ -306,7 +307,7 @@ tictoc::tic("start_estimation")
}
tictoc::toc()
coefs<-purrr::map(1:length(output),~summary(output[[.]])[["coefTable"]][c(1,8)] %>%
tibble::rownames_to_column() %>%
......
......@@ -18,7 +18,8 @@ sim_all(
estimate = TRUE,
chunks = 1,
utility_transform_type = "simple",
reshape_type = "auto"
reshape_type = "auto",
mode = c("parallel", "sequential")
)
}
\arguments{
......@@ -47,6 +48,8 @@ sim_all(
\item{utility_transform_type}{How the utility function you entered is transformed to the utility function required for mixl. You can use the classic way (simple) where parameters have to start with "b" and variables with "alt" or the more flexible (but potentially error prone) way (exact) where parameters and variables are matched exactly what how the are called in the dataset and in the bcoeff list. Default is "simple". In the long run, simple will be deleted, as exact should be downwards compatible.}
\item{reshape_type}{Must be "auto", "stats" to use the reshape from the stats package or tidyr to use pivot longer. Default is auto and should not bother you. Only change it once you face an error at this position and you may be lucky that it works then.}
\item{mode}{Set to "parallel" if parts should be run in parallel mode}
}
\value{
A list, with all information on the simulation. This list an be easily processed by the user and in the rmarkdown template.
......
......@@ -17,7 +17,7 @@ sim_choice(
estimate,
chunks = 1,
utility_transform_type = "simple",
mode = "parallel"
mode = c("parallel", "sequential")
)
}
\arguments{
......@@ -44,6 +44,8 @@ sim_choice(
\item{chunks}{The number of chunks determines how often results should be stored on disk as a safety measure to not loose simulations if models have already been estimated. For example, if no_sim is 100 and chunks = 2, the data will be saved on disk after 50 and after 100 runs.}
\item{utility_transform_type}{How the utility function you entered is transformed to the utility function required for mixl. You can use the classic way (simple) where parameters have to start with "b" and variables with "alt" or the more flexible (but potentially error prone) way (exact) where parameters and variables are matched exactly what how the are called in the dataset and in the bcoeff list. Default is "simple". In the long run, simple will be deleted, as exact should be downwards compatible.}
\item{mode}{Set to "parallel" if parts should be run in parallel mode}
}
\value{
a list with all information on the run
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment