File size: 4,358 Bytes
62fe804
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
PlotVarianceDecomposition <- function(
    resultMxl,                # MIXL model (no mvar)
    resultMxlHier,            # MIXL-OH model (with mvar)
    resultCount,
    dataMlogit,
    segmentationVars = c("Usage_Medium", "Usage_High"),
    attributeLabel = NULL,
    wrapWidth = 30
){

  # 1. Estimated attributes
  estimatedItems <- resultCount %>%
    filter(Rank != max(Rank)) %>%
    pull(labelId) %>%
    as.character()

  # 2. Build the z_i matrix (one row per respondent)
  respondentZ <- dataMlogit %>%
    distinct(Id, .keep_all = TRUE) %>%
    select(Id, all_of(segmentationVars))

  zMatrix <- as.matrix(respondentZ[, segmentationVars, drop = FALSE])

  mxlCoef  <- summary(resultMxl)$CoefTable
  hierCoef <- summary(resultMxlHier)$CoefTable

  # 3. For each attribute, decompose variance.
  #    MIXL:    unobserved = (sd.<item>)^2
  #    MIXL-OH: observed   = Var across i of (sum_m gamma_jm * z_im)
  #             unobserved residual = (sd.<item>)^2
  decompList <- list()

  for (item in estimatedItems) {

    # 3.1 MIXL total variance (single component)
    sdName_mxl   <- paste0("sd.", item)
    sd_mxl       <- if (sdName_mxl %in% rownames(mxlCoef))  mxlCoef[sdName_mxl, "Estimate"]  else NA
    totalVar_mxl <- if (!is.na(sd_mxl)) sd_mxl ^ 2 else NA

    # 3.2 MIXL-OH unobserved residual variance
    sdName_hier   <- paste0("sd.", item)
    sd_hier       <- if (sdName_hier %in% rownames(hierCoef)) hierCoef[sdName_hier, "Estimate"] else NA
    unobsVar_hier <- if (!is.na(sd_hier)) sd_hier ^ 2 else NA

    # 3.3 MIXL-OH observed variance (from gamma * z_i)
    # gmnl names gamma coefficients as "item:segVar" (colon separator)
    gammaVec <- sapply(segmentationVars, function(sv) {
      cn <- paste0(item, ":", sv)   # gmnl uses colon separator
      if (cn %in% rownames(hierCoef)) hierCoef[cn, "Estimate"] else 0
    })

    linearPred  <- as.numeric(zMatrix %*% gammaVec)
    obsVar_hier <- var(linearPred)

    decompList[[length(decompList) + 1]] <- data.frame(
      labelId   = item,
      model     = c("MIXL",        "MIXL-OH",          "MIXL-OH"),
      component = c("Unobserved",  "Observed (Usage)", "Unobserved residual"),
      value     = c(totalVar_mxl,  obsVar_hier,        unobsVar_hier),
      stringsAsFactors = FALSE,
      row.names = NULL
    )
  }

  decompDf <- do.call(rbind, decompList)

  # 4. Human-readable labels
  if (!is.null(attributeLabel)) {
    decompDf <- decompDf %>%
      left_join(attributeLabel %>% select(labelId, label), by = "labelId") %>%
      mutate(displayLabel = ifelse(is.na(label), labelId, str_wrap(label, width = wrapWidth)))
  } else {
    decompDf$displayLabel <- decompDf$labelId
  }

  # 5. Order attributes by total MIXL-OH variance
  attrOrder <- decompDf %>%
    filter(model == "MIXL-OH") %>%
    group_by(displayLabel) %>%
    summarise(totVar = sum(value, na.rm = TRUE), .groups = "drop") %>%
    arrange(totVar) %>%
    pull(displayLabel)

  decompDf$displayLabel <- factor(decompDf$displayLabel, levels = attrOrder)
  decompDf$component <- factor(decompDf$component,
                               levels = c("Observed (Usage)", "Unobserved residual", "Unobserved"))
  decompDf$model     <- factor(decompDf$model, levels = c("MIXL", "MIXL-OH"))

  # 6. Plot: faceted stacked bars
  p <- ggplot(decompDf, aes(x = value, y = displayLabel, fill = component)) +
    geom_col(width = 0.72) +
    facet_wrap(~ model, ncol = 2) +
    scale_fill_manual(values = c(
      "Observed (Usage)"    = "#2E5077",
      "Unobserved residual" = "#D87C26",
      "Unobserved"          = "#9E9E9E"
    )) +
    labs(
      x = "Variance of preferences across the sample",
      y = "Attribute",
      fill = "Variance component",
      title = "Decomposition of preference heterogeneity",
      subtitle = "MIXL-OH absorbs part of the variance into observed heterogeneity (Usage)"
    ) +
    theme_minimal(base_size = 10) +
    theme(
      plot.title = element_text(face = "bold", size = 12),
      plot.subtitle = element_text(color = "grey40", size = 10),
      legend.position = "bottom",
      panel.grid.major.y = element_blank(),
      axis.text.y = element_text(size = 8, lineheight = 0.9),
      strip.text = element_text(face = "bold", size = 10),
      strip.background = element_rect(fill = "grey95", color = NA)
    )

  return(list(plot = p, data = decompDf))
}