Preparing Data for Choice-Based Conjoint Models
Source:vignettes/02_preparing_model_data.Rmd
02_preparing_model_data.RmdIntroduction
This vignette demonstrates how to prepare survey data for fitting
choice-based conjoint models using the cjt_model() function
from the g6tr.conjoint package. We will walk through
transforming survey data from its wide format to the long format
required by cjt_model(), addressing common issues that may
arise during this process.
Example Data
We will use the internal datasets included in the package:
-
example_cjt_fp_drn_survey: Survey responses in wide format. -
example_cjt_fp_drn_design: The experimental design dataframe.
The survey involves a dual-response none (DRN) conjoint experiment where respondents first choose among subscription propositions without a “none” option and then answer follow-up DRN questions.
Survey response data
First, load the necessary libraries and examine the survey data structure.
glimpse(example_cjt_fp_drn_survey)
#> Rows: 843
#> Columns: 54
#> $ response_id <chr> "R_0001", "R_0002", "R_0003", "R_0004", "R_0005"…
#> $ weight <dbl> 0.9517360, 0.8810786, 1.0007142, 1.0733095, 0.94…
#> $ des_version <chr> "1", "1", "1", "10", "10", "10", "11", "11", "11…
#> $ country <fct> Italy, Italy, Italy, Italy, Italy, Italy, Italy,…
#> $ m3_customer <fct> Non-Customer, Non-Customer, Non-Customer, Non-Cu…
#> $ m3_sub_interest <fct> Swing, Swing, Swing, Swing, Swing, Swing, Swing,…
#> $ a1_m2_cjt <fct> Basic Plan, Basic Plan, Pro Plan, Pro Plan, Pro …
#> $ a2_m2_cjt <fct> Basic Plan, Standard Plan, Standard Plan, Pro Pl…
#> ........................
#> $ a12_m2_cjt <fct> Basic Plan, Basic Plan, Standard Plan, Pro Plan,…
#> $ a1_m2_sub_buy_cjt <fct> Yes, Yes, Yes, Yes, Yes, Yes, Yes, No, Yes, Yes,…
#> $ a2_m2_sub_buy_cjt <fct> Yes, Yes, Yes, Yes, No, Yes, Yes, No, Yes, Yes, …
#> ........................
#> $ a12_m2_sub_buy_cjt <fct> Yes, Yes, Yes, Yes, Yes, Yes, Yes, No, No, Yes, …
#> $ a1_m2_2nd_dev_cjt_it <fct> Yes, Yes, Yes, Yes, Yes, Yes, Yes, NA, Yes, Yes,…
#> $ a1_m2_2nd_dev_cjt_sp <fct> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
#> $ a2_m2_2nd_dev_cjt_it <fct> Yes, Yes, Yes, Yes, NA, Yes, Yes, NA, Yes, Yes, …
#> $ a2_m2_2nd_dev_cjt_sp <fct> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
#> ........................
#> $ a12_m2_2nd_dev_cjt_it <fct> Yes, Yes, No, Yes, Yes, Yes, Yes, NA, NA, Yes, Y…
#> $ a12_m2_2nd_dev_cjt_sp <fct> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …Column details:
-
response_id: Unique identifier for each respondent. -
weight: Case weight for each respondent. -
country,m3_customer,m3_sub_interest: Respondent-level demographic variables. -
a*_m2_cjt: Responses to the main conjoint tasks (choices made). -
a*_m2_sub_buy_cjt: Responses to the “subscribe” follow-up DRN questions. -
a*_m2_2nd_dev_cjt_itanda*_m2_2nd_dev_cjt_sp: Responses to the “second device” follow-up DRN questions for Italy and Spain, respectively. -
des_version: The design version assigned to each respondent.
Each respondent answered 12 conjoint tasks, selecting from three alternatives in each task. Follow-up DRN questions were asked after each task:
- Subscription Buy: Whether the respondent would buy the chosen subscription.
- Second Device: Whether the respondent would like to add a second device (questions vary for Italy and Spain).
Experimental Design Data
The example_cjt_fp_drn_design dataframe contains the
experimental design:
head(example_cjt_fp_drn_design)
#> # A tibble: 6 × 4
#> version task alt attributes$service $scenario
#> <dbl> <dbl> <dbl> <fct> <fct>
#> 1 1 1 1 No Service Scenario 3
#> 2 1 1 2 AppleTV+ and Apple Arcade Scenario 2
#> 3 1 1 3 Apple One Premier Scenario 1
#> 4 1 2 1 No Service Scenario 1
#> 5 1 2 2 AppleCare+ and iCloud+ 200GB Scenario 2
#> 6 1 2 3 Apple One Family and AppleCare+ Scenario 1Key aspects:
-
version: Design version (1 to 30). -
task: Task number (1 to 12 per version). -
alt: Alternative number within a task (1 to 3). -
attributes: A packed dataframe column containing the attributes defining each alternative.
Objective
Transform the survey data into a long format suitable for
cjt_model(), ensuring it includes:
- Respondent identifiers.
- Task identifiers.
- Predictor variables (attributes).
- Outcome variables (choice indicators for initial and DRN questions).
Preparing the Data
Parsing Survey Responses
We use the parse_wide_task_data() function to transform
the wide-format survey data into a long format.
example_cjt_fp_drn_answers_tall_raw <- parse_wide_task_data(
data = example_cjt_fp_drn_survey,
id_col = "response_id",
nn_answer = "a{task}_m2_cjt",
drn_sub_buy_answer = "a{task}_m2_sub_buy_cjt",
drn_2nd_dev_answer_it = "a{task}_m2_2nd_dev_cjt_it",
drn_2nd_dev_answer_sp = "a{task}_m2_2nd_dev_cjt_sp",
keep = c(weight, des_version, country, m3_customer, m3_sub_interest)
)
#>
#> ── Summary of task sets for answer column patterns ─────────────────────────────
#> • All patterns
#> → 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, and 12
#>
glimpse(example_cjt_fp_drn_answers_tall_raw)
#> Rows: 10,116
#> Columns: 11
#> $ id <chr> "R_0001", "R_0001", "R_0001", "R_0001", "R_0001"…
#> $ weight <dbl> 0.9517360, 0.9517360, 0.9517360, 0.9517360, 0.95…
#> $ des_version <chr> "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"…
#> $ country <fct> Italy, Italy, Italy, Italy, Italy, Italy, Italy,…
#> $ m3_customer <fct> Non-Customer, Non-Customer, Non-Customer, Non-Cu…
#> $ m3_sub_interest <fct> Swing, Swing, Swing, Swing, Swing, Swing, Swing,…
#> $ task <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, …
#> $ nn_answer <fct> Basic Plan, Basic Plan, Basic Plan, Basic Plan, …
#> $ drn_sub_buy_answer <fct> Yes, Yes, Yes, Yes, Yes, Yes, Yes, Yes, Yes, Yes…
#> $ drn_2nd_dev_answer_it <fct> Yes, Yes, Yes, Yes, Yes, Yes, Yes, Yes, Yes, Yes…
#> $ drn_2nd_dev_answer_sp <fct> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …Explanation:
-
id_col: Specifies the respondent ID column. -
Answer Patterns: We define patterns for the answer
columns, where
{task}is a placeholder for task numbers (1 to 12). -
keep: Additional columns to retain in the transformed dataframe.
Data Cleaning and Transformation
Due to survey logic, DRN responses for the “second device” question
are split between two columns (drn_2nd_dev_answer_it for
Italy and drn_2nd_dev_answer_sp for Spain). We combine them
into a single column.
example_cjt_fp_drn_answers_tall <-
example_cjt_fp_drn_answers_tall_raw |>
mutate(
drn_2nd_dev_answer = coalesce(drn_2nd_dev_answer_it, drn_2nd_dev_answer_sp)
) |>
select(-drn_2nd_dev_answer_it, -drn_2nd_dev_answer_sp)We convert response columns to the required types:
-
nn_answer: From factor to numeric (alternative number selected). We know that “Basic Plan” is always the first alternative, “Standard Plan” is the second, and “Pro Plan” is the third. -
drn_sub_buy_answer: From “Yes”/“No” to logical (TRUE/FALSE). -
drn_2nd_dev_answer: From “Yes”/“No” to logical (TRUE/FALSE).
example_cjt_fp_drn_answers_tall <-
example_cjt_fp_drn_answers_tall |>
mutate(
nn_answer = as.numeric(factor(
nn_answer,
levels = c("Basic Plan", "Standard Plan", "Pro Plan")
)),
drn_sub_buy_answer = drn_sub_buy_answer == "Yes",
drn_2nd_dev_answer = drn_2nd_dev_answer == "Yes"
)Merging with the Experimental Design
We merge the survey responses with the experimental design to associate each choice with the corresponding alternative attributes.
# we don't need packing in the model data
design_data <-
example_cjt_fp_drn_design |>
unpack(cols = attributes)
# Merge responses with design data
example_cjt_fp_drn_flat_data <-
example_cjt_fp_drn_answers_tall |>
rename(version = des_version) |>
mutate(version = as.numeric(version)) |>
left_join(
design_data,
by = c("version", "task"),
relationship = "many-to-many"
)Creating Outcome Variables
We create choice indicators for the initial choice
(nn_choice) and the two DRN questions
(drn_sub_buy_choice, drn_2nd_dev_choice):
-
nn_choice:TRUEif the alternative matches the respondent’s selected alternative. -
drn_sub_buy_choice:TRUEifnn_choiceisTRUEand the respondent answeredTRUEtodrn_sub_buy_answer. -
drn_2nd_dev_choice:TRUEifnn_choiceisTRUEand the respondent answeredTRUEtodrn_2nd_dev_answer.
example_cjt_fp_drn_flat_data <-
example_cjt_fp_drn_flat_data |>
mutate(
nn_choice = (nn_answer == alt),
drn_sub_buy_choice = nn_choice & drn_sub_buy_answer,
drn_2nd_dev_choice = nn_choice & drn_2nd_dev_answer
) |>
select(
id, weight, country, m3_customer, m3_sub_interest,
task, alt, everything(),
-nn_answer, -drn_sub_buy_answer, -drn_2nd_dev_answer, -version
)Final Data Overview
The example_cjt_fp_drn_flat_data dataframe is now ready
for modeling:
glimpse(example_cjt_fp_drn_flat_data)
#> Rows: 30,348
#> Columns: 12
#> $ id <chr> "R_0001", "R_0001", "R_0001", "R_0001", "R_0001", "…
#> $ weight <dbl> 0.951736, 0.951736, 0.951736, 0.951736, 0.951736, 0…
#> $ country <fct> Italy, Italy, Italy, Italy, Italy, Italy, Italy, It…
#> $ m3_customer <fct> Non-Customer, Non-Customer, Non-Customer, Non-Custo…
#> $ m3_sub_interest <fct> Swing, Swing, Swing, Swing, Swing, Swing, Swing, Sw…
#> $ task <dbl> 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, …
#> $ alt <dbl> 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, …
#> $ service <fct> No Service, AppleTV+ and Apple Arcade, Apple One Pr…
#> $ scenario <fct> Scenario 3, Scenario 2, Scenario 1, Scenario 1, Sce…
#> $ nn_choice <lgl> TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE…
#> $ drn_sub_buy_choice <lgl> TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE…
#> $ drn_2nd_dev_choice <lgl> TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE…