Skip to contents

Generates predicted choice probabilities for a set of concepts, aggregated across respondents within specified cohorts. This function allows for different ways of comparing concepts: as a single task, individually against a "none" option, or in separate tasks as specified by a column in the data.

Usage

compare_all_together(allow_none = FALSE)

compare_each_vs_none()

use_task_column(task_col, allow_none = FALSE)

predict_for_cohorts(
  model,
  concepts,
  task_spec = compare_all_together(),
  cohorts = NULL
)

Arguments

allow_none

Logical indicating whether to include a "none" option in the choice tasks. When TRUE, respondents can choose "none" instead of any concept.

task_col

Column name or expression identifying which concepts should be grouped together into choice tasks. Concepts with the same task value will be treated as alternatives in the same choice task.

model

A fitted cjt_model object containing respondent-level part-worth utilities

concepts

A dataframe where each row describes a concept (alternative) using the same predictors that were used in the model

task_spec

A task specification object defining how concepts should be compared. Create using compare_all_together() (default), compare_each_vs_none(), or use_task_column() (see details).

cohorts

A cohort, cohort_set, or NULL (for full population). Create cohorts using g6tr.cohorts::create_cohort() or g6tr.cohorts::create_cohort_set().

Value

  • For a single cohort: The original concepts dataframe with an additional probability column

  • For a cohort set: A modified cohort set with a new predictions column containing probability predictions for each cohort

Details

The function computes choice probabilities for each respondent in the specified cohort(s) and then averages these probabilities using respondent weights. There are three ways to specify how concepts should be compared using the task_spec parameter:

Task Specification Functions

  • compare_all_together(allow_none = FALSE): All concepts are treated as alternatives in a single choice task. Set allow_none = TRUE to include a "none" option.

  • compare_each_vs_none(): Each concept is evaluated separately against a "none" option.

  • use_task_column(task_col, allow_none = FALSE): Concepts are grouped into separate tasks based on the specified column. Set allow_none = TRUE to include a "none" option in each task.

Examples

library(dplyr)
library(g6tr.cohorts)
# Create a model
mod <- 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.362        0.390   0.222        0.41        0.45    0:02  

#>       200  0.301        0.507   0.434        1.60        1.16    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
#> 

# Create some concepts to evaluate
new_concepts <- tibble(
  service = c("Apple Music", "Apple One Premier", "Apple One Premier"),
  scenario = c("Scenario 1", "Scenario 1", "Scenario 2")
)

# Compare all concepts together (default)
pred_all <- predict_for_cohorts(mod, new_concepts)
#> ℹ No weights provided. Using equal weights of 1.

# Compare all concepts together, including "none" option
pred_all_none <- predict_for_cohorts(
  mod,
  new_concepts,
  task_spec = compare_all_together(allow_none = TRUE)
)
#> ℹ No weights provided. Using equal weights of 1.

# Compare each concept against "none"
pred_vs_none <- predict_for_cohorts(
  mod,
  new_concepts,
  task_spec = compare_each_vs_none()
)
#> ℹ No weights provided. Using equal weights of 1.

# Compare concepts in separate tasks
new_concepts_tasks <- new_concepts |>
  mutate(task_id = c(1, 1, 2))

pred_by_task <- predict_for_cohorts(
  mod,
  new_concepts_tasks,
  task_spec = use_task_column(task_id, allow_none = TRUE)
)
#> ℹ No weights provided. Using equal weights of 1.

# Predict for a specific cohort
italy_cohort <- example_cjt_fp_drn_survey |>
  filter(country == "Italy") |>
  create_cohort(id_col = response_id, weight_col = weight)

pred_italy <- predict_for_cohorts(mod, new_concepts, cohorts = italy_cohort)

# Predict for multiple cohorts
cohort_set <- example_cjt_fp_drn_survey |>
  create_cohort_set(
    id_col = response_id,
    grp_cols = c(country),
    weight_col = weight
  )

pred_by_country <- predict_for_cohorts(mod, new_concepts, cohorts = cohort_set)

filter(pred_by_country, level == "Spain")$predictions[[1]]
#> # A tibble: 3 × 4
#>    task service           scenario   probability
#>   <dbl> <chr>             <chr>            <dbl>
#> 1     1 Apple Music       Scenario 1       0.466
#> 2     1 Apple One Premier Scenario 1       0.272
#> 3     1 Apple One Premier Scenario 2       0.262