File size: 1,794 Bytes
a7b5df0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ==============================================================================
# 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)
}