Predict and Augment Methods for Conjoint Task Models
Source:R/hardhat_predict.R
predict.cjt_model.RdGenerate predictions from a fitted conjoint task model:
predict()returns predictions as a new tibbleaugment()adds predictions as a new column to the input data
The model can predict both probabilities of selecting each alternative and binary choice indicators.
Arguments
- object, x
An object of class
cjt_modelcontaining the fitted model.- new_data
A data frame containing the predictor variables for which to generate predictions. Must include the ID and task columns specified during model fitting.
- type
A character string specifying the type of prediction. Must be one of:
"probability"(default): predicts probability of selecting each alternative"choice": predicts binary indicators for the most likely choice
- allow_none
Logical. Should predictions include "none" as a possible outcome? Default is FALSE. Only applicable if the model was fit with
allows_none().- ...
Additional arguments passed to methods.
Value
predict()returns a tibble with a single column.predcontaining the predictionsaugment()returns the input data with an additional.predcolumn
For both functions, the predictions are either:
For
type = "probability": probabilities of selecting each alternativeFor
type = "choice": binary indicators (0/1) for predicted choices
Details
The functions require that the ID and task columns used in model fitting are present
in new_data. These columns must be compatible with those used to fit the model.
When allow_none = TRUE, the model must have been fit with an outcome wrapped in
allows_none(). Otherwise, an error will be thrown.
Examples
# Fit a conjoint model
my_model <- cjt_model(
nn_choice + 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)
)
#> Logit Data
#> ==================================================
#> Attribute Type Levels
#> -----------------------------------
#> Attribute 1 Linear 1
#> Attribute 2 Linear 1
#> Attribute 3 Linear 1
#> Attribute 4 Linear 1
#> Attribute 5 Linear 1
#> Attribute 6 Linear 1
#> Attribute 7 Linear 1
#> Attribute 8 Linear 1
#> Attribute 9 Linear 1
#> Attribute 10 Linear 1
#> Attribute 11 Linear 1
#> Attribute 12 Linear 1
#> Attribute 13 Linear 1
#> Attribute 14 Linear 1
#>
#> 14 parameters to be estimated.
#>
#> 843 total units.
#> Average of 3.5 alternatives in each of 24 sets per unit.
#> 20232 tasks in total.
#>
#> Table of choice data pooled across units:
#> Choice Count Pct.
#> --------------------
#> 1 5224 25.82%
#> 2 6575 32.5%
#> 3 4399 21.74%
#> 4 4034 19.94%
#>
#> MCMC Inference for Hierarchical Logit
#> ==================================================
#> Total Iterations: 200
#> Draws used in estimation: 200
#> Units: 843
#> Parameters per unit: 14
#> Constraints not in effect.
#> Draws are to be saved.
#> Prior degrees of freedom: 5
#> Prior variance: 2
#>
#> MCMC Iteration Beginning...
#> Iteration Acceptance RLH Pct. Cert. Avg. Var. RMS Time to End
#> 100 0.361 0.389 0.220 0.40 0.45 0:02
#> 200 0.297 0.506 0.433 1.66 1.18 0:00
#>
#> Total Time Elapsed: 0:04
#>
#> Writing estimated unit-level betas to Rbetas.csv in the working directory
#>
#> Writing RLH, the geometric mean of the likelihood of the choices made,
#>
#> across the choice sets of each unit to RLH.csv in the working directory
#>
# Get predictions as a new tibble
predict(my_model, new_data = example_cjt_fp_drn_flat_data, type = "probability")
#> # A tibble: 30,348 × 1
#> .pred
#> <dbl>
#> 1 0.854
#> 2 0.0862
#> 3 0.0594
#> 4 0.852
#> 5 0.144
#> 6 0.00419
#> 7 0.677
#> 8 0.257
#> 9 0.0659
#> 10 0.783
#> # ℹ 30,338 more rows
# Add predictions to the input data
augment(my_model, new_data = example_cjt_fp_drn_flat_data, type = "choice")
#> # A tibble: 30,348 × 12
#> id weight country m3_customer m3_sub_interest task service scenario
#> <chr> <dbl> <fct> <fct> <fct> <dbl> <fct> <fct>
#> 1 R_0001 0.952 Italy Non-Customer Swing 1 No Service Scenari…
#> 2 R_0001 0.952 Italy Non-Customer Swing 1 AppleTV+ a… Scenari…
#> 3 R_0001 0.952 Italy Non-Customer Swing 1 Apple One … Scenari…
#> 4 R_0001 0.952 Italy Non-Customer Swing 2 No Service Scenari…
#> 5 R_0001 0.952 Italy Non-Customer Swing 2 AppleCare+… Scenari…
#> 6 R_0001 0.952 Italy Non-Customer Swing 2 Apple One … Scenari…
#> 7 R_0001 0.952 Italy Non-Customer Swing 3 No Service Scenari…
#> 8 R_0001 0.952 Italy Non-Customer Swing 3 Apple Music Scenari…
#> 9 R_0001 0.952 Italy Non-Customer Swing 3 Apple One … Scenari…
#> 10 R_0001 0.952 Italy Non-Customer Swing 4 No Service Scenari…
#> # ℹ 30,338 more rows
#> # ℹ 4 more variables: nn_choice <lgl>, drn_sub_buy_choice <lgl>,
#> # drn_2nd_dev_choice <lgl>, .pred <dbl>