Spaces:
Sleeping
Sleeping
Download Functions/SegmentationAnalysis.R from rodrigoheldt/bws_case1_app: direct link, hf CLI and curl.
- Browser
- Download file 1.79 kB
-
https://huggingface.co/spaces/rodrigoheldt/bws_case1_app/resolve/main/Functions/SegmentationAnalysis.R
- Command line
-
hf download hf://spaces/rodrigoheldt/bws_case1_app/Functions/SegmentationAnalysis.R
-
curl -L -o SegmentationAnalysis.R https://huggingface.co/spaces/rodrigoheldt/bws_case1_app/resolve/main/Functions/SegmentationAnalysis.R
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) | |
| } | |