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.)^2 # MIXL-OH: observed = Var across i of (sum_m gamma_jm * z_im) # unobserved residual = (sd.)^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)) }