Introduction
The g6tr.conjoint package provides tools for fitting
choice-based conjoint models using the cjt_model class,
integrated with the tidymodels framework. This vignette
covers handling choice data with and without a “none” option,
implementing dual-response none (DRN) models, and generating
predictions. It also discusses evaluating model performance using
metrics like the Brier score and demonstrates how to perform v-fold
cross-validation.
Data used in this vignette
We’ll use the internal dataset
example_cjt_fp_drn_flat_data included in the package. This
dataset contains responses from a choice-based conjoint survey, where
respondents evaluated different subscription propositions. Each
proposition was defined by a combination of service type and pricing
scenario.
First, load the necessary libraries and examine the data structure.
library(g6tr.conjoint)
library(dplyr)
glimpse(example_cjt_fp_drn_flat_data)
#> Rows: 30,348
#> Columns: 11
#> $ 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, …
#> $ 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…-
id: A unique identifier for each respondent (e.g., “R_0001”). -
weight: A numeric value representing the weight assigned to each respondent. -
country,m3_customer,m3_sub_interest: These are respondent-level variables. Since the model we are using fits respondent-level utilities, these variables will not be included in the model. -
task: A numeric value representing the choice task number, with each respondent answering 12 tasks, each containing 3 options. -
serviceandscenario: These factor variables define task options, withserviceindicating the service bundle (e.g., “No Service”, “AppleTV+ and Apple Arcade”) andscenarioindicating the pricing scenarios. -
nn_choice: This logical variable indicates if a concept was chosen (TRUE) or not (FALSE) in each task of the conjoint experiment, where respondents select one of three options without a “none” choice. Thus, onenn_choiceis TRUE and the others are FALSE per task. -
drn_sub_buy_choice,drn_2nd_dev_choice: These variables represent the dual-response none (DRN) questions. After the initial choice question (where “none” is not an option), respondents are asked two independent follow-up questions about the chosen option: whether they would actually buy the subscription (drn_sub_buy_choice) and whether they would like to add a second device (drn_2nd_dev_choice). These can be considered as two separate DRN choice model experiments with a shared initial question. Each of these variables can only be TRUE if the corresponding option was chosen in the initial question. If the respondent answered “no” to either of the follow-up questions, the respective DRN column for that task will be all FALSE.
Typically the survey data does not come in such a long format and we need to restructure it to fit this format. We will demonstrate how to do this in a separate vignette.
Fitting a model
Fitting a model without a “none” option
Let’s start with the simplest case: modeling the initial choice task
(nn_choice) where respondents selected one of three
alternatives, with no “none” option available.
mod <- cjt_model(
nn_choice ~ service + scenario,
data = example_cjt_fp_drn_flat_data,
id_col = id,
task_col = task,
mcmc = list(R = 200, use = 200) # Adjust MCMC iterations as needed
)
mod
#> Conjoint Task Model
#> • Number of Respondents: 843
#> • ID Column: "id"
#> • Task Column: "task"
#> • Formula: "nn_choice ~ service + scenario"
#> • Allows None: `FALSE`The choice model can be thought of as an extension of logistic
regression. Like logistic regression, it predicts a binary outcome
(nn_choice) using predictors (service and
scenario). However, two additional elements are crucial for
modeling choice behavior:
Respondent ID (
id_col = id): The model estimates separate coefficients for each respondent. In other words, the choice outcome depends on who answers the question.Task ID (
task_col = task): Choice probabilities are computed relative to other alternatives presented in the same task. In other words, the choice outcome depends on what other alternatives are available to choose from.
The cjt_model() function accepts additional arguments
through ... which are passed to the underlying estimation
method ChoiceModelR::choicemodelr(). The most commonly used
is mcmc, which controls the MCMC sampling parameters. For
example, mcmc = list(R = 200, use = 200) sets the total
number of iterations (R) and how many of the last
iterations to use for computing results (use). In practice,
you would want to use much bigger numbers for better results.
If you’re curious, you can explore the individual-level part-worth
utilities stored in mod$pwu. Factor variables
(service and scenario) are automatically
decomposed into dummy-coded flags, resulting in a coefficient for each
level of each variable for every respondent. The corresponding
respondent IDs can be found in mod$id.
glimpse(mod$pwu)
#> Rows: 843
#> Columns: 13
#> $ `service[No Service]` <dbl> 1.59009627, 0.82561414, -0.560…
#> $ `service[Apple Music]` <dbl> 0.18324320, 0.89161641, -0.076…
#> $ `service[AppleCare+ and iCloud+ 200GB]` <dbl> 0.517004697, 0.782938940, 0.52…
#> $ `service[AppleTV+ and Apple Arcade]` <dbl> 0.056065035, 0.245408860, 0.12…
#> $ `service[iCloud+ 2TB]` <dbl> 0.445662486, 1.096910279, 0.44…
#> $ `service[Standard Accessory Package]` <dbl> 0.4145157, 1.0266770, 0.363929…
#> $ `service[Apple One Premier]` <dbl> -0.44008805, -0.44721244, -0.1…
#> $ `service[Premium Accessory Package]` <dbl> -0.21039258, -0.60183773, -0.2…
#> $ `scenario[Scenario 1]` <dbl> -0.15869537, 0.19715866, 0.285…
#> $ `scenario[Scenario 2]` <dbl> -0.196041343, 0.069552657, 0.2…
#> $ `scenario[Scenario 3]` <dbl> 0.153506752, 0.452214963, -0.1…
#> $ `scenario[Scenario 4]` <dbl> 0.32860068, 0.07431212, -0.078…
#> $ `scenario[Scenario 5]` <dbl> -0.08538145, -0.51377421, 0.04…
head(mod$id)
#> [1] "R_0001" "R_0002" "R_0003" "R_0004" "R_0005" "R_0006"Model with a “None” Option
If your experiment allows respondents to not select any of the
presented options, you can specify this using the
allows_none() function in the formula.
mod_with_none <- cjt_model(
allows_none(drn_sub_buy_choice) ~ service + scenario,
data = example_cjt_fp_drn_flat_data,
id_col = id,
task_col = task,
mcmc = list(R = 200, use = 200)
)If you explore the part-worth utilities in
mod_with_none$pwu, you’ll notice an additional column for
the “none” utilities.
Dual-Response None (DRN) Model
In DRN experiments, respondents first choose among options without a “none” option, then indicate if they would opt for “none” in a follow-up. For these experiments, it makes sense to model both responses simultaneously, as if feeding two batches of information into our model:
- No “None” responses allow the model to better capture preferences for different attribute values.
- Responses with “None” help calibrate these preferences against the option of not choosing any of the presented alternatives.
Define this in the formula by adding both outcomes on the left:
Making Predictions and Model Evaluation
The cjt_model class integrates seamlessly with the
tidymodels framework, allowing you to use familiar methods
like predict() and augment() for generating
predictions and assessing model performance.
Generating Predictions
To make predictions using your fitted model, you can use the
predict() method. The input data should be a data frame
that includes the id and task columns, along
with the features used in the model.
There are two types of predictions you can generate:
- Probability Predictions (default): Returns the probability of each option being chosen.
-
Choice Predictions: Determines the option with the
highest probability and returns
1for that option and0for the others.
You can specify the type of prediction using the type
parameter in the predict() method.
If your model was trained with a “none” option (like the DRN model
mod_drn), you can allow “none” as an option in the
prediction method. In this case:
- For Probability Predictions, the probabilities for each task may not sum to 1, as the “none” option is considered.
- For Choice Predictions, all options can return
0, indicating that “none” had the highest probability.
# Prepare new data for prediction (example uses the original data)
# Take only data corresponding to a subset of id values and keep
# only two alternatives within each task
new_data <-
example_cjt_fp_drn_flat_data |>
filter(id %in% sample(unique(id), 10)) |>
group_by(id, task) |>
slice_head(n = 2) |>
ungroup() |>
select(id, task, service, scenario)
# Generate probability predictions
prob_predictions <- predict(mod, new_data, type = "probability")
# Generate choice predictions
choice_predictions <- predict(mod, new_data, type = "choice")Note: If a respondent’s ID does not appear in the training data, the model will not be able to make predictions for that respondent.
Augmenting Data with Predictions
The augment() method allows you to append the
predictions directly to your data frame. This is useful for comparing
the predicted choices with the actual choices in your dataset.
# Augment data with probability predictions
augmented_data <- augment(mod, new_data)
augmented_dataInterpretation of Predictions
In conjoint analyses, we are typically more interested in aggregate-level insights rather than individual-level predictions. Summarizing preferences across the entire population or specific subgroups provides more meaningful conclusions. This topic will be covered in a separate vignette.
However, respondent-level predictions can still be valuable, especially when assessing model performance.
Assessing Model Performance
To evaluate how well your model fits the data, you can compare the predicted choices against the actual choices in your dataset. Here’s how you can do this:
# Augment the original data with probability predictions
evaluation_data <-
example_cjt_fp_drn_flat_data |>
augment(new_data = _, x = mod, type = "probability") |>
rename(prob_pred = .pred) |>
augment(new_data = _, x = mod, type = "choice") |>
rename(choice_pred = .pred) |>
select(id, task, nn_choice, prob_pred, choice_pred) |>
mutate(
brier_score = (nn_choice - prob_pred)^2,
correct = nn_choice == choice_pred
)
# brier score
mean(evaluation_data$brier_score)
#> [1] 0.120502
# accuracy
mean(evaluation_data$correct)
#> [1] 0.855806Cross-Validation with V-Folds
In conjoint analysis, each respondent provides data for only a limited number of tasks, often around a dozen. Splitting this data into separate training and test sets can lead to situations where certain feature levels are not represented in the training data for some respondents, potentially resulting in biased models. Given these constraints, v-fold cross-validation is worth considering, as it allows each data point to contribute to both training and validation, potentially offering a more balanced assessment of model performance.
In the context of conjoint analysis, you’ll want to ensure that the splits respect the structure of your data:
- Task Integrity: Do not split rows belonging to the same task. When splitting data, divide complete tasks for each respondent.
- Respondent IDs: Remember that the model cannot make predictions for respondents it hasn’t seen during training.
Here’s how you can perform 4-fold cross-validation, where the 12 tasks for each respondent are split into 4 groups of 3 tasks each.
library(dplyr)
library(purrr)
# Assign fold numbers to tasks within each respondent
set.seed(123)
folds_data <- example_cjt_fp_drn_flat_data |>
group_by(id) |>
mutate(
task_group = ntile(task, 4) # Divide the 12 tasks into 4 groups
) |>
ungroup()
# List to store accuracy for each fold
fold_accuracies <- vector("numeric", 4)
# Perform cross-validation
for (i in 1:4) {
# Split data into training and testing sets based on task groups
train_data <- folds_data |> filter(task_group != i)
test_data <- folds_data |> filter(task_group == i)
# Fit the model on training data
mod_cv <- cjt_model(
nn_choice ~ service + scenario,
data = train_data,
id_col = id,
task_col = task,
mcmc = list(R = 200, use = 200)
)
# Generate choice predictions on test data
test_predictions <- predict(mod_cv, test_data, type = "choice")
# Compare predictions with actual choices
test_evaluation <- test_data |>
select(id, task, nn_choice) |>
bind_cols(test_predictions) |>
mutate(correct_prediction = nn_choice == .pred)
# Calculate accuracy for the fold
fold_accuracy <- mean(test_evaluation$correct_prediction)
fold_accuracies[i] <- fold_accuracy
}
# Calculate average accuracy across folds
average_accuracy <- mean(fold_accuracies)
average_accuracyExploring Conjoint Model Variations
While the initial experimental design might suggest a specific model
structure (e.g., predicting choice from
service and scenario as independent features),
you have the freedom to experiment with different formulas. This
flexibility allows you to explore various model specifications and gain
deeper insights:
-
Include Interaction Effects: Investigate how
combinations of attributes influence choices by adding interaction
terms. For example, you might explore the interaction between
serviceandscenario.
# Model with interaction between service and scenario
mod_interaction <- cjt_model(
nn_choice ~ service * scenario,
data = example_cjt_fp_drn_flat_data,
id_col = id,
task_col = task,
mcmc = list(R = 200, use = 200)
)- Focus on Specific Attributes: Build separate models for individual attributes to study their effects in isolation. This can help you understand the standalone impact of each attribute on choice.
# Model focusing only on service
mod_service <- cjt_model(
nn_choice ~ service,
data = example_cjt_fp_drn_flat_data,
id_col = id,
task_col = task,
mcmc = list(R = 200, use = 200)
)
# Model focusing only on scenario
mod_scenario <- cjt_model(
nn_choice ~ scenario,
data = example_cjt_fp_drn_flat_data,
id_col = id,
task_col = task,
mcmc = list(R = 200, use = 200)
)- Create New Predictors: Craft custom predictors based on domain knowledge or hypotheses. For example, you might create a variable that captures a specific combination of attributes or external factors.
# Adding a custom predictor
example_cjt_fp_drn_flat_data <- example_cjt_fp_drn_flat_data %>%
mutate(
premium_offer = service == "Apple One Family" & scenario == "Scenario 1"
)
# Model including the custom predictor
mod_custom <- cjt_model(
nn_choice ~ service + scenario + premium_offer,
data = example_cjt_fp_drn_flat_data,
id_col = id,
task_col = task,
mcmc = list(R = 200, use = 200)
)