From e9d70b08968e2177ffc19cbd3cf6bc73f44d6839 Mon Sep 17 00:00:00 2001 From: samuelsmock <smock.samuel@gmail.com> Date: Mon, 19 Feb 2024 09:32:03 -0600 Subject: [PATCH] manipulations argument added where necessary. two notes on namespace and unused imports persist --- DESCRIPTION | 7 ++++-- R/sim_all.R | 6 ++--- R/sim_choice.R | 7 +++--- R/simulate_choices.R | 5 ++-- man/sim_all.Rd | 4 ++- man/sim_choice.Rd | 5 +++- man/simulate_choices.Rd | 7 ++++-- tests/manual-tests/SE-Agri.R | 45 ++++++++++++++++++++++++++++++++++ tests/testthat/test-sim_all.R | 1 - vignettes/SE_Agri-vignette.Rmd | 2 +- 10 files changed, 73 insertions(+), 16 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 310da68..fd09377 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 f140b5e..9af5058 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 45296f8..d361701 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 cb40c14..9676bd3 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 38cf5e8..1cc6d88 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 02579d3..7e85f8a 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 6163827..cf76d0a 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 e69de29..af380da 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 a1ea894..8176a8c 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 0cd051c..f69d31c 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) -- GitLab