diff --git a/DESCRIPTION b/DESCRIPTION index 310da68825b7258f35eaa2a4b588de34703b7b8f..fd093775c2ddc81a77927ca2ccc6156092411f99 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,8 +2,9 @@ Package: simulateDCE Title: Simulate data for discrete choice experiments Version: 0.1.1 Authors@R: - person("Julian", "Sagebiel", , "julian.sagebiel@idiv.de", role = c("aut", "cre"), + person(given = "Julian", family = "Sagebiel", email = "julian.sagebiel@idiv.de", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-0253-6875")) + person(given = "Samuel", family = "Smock", email = "samuel.smock@idiv.de", role = c("aut", "cre")) 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 @@ -20,8 +21,10 @@ Imports: psych, purrr, readr, - rlang, rmarkdown, + stats, + rlang, + utils, stringr, tibble, tictoc, diff --git a/R/sim_all.R b/R/sim_all.R index f140b5e67d0c5f9b5cdc3b8667f0b8f9af614250..9af50588b180b383d7f815a7ab0c286ad7f82d9d 100644 --- a/R/sim_all.R +++ b/R/sim_all.R @@ -7,7 +7,7 @@ #' @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 change terms of the utility functions eg shift be a factor of ten or apply selectively to different groups +#' @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 list, with all information on the simulation. This list an be easily processed by the user and in the rmarkdown template. #' @export @@ -28,7 +28,7 @@ #' bheight2=0.25, #' bheight3=0.50) #' -sim_all <- function(nosim=2, resps, destype="ngene", designpath, u, bcoeff, decisiongroups = c(0,1)){ +sim_all <- function(nosim=2, resps, destype="ngene", designpath, u, bcoeff, decisiongroups = c(0,1), manipulations = list()){ ################################################# ########## Input Validation Test ############### @@ -112,7 +112,7 @@ sim_all <- function(nosim=2, resps, destype="ngene", designpath, u, bcoeff, deci tictoc::tic() all_designs<- purrr::map(designfile, sim_choice, - no_sim= nosim,respondents = resps, destype=destype, ut=u, bcoefficients = bcoeff, decisiongroups = decisiongroups) %>% ## iterate simulation over all designs + no_sim= nosim,respondents = resps, destype=destype, ut=u, bcoefficients = bcoeff, decisiongroups = decisiongroups, manipulations = manipulations) %>% ## iterate simulation over all designs stats::setNames(designname) diff --git a/R/sim_choice.R b/R/sim_choice.R index 45296f88e31fb1266c2c31414c7eb412e2e43a99..d36170164d9b1f87f98d1613f0725f8eb5c563e0 100644 --- a/R/sim_choice.R +++ b/R/sim_choice.R @@ -15,6 +15,7 @@ #' @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 #' #' @return a list with all information on the run #' @export @@ -22,7 +23,7 @@ #' @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)) { +sim_choice <- function(designfile, no_sim=10, respondents=330,ut ,destype=destype, bcoefficients, decisiongroups=c(0,1), manipulations = list()) { @@ -40,7 +41,7 @@ sim_choice <- function(designfile, no_sim=10, respondents=330,ut ,destype=destyp cat("This is Run number ", run) - database <- simulate_choices(datadet, utility = ut, setspp=setpp, bcoefficients = bcoefficients, decisiongroups = decisiongroups) + database <- simulate_choices(datadet, utility = ut, setspp=setpp, bcoefficients = bcoefficients, decisiongroups = decisiongroups, manipulations = manipulations) cat("This is the utility functions \n" , mnl_U) @@ -99,7 +100,7 @@ designs_all <- list() as.data.frame() - database <- simulate_choices(data=datadet, utility = ut, setspp = setpp, bcoefficients = bcoefficients, decisiongroups = decisiongroups) + database <- simulate_choices(data=datadet, utility = ut, setspp = setpp, bcoefficients = bcoefficients, decisiongroups = decisiongroups, manipulations = manipulations) diff --git a/R/simulate_choices.R b/R/simulate_choices.R index cb40c149868a0027643d9347b312f93dad04e1a3..9676bd378ab6c3d0fc043052ba7f0869d5d9ef20 100644 --- a/R/simulate_choices.R +++ b/R/simulate_choices.R @@ -3,15 +3,16 @@ #' @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 desings should be stored as the standard .ngd output. spdesign should be the spdesign object design$design +#' @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 #' @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 #' #' @export #' #' @examples \dontrun{simulate_choices(datadet, ut,setspp)} -simulate_choices <- function(data, utility, setspp, destype, bcoefficients, decisiongroups = c(0,1)) { #the part in dataset that needs to be repeated in each run +simulate_choices <- function(data, utility, setspp, destype, bcoefficients, decisiongroups = c(0,1), manipulations = list()) { #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 38cf5e813c9f4ee6ab6da8cef52805d8a2741a62..1cc6d888b341a97e338a3a515fa82b8a90a2b21e 100644 --- a/man/sim_all.Rd +++ b/man/sim_all.Rd @@ -13,7 +13,7 @@ sim_all( u, bcoeff, decisiongroups = c(0, 1), - manipulations + manipulations = list() ) } \arguments{ @@ -30,6 +30,8 @@ sim_all( \item{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} \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} } \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 02579d3f35ae374c0b37b7c48c0b421f7ae5bbae..7e85f8a14a39f2cb06777461d02e30dc608d7966 100644 --- a/man/sim_choice.Rd +++ b/man/sim_choice.Rd @@ -11,7 +11,8 @@ sim_choice( ut, destype = destype, bcoefficients, - decisiongroups = c(0, 1) + decisiongroups = c(0, 1), + manipulations = list() ) } \arguments{ @@ -28,6 +29,8 @@ sim_choice( \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} } \value{ a list with all information on the run diff --git a/man/simulate_choices.Rd b/man/simulate_choices.Rd index 6163827bb54cf372ee1826918ed1732650cd15f5..cf76d0a035103dfc3d9e13acf002a9e9faef3bdc 100644 --- a/man/simulate_choices.Rd +++ b/man/simulate_choices.Rd @@ -10,7 +10,8 @@ simulate_choices( setspp, destype, bcoefficients, - decisiongroups = c(0, 1) + decisiongroups = c(0, 1), + manipulations = list() ) } \arguments{ @@ -20,11 +21,13 @@ 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 desings should be stored as the standard .ngd output. spdesign should be the spdesign object design$design} +\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} + +\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} } \value{ a dataframe that includes simulated choices and a design diff --git a/tests/manual-tests/SE-Agri.R b/tests/manual-tests/SE-Agri.R index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..af380da733d3646ba6f19c955348ea919fc735aa 100644 --- a/tests/manual-tests/SE-Agri.R +++ b/tests/manual-tests/SE-Agri.R @@ -0,0 +1,45 @@ +rm(list=ls()) +devtools::load_all() + + +library(rlang) + +designpath<- system.file("extdata","SE_AGRI", package = "simulateDCE") + +destype = 'ngene' +resps =360 # number of respondents +nosim=2 # number of simulations to run (about 500 is minimum) + +#betacoefficients should not include "-" +bcoeff <- list( + basc = 4.2, ## very high asc + bprof = 0.3, + bexp = 0.3, + bdomestic = 0.3, + bforeign = 0.3, + bdamage = 0.6, + bprice = 0.2) + + + +manipulations = list(alt1.professional= expr(alt1.initiator==1), + alt2.professional= expr(alt2.initiator==1), + alt1.expert = expr(alt1.initiator==2), + alt2.expert = expr(alt2.initiator==2), + alt1.domestic = expr(alt1.funding==1), + alt2.domestic = expr(alt2.funding==1), + alt1.foreign = expr(alt1.funding==2), + alt2.foreign = expr(alt2.funding==2)) + + +#place your utility functions here +ul<- list(u1= + list( + v1 =V.1 ~ bprof*alt1.professional+ bexp * alt1.expert + bdomestic * alt1.domestic + bforeign * alt1.foreign + bdamage*alt1.damage + bprice * alt1.compensation, + v2 =V.2 ~ bprof*alt2.professional + bexp * alt2.expert + bdomestic * alt2.domestic + bforeign * alt2.foreign + bdamage*alt2.damage + bprice * alt2.compensation, + v3 =V.3 ~ basc) +) + +seagri <- sim_all(nosim = nosim, resps=resps, destype = destype, + designpath = designpath, u=ul, bcoeff = bcoeff, manipulations = manipulations) + diff --git a/tests/testthat/test-sim_all.R b/tests/testthat/test-sim_all.R index a1ea894b8b7dc8aa6c3e5618f80661bc34db416a..8176a8ce9bd2a206ea057f32a382abbb4e890163 100644 --- a/tests/testthat/test-sim_all.R +++ b/tests/testthat/test-sim_all.R @@ -1,4 +1,3 @@ - library(rlang) library(formula.tools) diff --git a/vignettes/SE_Agri-vignette.Rmd b/vignettes/SE_Agri-vignette.Rmd index 0cd051c7f1dbf228d80d39d8ca95ecc6da26454e..f69d31c9b6bfeba0d79699df1f1153a8c4a4225a 100644 --- a/vignettes/SE_Agri-vignette.Rmd +++ b/vignettes/SE_Agri-vignette.Rmd @@ -118,7 +118,7 @@ The sim_all function returns a multidimensional R list containing graphs, simula ```{r output} seagri <- simulateDCE::sim_all(nosim = nosim, resps=resps, destype = destype, - designpath = designpath, u= ul, bcoeff = bcoeff) + designpath = designpath, u= ul, bcoeff = bcoeff, manipulations = manipulations)