# The following lines of code synthesize responses to Case 3 BWS questions,
# return them in detailed and simple format, and then fit the models using
# the function clogit in the survival package. The choice sets include
# three alternatives that are expressed by three attributes (A, B, and C)
# with three levels each. The function bws3.design creates the choice sets
# from a three-level fractional factorial design with 3 factors that is
# generated using the function oa.design in the DoE.base package and a BIBD
# with 9 treatments, 12 blocks, and size 3 that is generated using
# the function find.BIB in the crossdes package. The systematic component
# of the utility for alternatives where all of the three attributes are
# treated as categorical attributes is the same as that explained in
# the Details section.
if (FALSE) {
# Load packages
library(DoE.base)
library(survival)
library(crossdes)
# Create design matrix
set.seed(987)
bibd <- find.BIB(trt = 9, b = 12, k = 3)
isGYD(bibd)
oa <- oa.design(nl = c(3, 3, 3), randomize = FALSE)
atr <- list(
A = c("A1", "A2", "A3"), B = c("B1", "B2", "B3"),
C = c("C1", "C2", "C3"))
dsgn <- bws3.design(bibd = bibd, ffd = oa, attribute.levels = atr)
# Synthesize responses to Case 3 BWS questions
param <- c(0, 0.2, 0.4, 0, 0.4, 0.8, 0, -0.7, -1.4)
atr.names <- c("A", "B", "C")
## dataset in detailed format
dat.detail <- bws3.response(
design = dsgn, b = param, n = 100,
categorical.attributes = atr.names,
detail = TRUE, seed = 987)
str(dat.detail)
## dataset in simple format
dat.simple <- bws3.response(
design = dsgn, b = param, n = 100,
categorical.attributes = atr.names,
detail = FALSE, seed = 987)
str(dat.simple)
# Convert dat.simple into dataset for the analysis
dat.simple.md <- bws3.dataset(
data = dat.simple,
response = list(
c("B1", "W1"), c("B2", "W2"), c("B3", "W3"),
c("B4", "W4"), c("B5", "W5"), c("B6", "W6"),
c("B7", "W7"), c("B8", "W8"), c("B9", "W9"),
c("B10", "W10"), c("B11", "W11"), c("B12", "W12")),
choice.sets = dsgn,
categorical.attributes = atr.names,
model = "maxdiff")
# Fit conditional logit models
mf <- RES ~ A2 + A3 + B2 + B3 + C2 + C3 + strata(STR)
out.detail <- clogit(mf, dat.detail)
out.simple <- clogit(mf, dat.simple.md)
out.simple
all.equal(coef(out.detail), coef(out.simple))
}
Run the code above in your browser using DataLab