diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..a733281d39821eb2347610baaf7e69fceabde06a Binary files /dev/null and b/.DS_Store differ diff --git a/R/sim_choice.R b/R/sim_choice.R index 1972fe1f2be082d3cddd5426bbf123fcfd222095..d3612746a34adefcead307002cd05b0cbaa27d2e 100644 --- a/R/sim_choice.R +++ b/R/sim_choice.R @@ -85,16 +85,21 @@ designs_all <- list() replications <- respondents/nblocks + ##browser() datadet<- design %>% dplyr::arrange(Block,Choice.situation) %>% dplyr::slice(rep(dplyr::row_number(), replications)) %>% ## replicate design according to number of replications + dplyr::mutate(ID = rep(1:respondents, each=setpp)) %>% # create Respondent ID. dplyr::relocate(ID,`Choice.situation`) %>% as.data.frame() + database <- simulate_choices(data=datadet, utility = ut, setspp = setpp, bcoefficients = bcoefficients) + + # specify model for mixl estimation model_spec <- mixl::specify_model(mnl_U, database, disable_multicore=F) diff --git a/inst/.DS_Store b/inst/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..e24034ed332799b6b8eb75a5002f23e197cb11f6 Binary files /dev/null and b/inst/.DS_Store differ diff --git a/inst/extdata/.DS_Store b/inst/extdata/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..aaa07329a15fb49c5c8bcebff3575c2d821b2a25 Binary files /dev/null and b/inst/extdata/.DS_Store differ diff --git a/inst/extdata/feedadditives/.DS_Store b/inst/extdata/feedadditives/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 Binary files /dev/null and b/inst/extdata/feedadditives/.DS_Store differ diff --git a/tests/manual-tests/csa.R b/tests/manual-tests/csa.R index 0788975956ab4b42651fc41eaddec692726b70b2..7dc63961d6cc43eeda87f558cecf752242a9444a 100644 --- a/tests/manual-tests/csa.R +++ b/tests/manual-tests/csa.R @@ -2,7 +2,7 @@ rm(list=ls()) devtools::load_all() -#place your utility functions here + @@ -14,8 +14,9 @@ designpath<- system.file("extdata","CSA" ,package = "simulateDCE") notes <- "No Heuristics" resps =240 # number of respondents -nosim=1000 # number of simulations to run (about 500 is minimum) +nosim=2 # number of simulations to run (about 500 is minimum) +destype <- "spdesign" bcoeff<-list( bx1 = -0.1, @@ -23,10 +24,6 @@ bcoeff<-list( bx3 = -0.05, bx4 = -0.025) - -destype <- "spdesign" - - #place your utility functions here ul<- list(u1= list( v1 =V.1 ~ bx1 * alt1.x1 + bx2 * alt1.x2 + bx3 * alt1.x3 + bx4 * alt1.x4, diff --git a/tests/manual-tests/csa.RDS b/tests/manual-tests/csa.RDS new file mode 100644 index 0000000000000000000000000000000000000000..2a4e75affafc7970136eff5c2c3914e3766ed77f Binary files /dev/null and b/tests/manual-tests/csa.RDS differ diff --git a/tests/manual-tests/feedadditives.R b/tests/manual-tests/feedadditives.R index 6892e210ad649e14fda99ac0806a5ff5d2425bfc..3ea5e06bf56dfc28379bebe0bde2fe5816ac8290 100644 --- a/tests/manual-tests/feedadditives.R +++ b/tests/manual-tests/feedadditives.R @@ -9,22 +9,24 @@ notes <- "Three heuristics" resps =300 # number of respondents nosim=2 # number of simulations to run (about 500 is minimum) +destype = "ngene" #betacoefficients should not include "-" -basc = 0.2 -bcow = 0.3 -badv = 0.3 -bvet = 0.3 -bfar = 0.3 -bmet = 0.3 -bbon = 0.3 -bbon2 = 1.9 -basc2 =2 +bcoeff = list( + basc = 0.2, + bcow = 0.3, + badv = 0.3, + bvet = 0.3, + bfar = 0.3, + bmet = 0.3, + bbon = 0.3, + bbon2 = 1.9, + basc2 =2) decisiongroups=c(0,0.3,0.6,0.8,1) #place your utility functions here -u<- list(u1= list( +ul<- list(u1= list( v1 =V.1 ~ bcow*alt1.cow + badv * alt1.adv + bvet * alt1.vet + bfar * alt1.far + bmet*alt1.met + bbon * alt1.bon, v2 =V.2 ~ bcow*alt2.cow + badv * alt2.adv + bvet * alt2.vet + bfar * alt2.far + bmet*alt2.met + bbon * alt2.bon, v3 =V.3 ~ basc) @@ -41,7 +43,7 @@ u<- list(u1= list( u4 = list(v1 =V.1 ~ basc2 + bcow*alt1.cow + badv * alt1.adv + bvet * alt1.vet + bfar * alt1.far + bmet*alt1.met + bbon * alt1.bon, v2 =V.2 ~ bcow*alt2.cow + badv * alt2.adv + bvet * alt2.vet + bfar * alt2.far + bmet*alt2.met + bbon * alt2.bon, v3 =V.3 ~ basc) - - - ) + +sedrive <- sim_all(nosim = nosim, resps=resps, destype = destype, + designpath = designpath, u=ul, bcoeff = bcoeff) diff --git a/vignettes/SE_drive-vignette.Rmd b/vignettes/SE_drive-vignette.Rmd index 9a8717139b195390ab3ed28d695997978a026f15..2b4d4ef6ece09ac2665ad305860fd6f242f8c781 100644 --- a/vignettes/SE_drive-vignette.Rmd +++ b/vignettes/SE_drive-vignette.Rmd @@ -13,7 +13,7 @@ rather than inst/doc Best Practice is to follow these steps 1. Create vignette using usethis::use_vignette("my-vignette") -2. After making changes run devtools::build_vignettes() +2. After making changes, then run devtools::build_vignettes() 3. Rebuild using devtools::install(build_vignettes = TRUE) 4. Check that it is in the vignette environment using browseVigettes() @@ -86,7 +86,7 @@ ul<-list( u1 = ``` # Other parameters -Besides these arguments the user must also specify the number of respondents in there simulated survey and the number of times to run the model. The number of respondents (resps) should be selected based on experimental design parameters, while the number of simulations (nosim) should be large enough to glean statistically significant data. It is best to use a small number for this while learning to use the package and a large number (at least 500) once the other parameters have been settled. +Besides these arguments the user must also specify the number of respondents in the simulated survey and the number of times to run the model. The number of respondents (resps) should be selected based on experimental design parameters, while the number of simulations (nosim) should be large enough to glean statistically significant data. It is best to use a small number for this while learning to use the package and a large number (at least 500) once the other parameters have been settled. The simulation can be ran using spdesign or NGENE design files which will be contained in the design path. The design path and design type, must also be specified: diff --git a/vignettes/csa-vignette.Rmd b/vignettes/csa-vignette.Rmd new file mode 100644 index 0000000000000000000000000000000000000000..4162a3123061d12790fe7bfb4d741435b947b61f --- /dev/null +++ b/vignettes/csa-vignette.Rmd @@ -0,0 +1,130 @@ +--- +title: "csa-vignette" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{csa-vignette} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +--- + +<!-- There is an issue when creating these vignettes using usethis::use_vignette and +devtools::build_vignettes() where the compilied vignette HTML files are placed in /doc +rather than inst/doc + +Best Practice is to follow these steps +1. Create vignette using usethis::use_vignette("my-vignette") +2. After making changes, then run devtools::build_vignettes() +3. Rebuild using devtools::install(build_vignettes = TRUE) +4. Check that it is in the vignette environment using browseVigettes() + +If vignette does not appear in gitHub, it is possibly due to a file heirarchy problem where rendered files +appear in /doc instead of /inst/doc + +To avoid this run: +tools::buildVignettes(dir = ".", tangle=TRUE) +dir.create("inst/doc") +file.copy(dir("vignettes", full.names=TRUE), "inst/doc", overwrite=TRUE) + +More info here: https://community.rstudio.com/t/browsevignettes-mypackage-saying-no-vignettes-found/68656/7 +--> + +```{r, include = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>" +) +``` +# Introduction + +This vignette demonstrates how to use the `simulateDCE` package to simulate discrete choice experiments (DCEs). We will use a sample dataset and utility functions to generate simulated data and analyze the results. + +```{r setup} +library(simulateDCE) + +rm(list=ls()) +devtools::load_all() + + +library(rlang) +``` + +# Inititalize Variables + +sim_all is the highest level function in the package and will run simulations for all designs contained in the specified design folder. Accordingly, this is generally the function the user will want to call. To prepare for using this function, a hypothesized utility function with corresponding beta coefficients representing the weight of each term must be declared in R like so: + +```{r initialize} +bcoeff<-list( + bx1 = -0.1, + bx2 = -0.1, + bx3 = -0.05, + bx4 = -0.025) + +#place your utility functions here +ul<- list(u1= list( + v1 =V.1 ~ bx1 * alt1.x1 + bx2 * alt1.x2 + bx3 * alt1.x3 + bx4 * alt1.x4, + v2 =V.2 ~ bx1 * alt2.x1 + bx2 * alt2.x2 + bx3 * alt2.x3 + bx4 * alt2.x4, + v3 =V.3 ~ -5 +) +) +``` +# Other parameters + +Besides these arguments the user must also specify the number of respondents in the simulated survey and the number of times to run the model. The number of respondents (resps) should be selected based on experimental design parameters, while the number of simulations (nosim) should be large enough to glean statistically significant data. It is best to use a small number for this while learning to use the package and a large number (at least 500) once the other parameters have been settled. + +The simulation can be ran using spdesign or NGENE design files which will be contained in the design path. The design path and design type, must also be specified as strings: + +```{r other} + +designpath<- system.file("extdata","CSA" ,package = "simulateDCE") +## can also be specified using relative path eg. designpath<- "Projects/CSA/Designs/" + +#notes <- "This design consists of different heuristics. One group did not attend the methan attribute, another group only decided based on the payment" + +notes <- "No Heuristics" + +resps =240 # number of respondents +nosim=2 # number of simulations to run (about 500 is minimum) + +## design type must be either 'spdesign' or 'ngene' +destype <- "spdesign" +``` +# Randomness + +As several part of the simulation rely on random values within experimentally defined bounds, the output of a given simulation call using sim_all will vary each time it is called. Unless the seed for R's randome number generator is set like so: + +```{r random} + +set.seed(3393) + +``` + +# Output + +The sim_all function returns a multidimensional R list containing graphs, simulated observations and a dataframe containing sumaries of estimated b coefficients. In general these will be printed to the console, but the entire results can also be assigned to an r list object. + +```{r output} +csa <- simulateDCE::sim_all(nosim = nosim, resps=resps, destype = destype, + designpath = designpath, u= ul, bcoeff = bcoeff) + + + +``` + +# Accessing the Output in R + +The beta cofficients for each simulation are contained in a dataframe called coeffs located within {result}->olddesign->coefs. and a summary table, which displays +statistics of these b coefficients across all simulations is contained in ->olddesign->summary. + +You can also save the results to disk using saveRDS(csa,file = "tests/manual-tests/csa.RDS") + + +```{r accessOutput} +## nested results are hard coded, if the design changes this must aswell +simulationCoeff <- csa$olddesign$coefs +coeffSummary <- csa$olddesign$summary + +print(simulationCoeff) +print(coeffSummary) + +## saveRDS(csa,file = "tests/manual-tests/csa.RDS") +```