# SE_drive-vignette

# 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.

    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:

    # pass beta coefficients as a list
    bcoeff <- list(
      bpreis = -0.01,
      blade = -0.07,
      bwarte = 0.02
    )

    manipulations <- list(
      alt1.x2 = expr(alt1.x2 / 10),
      alt1.x3 = expr(alt1.x3 / 10),
      alt2.x2 = expr(alt2.x2 / 10),
      alt2.x3 = expr(alt2.x3 / 10)
    )

    # place your utility functions here
    ul <- list(
      u1 =

        list(
          v1 = V.1 ~ bpreis * alt1.x1 + blade * alt1.x2 + bwarte * alt1.x3,
          v2 = V.2 ~ bpreis * alt2.x1 + blade * alt2.x2 + bwarte * alt2.x3
        ),
      u2 = list(
        v1 = V.1 ~ bpreis * alt1.x1,
        v2 = V.2 ~ bpreis * alt2.x1
      )
    )

# Decision Groups

If the utility function set passed to sim all has more than 1 utility function group as is the case above (u1, u2 etc.) an additional variable, ‘decision groups’ must be given to tell the function what portion of respondents should be given each utility function.

    decisiongroups <- c(0, 0.7, 1)

# 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:

    designpath <- system.file("extdata", "SE_DRIVE", package = "simulateDCE")

    resps <- 120 # number of respondents
    nosim <- 2 # number of simulations to run (about 500 is minimum)

    destype <- "ngene"

# 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.

    sedrive <- sim_all(
      nosim = nosim, resps = resps, designtype = destype,
      designpath = designpath, u = ul, bcoeff = bcoeff, decisiongroups = decisiongroups
    )
    #> 'simple' is deprecated and will be removed in the future. Use 'exact' instead.
    #> bcoeff_lookup already exists; skipping modification.
    #>
    #> Utility function used in simulation (true utility):
    #> $u1
    #> $u1$v1
    #> V.1 ~ bpreis * alt1.x1 + blade * alt1.x2 + bwarte * alt1.x3
    #> <environment: 0x6553d510e0a8>
    #>
    #> $u1$v2
    #> V.2 ~ bpreis * alt2.x1 + blade * alt2.x2 + bwarte * alt2.x3
    #> <environment: 0x6553d588dd50>
    #>
    #>
    #> $u2
    #> $u2$v1
    #> V.1 ~ bpreis * alt1.x1
    #> <environment: 0x6553d57f7c10>
    #>
    #> $u2$v2
    #> V.2 ~ bpreis * alt2.x1
    #> <environment: 0x6553d5747c20>
    #> New names:
    #> No preprocess function provided. Proceeding without additional preprocessing.
    #>
    #> dataset preprossed_data exists: FALSE
    #> decisiongroups exists: TRUE
    #> Group counts:
    #>
    #> 1 2 1007 433
    #>
    #>
    #>
    #> data has been created
    #> First few observations of the dataset ID Choice_situation alt1_x1 alt1_x2
    #> alt1_x3 alt2_x1 alt2_x2 alt2_x3 Block 1 1 7 80 25 100 60 200 100 1 2 1 19 20 25
    #> 50 60 25 0 1 3 1 30 20 100 50 80 50 100 1 4 1 32 40 200 25 80 25 0 1 5 1 39 40
    #> 200 0 80 100 100 1 6 1 48 60 50 25 20 50 100 1 group V_1 V_2 e_1 e_2 U_1 U_2
    #> CHOICE 1 1 -0.55 -12.60 1.4192138 0.2215459 0.8692138 -12.378454 1 2 1 -0.95
    #> -2.35 2.4848661 0.6918230 1.5348661 -1.658177 1 3 1 -6.20 -2.30 1.6265497
    #> 0.2191308 -4.5734503 -2.080869 2 4 1 -13.90 -2.55 0.2251272 -0.5299483
    #> -13.6748728 -3.079948 2 5 1 -14.40 -5.80 0.3548944 -0.8771261 -14.0451056
    #> -6.677126 2 6 1 -3.60 -1.70 -1.2371637 -0.1345258 -4.8371637 -1.834526 2
    #> No preprocess function provided. Proceeding without additional preprocessing.
    #>
    #> dataset preprossed_data exists: FALSE
    #> decisiongroups exists: TRUE
    #> Group counts:
    #>
    #> 1 2 1007 433
    #>
    #>
    #>
    #> data has been created
    #> First few observations of the dataset ID Choice_situation alt1_x1 alt1_x2
    #> alt1_x3 alt2_x1 alt2_x2 alt2_x3 Block 1 1 7 80 25 100 60 200 100 1 2 1 19 20 25
    #> 50 60 25 0 1 3 1 30 20 100 50 80 50 100 1 4 1 32 40 200 25 80 25 0 1 5 1 39 40
    #> 200 0 80 100 100 1 6 1 48 60 50 25 20 50 100 1 group V_1 V_2 e_1 e_2 U_1 U_2
    #> CHOICE 1 1 -0.55 -12.60 0.4151715 -1.2972373 -0.1348285 -13.8972373 1 2 1 -0.95
    #> -2.35 -0.9774608 2.8345422 -1.9274608 0.4845422 2 3 1 -6.20 -2.30 -0.4108713
    #> -0.6769322 -6.6108713 -2.9769322 2 4 1 -13.90 -2.55 0.7479234 -0.2349984
    #> -13.1520766 -2.7849984 2 5 1 -14.40 -5.80 0.1328288 1.0853506 -14.2671712
    #> -4.7146494 2 6 1 -3.60 -1.70 -1.2191798 0.7015295 -4.8191798 -0.9984705 2
    #> Transformed utility function (type: simple): [1] "U_1 = @bpreis * $alt1_x1 +
    #> @blade * $alt1_x2 + @bwarte * $alt1_x3 ;U_2 = @bpreis * $alt2_x1 + @blade *
    #> $alt2_x2 + @bwarte * $alt2_x3 ;"
    #> • `Choice situation` -> `Choice.situation`
    #> • `` -> `...10`
    #> Initial function value: -998.1319
    #> Initial gradient value:
    #> bpreis  blade bwarte
    #>   6820 -40550   6225
    #> initial  value 998.131940
    #> iter   2 value 789.382374
    #> iter   3 value 777.131356
    #> iter   4 value 774.698957
    #> iter   5 value 743.983087
    #> iter   6 value 735.184026
    #> iter   7 value 733.822906
    #> iter   8 value 733.788021
    #> iter   9 value 733.787950
    #> iter   9 value 733.787941
    #> iter   9 value 733.787934
    #> final  value 733.787934
    #> converged
    #> Initial function value: -998.1319
    #> Initial gradient value:
    #> bpreis  blade bwarte
    #>   6440 -39175   6600
    #> initial  value 998.131940
    #> iter   2 value 809.190369
    #> iter   3 value 797.131190
    #> iter   4 value 795.175818
    #> iter   5 value 760.235522
    #> iter   6 value 751.601571
    #> iter   7 value 750.199349
    #> iter   8 value 750.167039
    #> iter   9 value 750.166980
    #> iter   9 value 750.166970
    #> iter   9 value 750.166963
    #> final  value 750.166963
    #> converged
    #>
    #>
    #> Summary table:
    #>
    #>
    #> ================  ====  ===  =====  ===  ======  =====  =====  =====  ====  ========  ===
    #> \                 vars    n   mean   sd  median    min    max  range  skew  kurtosis   se
    #> ================  ====  ===  =====  ===  ======  =====  =====  =====  ====  ========  ===
    #> est_bpreis           1    2  -0.01    0   -0.01  -0.01  -0.01      0     0     -2.75    0
    #> est_blade            2    2  -0.02    0   -0.02  -0.02  -0.01      0     0     -2.75    0
    #> est_bwarte           3    2   0.01    0    0.01   0.01   0.01      0     0     -2.75    0
    #> rob_pval0_bpreis     4    2   0.01    0    0.01   0.01   0.02      0     0     -2.75    0
    #> rob_pval0_blade      5    2   0.00    0    0.00   0.00   0.00      0     0     -2.75    0
    #> rob_pval0_bwarte     6    2   0.00    0    0.00   0.00   0.00      0     0     -2.75    0
    #> ================  ====  ===  =====  ===  ======  =====  =====  =====  ====  ========  ===
    #>
    #> Power results:
    #>
    #> TRUE
    #>  100
    #> 'simple' is deprecated and will be removed in the future. Use 'exact' instead.
    #> bcoeff_lookup already exists; skipping modification.
    #>
    #> Utility function used in simulation (true utility):
    #> $u1
    #> $u1$v1
    #> V.1 ~ bpreis * alt1.x1 + blade * alt1.x2 + bwarte * alt1.x3
    #> <environment: 0x6553d51a60a8>
    #>
    #> $u1$v2
    #> V.2 ~ bpreis * alt2.x1 + blade * alt2.x2 + bwarte * alt2.x3
    #> <environment: 0x6553d51803b8>
    #>
    #>
    #> $u2
    #> $u2$v1
    #> V.1 ~ bpreis * alt1.x1
    #> <environment: 0x6553d5149168>
    #>
    #> $u2$v2
    #> V.2 ~ bpreis * alt2.x1
    #> <environment: 0x6553d510a408>
    #>
    #>
    #> New names:
    #>  No preprocess function provided. Proceeding without additional preprocessing.
    #>
    #>
    #>
    #>  dataset preprossed_data exists: FALSE
    #>
    #>
    #>  decisiongroups exists: TRUE
    #>
    #> Group counts:
    #>
    #>    1    2
    #> 1007  433
    #>
    #>
    #>
    #>
    #>  data has been created
    #>
    #>
    #> First few observations of the dataset
    #>   ID Choice_situation alt1_x1 alt1_x2 alt1_x3 alt2_x1 alt2_x2 alt2_x3 Block
    #> 1  1               12      60      25       0      20     200     100     1
    #> 2  1               16      20     100      50      40      50       0     1
    #> 3  1               17      20     200       0      80     100     100     1
    #> 4  1               25      60      50     100      20     200      50     1
    #> 5  1               29      20      50     100      80      50       0     1
    #> 6  1               32      40     100      25      80      25      50     1
    #>   group    V_1    V_2        e_1        e_2         U_1        U_2 CHOICE
    #> 1     1  -2.35 -12.20 -0.5779528 -0.8428726  -2.9279528 -13.042873      1
    #> 2     1  -6.20  -3.90  0.1718701  2.4305443  -6.0281299  -1.469456      2
    #> 3     1 -14.20  -5.80  3.6657341 -0.3602399 -10.5342659  -6.160240      2
    #> 4     1  -2.10 -13.20  1.0176273 -0.8641231  -1.0823727 -14.064123      1
    #> 5     1  -1.70  -4.30  2.0034992 -1.2099166   0.3034992  -5.509917      1
    #> 6     1  -6.90  -1.55 -0.8402340  0.2320075  -7.7402340  -1.317993      2
    #>
    #>
    #>
    #>  No preprocess function provided. Proceeding without additional preprocessing.
    #>
    #>
    #>
    #>  dataset preprossed_data exists: FALSE
    #>
    #>
    #>  decisiongroups exists: TRUE
    #>
    #> Group counts:
    #>
    #>    1    2
    #> 1007  433
    #>
    #>
    #>
    #>
    #>  data has been created
    #>
    #>
    #> First few observations of the dataset
    #>   ID Choice_situation alt1_x1 alt1_x2 alt1_x3 alt2_x1 alt2_x2 alt2_x3 Block
    #> 1  1               12      60      25       0      20     200     100     1
    #> 2  1               16      20     100      50      40      50       0     1
    #> 3  1               17      20     200       0      80     100     100     1
    #> 4  1               25      60      50     100      20     200      50     1
    #> 5  1               29      20      50     100      80      50       0     1
    #> 6  1               32      40     100      25      80      25      50     1
    #>   group    V_1    V_2         e_1         e_2         U_1        U_2 CHOICE
    #> 1     1  -2.35 -12.20  2.72395272 -0.46401377   0.3739527 -12.664014      1
    #> 2     1  -6.20  -3.90  0.07465059  0.30152772  -6.1253494  -3.598472      2
    #> 3     1 -14.20  -5.80 -0.26590307 -1.03298376 -14.4659031  -6.832984      2
    #> 4     1  -2.10 -13.20 -0.38975123  0.49288244  -2.4897512 -12.707118      1
    #> 5     1  -1.70  -4.30 -0.54471014  0.08824802  -2.2447101  -4.211752      1
    #> 6     1  -6.90  -1.55  0.81615270 -0.59339607  -6.0838473  -2.143396      2
    #>
    #>
    #>
    #> Transformed utility function (type: simple):
    #> [1] "U_1 = @bpreis * $alt1_x1 + @blade * $alt1_x2 + @bwarte * $alt1_x3 ;U_2 = @bpreis * $alt2_x1 + @blade * $alt2_x2 + @bwarte * $alt2_x3 ;"
    #> Initial function value: -998.1319
    #> Initial gradient value:
    #> bpreis  blade bwarte
    #>   4840 -38625   8450
    #> initial  value 998.131940
    #> iter   2 value 817.475918
    #> iter   3 value 815.431642
    #> iter   4 value 813.052538
    #> iter   5 value 764.476070
    #> iter   6 value 754.717940
    #> iter   7 value 753.223793
    #> iter   8 value 753.200553
    #> iter   9 value 753.200421
    #> iter  10 value 753.200013
    #> iter  10 value 753.200008
    #> iter  11 value 753.199983
    #> iter  12 value 753.199955
    #> iter  12 value 753.199955
    #> iter  12 value 753.199949
    #> final  value 753.199949
    #> converged
    #> Initial function value: -998.1319
    #> Initial gradient value:
    #> bpreis  blade bwarte
    #>   4800 -37925   8400
    #> initial  value 998.131940
    #> iter   2 value 827.821924
    #> iter   3 value 827.231563
    #> iter   4 value 825.298772
    #> iter   5 value 774.246520
    #> iter   6 value 764.599998
    #> iter   7 value 763.084991
    #> iter   8 value 763.061068
    #> iter   9 value 763.060955
    #> iter  10 value 763.060676
    #> iter  10 value 763.060671
    #> iter  10 value 763.060665
    #> final  value 763.060665
    #> converged
    #>
    #>
    #> Summary table:
    #>
    #>
    #> ================  ====  ===  =====  ===  ======  =====  =====  =====  ====  ========  ===
    #> \                 vars    n   mean   sd  median    min    max  range  skew  kurtosis   se
    #> ================  ====  ===  =====  ===  ======  =====  =====  =====  ====  ========  ===
    #> est_bpreis           1    2  -0.01    0   -0.01  -0.01  -0.01      0     0     -2.75    0
    #> est_blade            2    2  -0.01    0   -0.01  -0.01  -0.01      0     0     -2.75    0
    #> est_bwarte           3    2   0.01    0    0.01   0.01   0.01      0     0     -2.75    0
    #> rob_pval0_bpreis     4    2   0.00    0    0.00   0.00   0.00      0     0     -2.75    0
    #> rob_pval0_blade      5    2   0.00    0    0.00   0.00   0.00      0     0     -2.75    0
    #> rob_pval0_bwarte     6    2   0.00    0    0.00   0.00   0.00      0     0     -2.75    0
    #> ================  ====  ===  =====  ===  ======  =====  =====  =====  ====  ========  ===
    #>
    #> Power results:
    #>
    #> TRUE
    #>  100
    #> total time for simulation and estimation: 10.667 sec elapsed
    #> $tic
    #> elapsed
    #>  22.121
    #>
    #> $toc
    #> elapsed
    #>  32.788
    #>
    #> $msg
    #> [1] "total time for simulation and estimation"
    #>
    #> $callback_msg
    #> [1] "total time for simulation and estimation: 10.667 sec elapsed"
    #>
    #> Trying tidyr::pivot_longer for reshaping...

# 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 is contained in ->olddesign->summary

    ## nested results are hard coded, if the design changes this must aswell
    simulationCoeff <- sedrive$olddesign$coefs
    coeffSummary <- sedrive$olddesign$summary

    print(simulationCoeff)
    #> NULL
    print(coeffSummary)
    #> NULL