From 11286e167f7cb48edf748daf7a4481e9c0689e70 Mon Sep 17 00:00:00 2001 From: dj44vuri <julian.sagebiel@idiv.de> Date: Sun, 31 Dec 2023 01:33:03 +0100 Subject: [PATCH] added some arguments to sim_all function --- DESCRIPTION | 2 +- R/sim_all.R | 10 +++++++--- R/sim_choice.R | 21 ++++++++++++++++----- R/simulate_choices.R | 6 +++--- man/sim_all.Rd | 11 ++++++++++- man/sim_choice.Rd | 2 +- man/simulate_choices.Rd | 6 ++++-- tests/manual-tests/Rbookfull.R | 3 ++- 8 files changed, 44 insertions(+), 17 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index ffab5ef..b5224fe 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -3,7 +3,7 @@ Title: Simulate data for discrete choice experiments Version: 0.0.1.0000 Authors@R: person("Julian", "Sagebiel", , "julian.sagebiel@idiv.de", role = c("aut", "cre"), - comment = c(ORCID = "YOUR-ORCID-ID")) + comment = c(ORCID = "0000-0002-0253-6875")) Description: A package that supports simulating choice experiment data for given designs. It helps to quickly test different designs against each other. License: MIT + file LICENSE Encoding: UTF-8 diff --git a/R/sim_all.R b/R/sim_all.R index 39e90c4..8be4da4 100644 --- a/R/sim_all.R +++ b/R/sim_all.R @@ -1,5 +1,10 @@ #' Title Is a wrapper for sim_choice executing the simulation over all designs stored in a specific folder #' +#' @param nosim Number of runs or simulations. For testing use 2 but once you go serious, use at least 200, for better results use 2000. +#' @param resps Number of respondents you want to simulate +#' @param destype Is it a design created with ngene or with spdesign. Ngene desings should be stored as the standard .ngd output. spdesign should be the spdesign object design$design +#' @param designpath The path to the folder where the designs are stored. For example "c:/myfancydec/Designs" +#' #' @return a list, with all information on the simulation. This list an be easily processed by the user and in the rmarkdown template. #' @export #' @@ -9,15 +14,14 @@ #' resps =240 # number of respondents #' nosim=2 # number of simulations to run (about 500 is minimum) #' -sim_all <- function(){ - +sim_all <- function(nosim=2, resps, destype="ngene", designpath){ +#browser() designfile<-list.files(designpath,full.names = T) 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 - if (!exists("destype")) destype="ngene" tictoc::tic() diff --git a/R/sim_choice.R b/R/sim_choice.R index 4cc3dc0..5c03424 100644 --- a/R/sim_choice.R +++ b/R/sim_choice.R @@ -21,17 +21,19 @@ #' @examples \dontrun{ simchoice(designfile="somefile", no_sim=10, respondents=330, #' mnl_U,utils=u[[1]] ,destype="ngene")} #' -sim_choice <- function(designfile, no_sim=10, respondents=330, mnl_U,utils=u[[1]] ,destype) { +sim_choice <- function(designfile, no_sim=10, respondents=330, mnl_U,utils=u[[1]] ,destype=destype) { -## Function that transforms user written utiliy for simulation into utility function for mixl. +#### Function that transforms user written utility for simulation into utility function for mixl. transform_util <- function() { mnl_U <-paste(purrr::map_chr(utils,as.character,keep.source.attr = TRUE),collapse = "",";") %>% stringr::str_replace_all( c( "priors\\[\"" = "" , "\"\\]" = "" , "~" = "=", "\\." = "_" , " b" = " @b" , "V_"="U_", " alt"="$alt")) } +#### Function to simulate and estimate + estimate_sim <- function(run=1) { #start loop cat("This is Run number ", run) @@ -49,8 +51,13 @@ sim_choice <- function(designfile, no_sim=10, respondents=330, mnl_U,utils=u[[1] +# transform utility function to mixl format mnl_U <- transform_util() +# Empty list where to store all designs later on +designs_all <- list() + +#### Print some messages #### cat("Utility function used in simulation, ie the true utility: \n\n") @@ -62,16 +69,20 @@ mnl_U <- transform_util() - designs_all <- list() ## Empty list where to store all designs later on - design<- readdesign(design = designfile) # Read in the design file + +#### Read in the design file and set core variables #### + + + + design<- readdesign(design = designfile, designtype = destype) if (!("Block" %in% colnames(design))) design$Block=1 # If no Blocks exist, create a variable Blocks to indicate it is only one block nsets<-nrow(design) nblocks<-max(design$Block) - setpp <- nsets/nblocks # Choice Sets per respondent; in this 'no blocks' design everyone sees all 24 sets + setpp <- nsets/nblocks # Choice Sets per respondent replications <- respondents/nblocks diff --git a/R/simulate_choices.R b/R/simulate_choices.R index 5ec9b25..3796950 100644 --- a/R/simulate_choices.R +++ b/R/simulate_choices.R @@ -2,13 +2,13 @@ #' #' @param data a dataframe that includes a design repeated for the number of observations #' @param utility a list with the utility functions, one utility function for each alternatives -#' @param setspp an integeger, the number of choice sets per person -#' +#' @param setspp an integer, the number of choice sets per person +#' @param destype Is it a design created with ngene or with spdesign. Ngene desings should be stored as the standard .ngd output. spdesign should be the spdesign object design$design #' @return a dataframe that includes simulated choices and a design #' @export #' #' @examples \dontrun{simulate_choices(datadet, utils,setspp)} -simulate_choices <- function(data=datadet, utility =utils, setspp) { #the part in dataset that needs to be repeated in each run +simulate_choices <- function(data=datadet, utility =utils, setspp, destype) { #the part in dataset that needs to be repeated in each run diff --git a/man/sim_all.Rd b/man/sim_all.Rd index 7a11efc..25ff117 100644 --- a/man/sim_all.Rd +++ b/man/sim_all.Rd @@ -4,7 +4,16 @@ \alias{sim_all} \title{Title Is a wrapper for sim_choice executing the simulation over all designs stored in a specific folder} \usage{ -sim_all() +sim_all(nosim = 2, resps, destype = "ngene", designpath) +} +\arguments{ +\item{nosim}{Number of runs or simulations. For testing use 2 but once you go serious, use at least 200, for better results use 2000.} + +\item{resps}{Number of respondents you want to simulate} + +\item{destype}{Is it a design created with ngene or with spdesign. Ngene desings should be stored as the standard .ngd output. spdesign should be the spdesign object design$design} + +\item{designpath}{The path to the folder where the designs are stored. For example "c:/myfancydec/Designs"} } \value{ a list, with all information on the simulation. This list an be easily processed by the user and in the rmarkdown template. diff --git a/man/sim_choice.Rd b/man/sim_choice.Rd index 7ec225d..a035a37 100644 --- a/man/sim_choice.Rd +++ b/man/sim_choice.Rd @@ -10,7 +10,7 @@ sim_choice( respondents = 330, mnl_U, utils = u[[1]], - destype + destype = destype ) } \arguments{ diff --git a/man/simulate_choices.Rd b/man/simulate_choices.Rd index 80d6ecc..f10c00a 100644 --- a/man/simulate_choices.Rd +++ b/man/simulate_choices.Rd @@ -4,14 +4,16 @@ \alias{simulate_choices} \title{Simulate choices based on a dataframe with a design} \usage{ -simulate_choices(data = datadet, utility = utils, setspp) +simulate_choices(data = datadet, utility = utils, setspp, destype) } \arguments{ \item{data}{a dataframe that includes a design repeated for the number of observations} \item{utility}{a list with the utility functions, one utility function for each alternatives} -\item{setspp}{an integeger, the number of choice sets per person} +\item{setspp}{an integer, the number of choice sets per person} + +\item{destype}{Is it a design created with ngene or with spdesign. Ngene desings should be stored as the standard .ngd output. spdesign should be the spdesign object design$design} } \value{ a dataframe that includes simulated choices and a design diff --git a/tests/manual-tests/Rbookfull.R b/tests/manual-tests/Rbookfull.R index f92597d..214f963 100644 --- a/tests/manual-tests/Rbookfull.R +++ b/tests/manual-tests/Rbookfull.R @@ -38,7 +38,8 @@ u<- list(u1= list( -rbook <- sim_all() +rbook <- sim_all(nosim = nosim, resps=resps, destype = destype, + designpath = designpath) -- GitLab