From ad02fc3c600c7c89d4a9fd7299f516bab5632df5 Mon Sep 17 00:00:00 2001 From: Julian Sagebiel <julian.sagebiel@idiv.de> Date: Wed, 26 Jun 2024 21:42:55 +0200 Subject: [PATCH] added function estimate = TRUE to allow for generating data only and not estimating a model and improved roxigen skeleton --- R/sim_all.R | 9 ++++----- R/sim_choice.R | 25 ++++++++++--------------- R/simulate_choices.R | 13 +++++-------- man/sim_all.Rd | 7 ++++--- man/sim_choice.Rd | 11 ++++++----- man/simulate_choices.Rd | 2 -- 6 files changed, 29 insertions(+), 38 deletions(-) diff --git a/R/sim_all.R b/R/sim_all.R index 6b071dc..e38cf7c 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 d361701..45cd91f 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 9676bd3..3ccdaf2 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 4fd1b2c..6cca772 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 7e85f8a..5dbfddc 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 cf76d0a..db94ad0 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} -- GitLab