diff --git a/R/sim_all.R b/R/sim_all.R index 6b071dcf70fe0048d5603b2893ff68602b5c6f5d..e38cf7c5512b433915b90ce5233286d0467f2705 100644 --- a/R/sim_all.R +++ b/R/sim_all.R @@ -2,13 +2,12 @@ #' update #' @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 +#' @inheritParams readdesign #' @param designpath The path to the folder where the designs are stored. For example "c:/myfancydec/Designs" #' @param u A list with utility functions. The list can incorporate as many decision rule groups as you want. However, each group must be in a list in this list. If you just use one group (the normal), this group still has to be in a list in the u list. As a convention name beta coefficients starting with a lower case "b" #' @param bcoeff List of initial coefficients for the utility function. List content/length can vary based on application, but should all begin with b and be the same as those entered in the utility functions -#' @param decisiongroups A vector showing how decision groups are numerically distributed -#' @param manipulations A variable to alter terms of the utility functions examples may be applying a factor or applying changes to terms selectively for different groups -#' +#' @inheritParams sim_choice +#' @inheritParams simulate_choices #' @return A list, with all information on the simulation. This list an be easily processed by the user and in the rmarkdown template. #' @export #' @@ -28,7 +27,7 @@ #' bheight2=0.25, #' bheight3=0.50) #' -sim_all <- function(nosim=2, resps, destype=NULL, designpath, u, bcoeff, decisiongroups = c(0,1), manipulations = list()){ +sim_all <- function(nosim=2, resps, destype=NULL, designpath, u, bcoeff, decisiongroups = c(0,1), manipulations = list(), estimate = TRUE){ ################################################# ########## Input Validation Test ############### diff --git a/R/sim_choice.R b/R/sim_choice.R index d36170164d9b1f87f98d1613f0725f8eb5c563e0..45cd91f9ed4b5555b65e9e6bcaa51b8341929f89 100644 --- a/R/sim_choice.R +++ b/R/sim_choice.R @@ -1,29 +1,19 @@ - - - - - - - - -#' Title +#' Simulate and estimate choices #' #' @param designfile path to a file containing a design. #' @param no_sim Number of runs i.e. how often do you want the simulation to be repeated #' @param respondents Number of respondents. How many respondents do you want to simulate in each run. #' @param ut The first element of the utility function list -#' @param destype Specify which type of design you use. Either ngene or spdesign -#' @param bcoefficients List of initial coefficients for the utility function. List content/length can vary based on application, but should all begin with b and be the same as those entered in the utility functions -#' @param decisiongroups A vector showing how decision groups are numerically distributed -#' @param manipulations A variable to alter terms of the utility functions examples may be applying a factor or applying changes to terms selectively for different groups -#' +#' @inheritParams readdesign +#' @param estimate If TRUE models will be estimated. If false only a dataset will be simulated. Default is true +#' @inheritParams simulate_choices #' @return a list with all information on the run #' @export #' #' @examples \dontrun{ simchoice(designfile="somefile", no_sim=10, respondents=330, #' mnl_U,ut=u[[1]] ,destype="ngene")} #' -sim_choice <- function(designfile, no_sim=10, respondents=330,ut ,destype=destype, bcoefficients, decisiongroups=c(0,1), manipulations = list()) { +sim_choice <- function(designfile, no_sim=10, respondents=330,ut ,destype=destype, bcoefficients, decisiongroups=c(0,1), manipulations = list() , estimate = TRUE) { @@ -103,6 +93,10 @@ designs_all <- list() database <- simulate_choices(data=datadet, utility = ut, setspp = setpp, bcoefficients = bcoefficients, decisiongroups = decisiongroups, manipulations = manipulations) +### start estimation + + if (estimate==TRUE) { + # specify model for mixl estimation @@ -145,6 +139,7 @@ designs_all <- list() return(output) +} else return(database) } diff --git a/R/simulate_choices.R b/R/simulate_choices.R index 9676bd378ab6c3d0fc043052ba7f0869d5d9ef20..3ccdaf29346cbbb82a38a4345a8afabf002bb6a1 100644 --- a/R/simulate_choices.R +++ b/R/simulate_choices.R @@ -3,12 +3,11 @@ #' @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 integer, the number of choice sets per person -#' @param destype Is it a design created with ngene or with spdesign. Ngene designs 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 +#' @inheritParams readdesign #' @param bcoefficients List of initial coefficients for the utility function. List content/length can vary based on application, but should all begin with b and be the same as those entered in the utility functions #' @param decisiongroups A vector showing how decision groups are numerically distributed #' @param manipulations A variable to alter terms of the utility functions examples may be applying a factor or applying changes to terms selectively for different groups -#' +#' @return a dataframe that includes simulated choices and a design #' @export #' #' @examples \dontrun{simulate_choices(datadet, ut,setspp)} @@ -25,10 +24,8 @@ simulate_choices <- function(data, utility, setspp, destype, bcoefficients, deci by_formula <- function(equation){ #used to take formulas as inputs in simulation utility function - # //! cur_data_all may get deprecated in favor of pick - dplyr::pick(dplyr::everything()) |> - #cur_data_all() |> - dplyr::transmute(!!formula.tools::lhs(equation) := !!formula.tools::rhs(equation) ) + dplyr::pick(dplyr::everything()) |> + dplyr::transmute(!!formula.tools::lhs(equation) := !!formula.tools::rhs(equation) ) } # Here one can add additional case-specific data @@ -50,7 +47,7 @@ simulate_choices <- function(data, utility, setspp, destype, bcoefficients, deci if(exists("final_set")) data = dplyr::left_join(data,final_set, by = "ID") - cat("\n decisiongroups exists: " ,exists("decisiongroups")) + cat("\n decisiongroups exists: " ,length(decisiongroups)>2) if(length(decisiongroups)>2) { ### create a new variable to classify decision groups. diff --git a/man/sim_all.Rd b/man/sim_all.Rd index 4fd1b2cd5e494685d7916a2b5e6c51ac55ee517a..6cca772af0cb8992e547a8a4929bea68298a9c42 100644 --- a/man/sim_all.Rd +++ b/man/sim_all.Rd @@ -13,7 +13,8 @@ sim_all( u, bcoeff, decisiongroups = c(0, 1), - manipulations = list() + manipulations = list(), + estimate = TRUE ) } \arguments{ @@ -21,8 +22,6 @@ sim_all( \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"} \item{u}{A list with utility functions. The list can incorporate as many decision rule groups as you want. However, each group must be in a list in this list. If you just use one group (the normal), this group still has to be in a list in the u list. As a convention name beta coefficients starting with a lower case "b"} @@ -32,6 +31,8 @@ sim_all( \item{decisiongroups}{A vector showing how decision groups are numerically distributed} \item{manipulations}{A variable to alter terms of the utility functions examples may be applying a factor or applying changes to terms selectively for different groups} + +\item{estimate}{If TRUE models will be estimated. If false only a dataset will be simulated. Default is true} } \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 7e85f8a14a39f2cb06777461d02e30dc608d7966..5dbfddcb825c74a0af917f58ccb0e03a94b884cc 100644 --- a/man/sim_choice.Rd +++ b/man/sim_choice.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/sim_choice.R \name{sim_choice} \alias{sim_choice} -\title{Title} +\title{Simulate and estimate choices} \usage{ sim_choice( designfile, @@ -12,7 +12,8 @@ sim_choice( destype = destype, bcoefficients, decisiongroups = c(0, 1), - manipulations = list() + manipulations = list(), + estimate = TRUE ) } \arguments{ @@ -24,19 +25,19 @@ sim_choice( \item{ut}{The first element of the utility function list} -\item{destype}{Specify which type of design you use. Either ngene or spdesign} - \item{bcoefficients}{List of initial coefficients for the utility function. List content/length can vary based on application, but should all begin with b and be the same as those entered in the utility functions} \item{decisiongroups}{A vector showing how decision groups are numerically distributed} \item{manipulations}{A variable to alter terms of the utility functions examples may be applying a factor or applying changes to terms selectively for different groups} + +\item{estimate}{If TRUE models will be estimated. If false only a dataset will be simulated. Default is true} } \value{ a list with all information on the run } \description{ -Title +Simulate and estimate choices } \examples{ \dontrun{ simchoice(designfile="somefile", no_sim=10, respondents=330, diff --git a/man/simulate_choices.Rd b/man/simulate_choices.Rd index cf76d0a035103dfc3d9e13acf002a9e9faef3bdc..db94ad023faaca4954ecd12acb75737db4a04bdf 100644 --- a/man/simulate_choices.Rd +++ b/man/simulate_choices.Rd @@ -21,8 +21,6 @@ simulate_choices( \item{setspp}{an integer, the number of choice sets per person} -\item{destype}{Is it a design created with ngene or with spdesign. Ngene designs should be stored as the standard .ngd output. spdesign should be the spdesign object design$design} - \item{bcoefficients}{List of initial coefficients for the utility function. List content/length can vary based on application, but should all begin with b and be the same as those entered in the utility functions} \item{decisiongroups}{A vector showing how decision groups are numerically distributed}