---
title: "csa-vignette"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{csa-vignette}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{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)
library(rlang)
library(formula.tools)
```
# 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", "linear", 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, designtype = 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 within within a nested list structure output. A summary table showing the beta coefficient statistics is also made within each experimental design.
You can also save the results to disk using saveRDS(csa,file = "tests/manual-tests/csa.RDS")
```{r accessOutput}
topLevelResults <- names(csa[sapply(csa, is.list)])
print(topLevelResults)
## saves and prints the key results of the first expreimental design
simulationCoeff <- csa[[1]]$coefs
coeffSummary <- csa[[1]]$summary
print(simulationCoeff)
print(coeffSummary)
## saveRDS(csa,file = "tests/manual-tests/csa.RDS")
```