# NOT RUN {
data("wide_conjoint")
# character string interface
## profile_variables
list1 <- list(
feature1 = list(
names(wide_conjoint)[grep("^feature1.{1}1", names(wide_conjoint))],
names(wide_conjoint)[grep("^feature1.{1}2", names(wide_conjoint))]
),
feature2 = list(
names(wide_conjoint)[grep("^feature2.{1}1", names(wide_conjoint))],
names(wide_conjoint)[grep("^feature2.{1}2", names(wide_conjoint))]
),
feature3 = list(
names(wide_conjoint)[grep("^feature3.{1}1", names(wide_conjoint))],
names(wide_conjoint)[grep("^feature3.{1}2", names(wide_conjoint))]
),
rating = list(
names(wide_conjoint)[grep("^rating.+1", names(wide_conjoint))],
names(wide_conjoint)[grep("^rating.+2", names(wide_conjoint))]
)
)
## task variables
list2 <- list(choice = paste0("choice_", letters[1:4]),
timing = paste0("timing_", letters[1:4]))
# formula interface
## profile_variables
list1 <- list(
feature1 = list(
~ feature1a1 + feature1b1 + feature1c1 + feature1d1,
~ feature1a2 + feature1b2 + feature1c2 + feature1d2
),
feature2 = list(
~ feature2a1 + feature2b1 + feature2c1 + feature2d1,
~ feature2a2 + feature2b2 + feature2c2 + feature2d2
),
feature3 = list(
~ feature3a1 + feature3b1 + feature3c1 + feature3d1,
~ feature3a2 + feature3b2 + feature3c2 + feature3d2
),
rating = list(
~ rating_a1 + rating_b1 + rating_c1 + rating_d1,
~ rating_a2 + rating_b2 + rating_c2 + rating_d2
)
)
# task variables
list2 <- list(choice = ~ choice_a + choice_b + choice_c + choice_d,
timing = ~ timing_a + timing_b + timing_c + timing_d)
# perform reshape
str(long <- cj_tidy(wide_conjoint,
profile_variables = list1,
task_variables = list2,
id = ~ respondent))
stopifnot(nrow(long) == nrow(wide_conjoint)*4*2)
# recode outcome so it is coded sensibly
long$chosen <- ifelse((long$profile == "A" & long$choice == 1) |
(long$profile == "B" & long$choice == 2), 1, 0)
# use for analysis
cj(long, chosen ~ feature1 + feature2 + feature3, id = ~ respondent)
# }
Run the code above in your browser using DataLab