bws_case1_app / Functions /SegmentationAnalysis.R
rodrigoheldt
Deploy MaxDiff Best-Worst Scaling app
a7b5df0
Raw History Blame Contribute Delete
1.79 kB
# ==============================================================================
# SegmentationAnalysis.R – Cópia idêntica à versão original (Sandbox)
# Cruza os parâmetros individuais do MXL (indpar) com uma variável de
# segmentação demográfica/comportamental e gera gráfico de barras.
# ==============================================================================
SegmentationAnalysis <- function(
indpar = resultMxl$indpar,
indCharacteristics = indCharacteristics,
segmentationVar = "Usage"
){
segmentationAnalysis <- indpar %>%
left_join(
indCharacteristics %>%
select(Id, !!sym(segmentationVar)),
by = c("id" = "Id")
) %>%
group_by(!!sym(segmentationVar)) %>%
summarise_at(
vars(starts_with("T")), mean
) %>%
select(!!sym(segmentationVar), mixedsort((colnames(indpar %>% select(-id))))) %>%
pivot_longer(
cols = -1,
names_to = "Attribute",
values_to = "Estimate"
) %>%
mutate(
Attribute = factor(
x = Attribute,
levels = mixedsort((colnames(indpar %>% select(-id))))
)
)
p <- ggplot(
data = segmentationAnalysis %>%
rename(segVar = !!sym(segmentationVar)),
aes(
x = Attribute,
y = Estimate,
fill = segVar
)
) +
geom_col(position = "dodge", color = "black") +
scale_fill_grey(start = 0.8, end = 0.2) +
labs(
title = paste0("Preferences by ", segmentationVar),
x = "Attributes",
y = "Preference",
fill = segmentationVar
) +
theme_minimal()
png(
filename = paste0("Results/segmentation_", segmentationVar, ".png"),
width = 924,
height = 378
)
print(p)
dev.off()
return(segmentationAnalysis)
}