Skip to contents

This function generates a bar plot visualizing the level importance of each attribute based on the provided model and cohorts. It uses different methods depending on the class of the input cohorts object.

Usage

plot_level_preference(
  model,
  cohorts = NULL,
  attributes = NULL,
  p_value = FALSE,
  relative = FALSE,
  text_position = "end",
  bar_color = (g6tr.theme::g6tr_pal("core"))(1),
  base_size = 30,
  ...
)

Arguments

model

A fitted conjoint task model object of class cjt_model

cohorts

A cohort or cohort_set object for analyzing specific respondent cohorts, or NULL for full dataset analysis. Create cohorts using create_cohort() or create_cohort_set().

attributes

Character. Attribute levels to show summary for. If set to NULL (default), all attribute are shown.

p_value

Logical. Show statistical significance?

relative

Logical. Show relative importance?

text_position

Character. One of "outside"(default), "beginning", "middle" and "end".

bar_color

Bar color.

base_size

Numeric. All text elements in the plot are scaled based on this size.

...

Additional arguments passed to g6tr.theme::g6tr_bar_plot().

Value

A ggplot object representing the bar plot of level importance.

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.360        0.391   0.223        0.41        0.45    0:02  
#>       200  0.301        0.507   0.435        1.71        1.19    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
#> 

# Plot attribute importance for the whole population
plot_level_preference(mod)
#> ℹ No weights provided. Using equal weights of 1.


# Show for only service attribute and make some advanced customization
plot_level_preference(mod, attributes = "service", highlight = "Apple Music", wrap_length = 60)
#> ℹ No weights provided. Using equal weights of 1.
#> Warning: Using provided subset of `attributes`: "service"
#> ! Attribute importance statistics will likely be misleading.


# Plot relative level importance for a specific cohort (Italy)
italy_cohort <-
  example_cjt_fp_drn_survey |>
  filter(country == "Italy") |>
  create_cohort(
    id_col = response_id,
    weight_col = weight
  )
plot_level_preference(mod, italy_cohort, relative = TRUE, text_position = "outside")


# Plot level importance for multiple cohorts
my_cohort_set <- create_cohort_set(
  example_cjt_fp_drn_survey,
  id_col = response_id,
  grp_cols = c(country, m3_customer),
  weight_col = weight
)
plot_level_preference(mod, my_cohort_set)
#> # A tibble: 5 × 5
#>   column      label              level        summaries        lvl_plot    
#>   <chr>       <chr>              <chr>        <list>           <named list>
#> 1 population  Overall population population   <tibble [2 × 6]> <ggplt2::>  
#> 2 country     NA                 Italy        <tibble [2 × 6]> <ggplt2::>  
#> 3 country     NA                 Spain        <tibble [2 × 6]> <ggplt2::>  
#> 4 m3_customer NA                 Customer     <tibble [2 × 6]> <ggplt2::>  
#> 5 m3_customer NA                 Non-Customer <tibble [2 × 6]> <ggplt2::>