Skip to contents

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

Usage

plot_attribute_importance(
  model,
  cohorts = NULL,
  flipped = TRUE,
  text_position = "end",
  bar_order = "desc",
  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().

flipped

Logical. Should the graph be flipped? Default is FALSE.

text_position

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

bar_order

Character. How bars should be sorted. One of "none" (default), "asc" or "desc".

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 attribute 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.361        0.391   0.223        0.39        0.44    0:02  
#>       200  0.301        0.510   0.438        1.62        1.17    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_attribute_importance(mod)
#> ℹ No weights provided. Using equal weights of 1.


# Plot attribute 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_attribute_importance(mod, italy_cohort)


# Plot attribute 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_attribute_importance(mod, my_cohort_set)
#> # A tibble: 5 × 5
#>   column      label              level        summaries        attr_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::>