Skip to content

Commit

Permalink
Updated examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
edsandorf committed Jun 15, 2023
1 parent 4d0a4ae commit 37fc3a0
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 15 deletions.
13 changes: 8 additions & 5 deletions examples/mnl-design-bayesian-priors.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ rm(list = ls(all = TRUE))
# library(spdesign)

# Define the list of utility functions ----
#' Generating a design with Bayesian priors.
utility <- list(
alt1 = "b_x1[0.1] * x_1[2:5] + b_x2[uniform_p(-1, 1)] * x_2[c(0, 1)] + b_x3[normal_p(0, 1)] * x_3[seq(0, 1, 0.25)]",
alt2 = "b_x1 * x_1 + b_x2 * x_2 + b_x3 * x_3"
alt1 = "b_x1[0.1] * x1[2:5] + b_x2[uniform_p(-1, 1)] * x2[c(0, 1)] + b_x3[normal_p(0, 1)] * x3[seq(0, 1, 0.25)]",
alt2 = "b_x1 * x1 + b_x2 * x2 + b_x3 * x3"
)

# Generate designs ----
design <- generate_design(utility, rows = 6,
design <- generate_design(utility, rows = 20,
model = "mnl", efficiency_criteria = "d-error",
algorithm = "federov", draws = "scrambled-sobol",
algorithm = "rsc", draws = "scrambled-sobol",
control = list(
max_iter = 100
max_iter = 10000
))

summary(design)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# Example file for creating a simple MNL design with Bayesian priors
#
rm(list = ls(all = TRUE))
# library(spdesign)

# Define the list of utility functions ----
#' Generating a design with Bayesian priors using dummy coding and level occurrences
#' NOTE: This design may take a long time. It has to first find a candidate that
#' meets the restrictions and then evaluate whether it is better than the
#' current best. Little information is provided about the process along the way.
utility <- list(
alt1 = "b_x1_dummy[c(uniform_p(-1, 1), uniform_p(-1, 1))] * x1[c(1, 2, 3)](6:10, 4:14, 6:10) + b_x2[0.4] * x2[c(0, 1)](9:11) + b_x3[-0.2] * x3[seq(0, 1, 0.25)]",
alt2 = "b_x1_dummy * x1 + b_x2 * x2 + b_x3 * x3"
)


# Generate designs ----
design <- generate_design(utility, rows = 20,
model = "mnl", efficiency_criteria = "d-error",
algorithm = "random", draws = "scrambled-sobol",
control = list(
max_iter = 10000
))

summary(design)
3 changes: 3 additions & 0 deletions examples/mnl-design-dummy-coding.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ design <- generate_design(utility, rows = 20,

# Add a blocking variable to the design with 2 blocks.
design <- block(design, 2)


summary(design)
6 changes: 4 additions & 2 deletions examples/mnl-design-random-with-specified-level-occurrence.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ rm(list = ls(all = TRUE))

# Define the list of utility functions ----
utility <- list(
alt1 = "b_x1_dummy[c(0.1, 0.2)] * x_1[c(1, 3, 5)](6:10) + b_x2[0.4] * x_2[c(0, 1)](9:11) + b_x3[-0.2] * x_3[seq(0, 1, 0.25)]",
alt2 = "b_x1_dummy * x_1 + b_x2 * x_2 + b_x3 * x_3"
alt1 = "b_x1_dummy[c(0.1, 0.2)] * x1[c(1, 2, 3)](4:14) + b_x2[0.4] * x2[c(0, 1)](9:11) + b_x3[-0.2] * x3[seq(0, 1, 0.25)]",
alt2 = "b_x1_dummy * x1 + b_x2 * x2 + b_x3 * x3"
)


Expand All @@ -17,3 +17,5 @@ design <- generate_design(utility, rows = 20,
algorithm = "federov", draws = "scrambled-sobol")

design <- block(design, 4)

summary(design)
File renamed without changes.
9 changes: 6 additions & 3 deletions examples/mnl-design-with-interactions.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ rm(list = ls(all = TRUE))

# Define the list of utility functions ----
utility <- list(
alt1 = "b_x1[0.1] * x_1[1:5] + b_x2[0.4] * x_2[c(0, 1)] + b_x3[-0.2] * x_3[seq(0, 1, 0.25)] + b_x1x2[-0.1] * I(x_1 * x_2)",
alt2 = "b_x1 * x_1 + b_x2 * x_2 + b_x3 * x_3"
alt1 = "b_x1[0.1] * x1[1:5] + b_x2[0.4] * x2[c(0, 1)] + b_x3[-0.2] * x3[seq(0, 1, 0.25)] + b_x1x2[-0.1] * I(x1 * x2)",
alt2 = "b_x1 * x1 + b_x2 * x2 + b_x3 * x3"
)

# Generate designs ----
design <- generate_design(utility, rows = 20,
model = "mnl", efficiency_criteria = "d-error",
algorithm = "federov", draws = "scrambled-sobol")
algorithm = "federov", draws = "scrambled-sobol",
dudx = "b_x3")

summary(design)
9 changes: 6 additions & 3 deletions examples/mnl-design-with-multiway-interactions.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ rm(list = ls(all = TRUE))

# Define the list of utility functions ----
utility <- list(
alt1 = "b_x1[0.1] * x_1[1:5] + b_x2[0.4] * x_2[c(0, 1)] + b_x3[-0.2] * x_3[seq(0, 1, 0.25)] + b_x1x2[-0.1] * I(x_1 * x_2)",
alt2 = "b_x1 * x_1 + b_x2 * x_2 + b_x3 * x_3 + b_x1x2x3[0.1] * I(x_1 * x_2 * x_3)"
alt1 = "b_x1[0.1] * x1[1:5] + b_x2[0.4] * x2[c(0, 1)] + b_x3[-0.2] * x3[seq(0, 1, 0.25)] + b_x1x2[-0.1] * I(x1 * x2)",
alt2 = "b_x1 * x1 + b_x2 * x2 + b_x3 * x3 + b_x1x2x3[0.1] * I(x1 * x2 * x3)"
)

# Generate designs ----
design <- generate_design(utility, rows = 20,
model = "mnl", efficiency_criteria = "d-error",
algorithm = "federov", draws = "scrambled-sobol")
algorithm = "rsc", draws = "scrambled-sobol",
dudx = "b_x3")

summary(design)
7 changes: 5 additions & 2 deletions examples/mnl-design-with-supplied-candidate-set.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ utility <- list(
# Use the full factorial as the candidate set
candidate_set <- full_factorial(
list(
alt1_x1 = 1:5,
alt1_x1 = 2:5,
alt1_x2 = c(0, 1),
alt1_x3 = seq(0, 1, 0.25),
alt2_x1 = 1:5,
alt2_x1 = 2:5,
alt2_x2 = c(0, 1),
alt2_x3 = seq(0, 1, 0.25)
)
Expand All @@ -30,3 +30,6 @@ design <- generate_design(utility, rows = 20,
model = "mnl", efficiency_criteria = "d-error",
algorithm = "federov", draws = "scrambled-sobol",
candidate_set = candidate_set)


summary(design)
3 changes: 3 additions & 0 deletions examples/mnl-design.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ design <- generate_design(utility, rows = 20,

# Add a blocking variable to the design with 4 blocks.
design <- block(design, 4)


summary(design)

0 comments on commit 37fc3a0

Please sign in to comment.