PASO 1. Cargar librerías
library(readxl) # Lectura de Excel
library(dplyr) # Manipulación de datos
library(tidyr) # Transformación de datos (pivot, etc.)
library(ggplot2) # Visualizaciones
library(plotly) # Gráficos interactivos
library(knitr) # Tablas e imágenes
library(kableExtra) # Formato de tablas
library(scales) # Formato de ejes ($, %)
library(corrplot) # Matriz de correlación
PASO 2. Cargar y explorar los datos
DatosCaso1_1_ <- read_excel("~/Jave Universidad COL/Semestre 2026-2/Analisis de Datos de Toma de Decisiones/Archivos R/DatosCaso1 (1).xlsx")
View(DatosCaso1_1_)
df <- DatosCaso1_1_
head(df)
## # A tibble: 6 × 11
## distribuidor region estado ciudad producto precio_unidad unidades_vendidas
## <chr> <chr> <chr> <chr> <chr> <dbl> <dbl>
## 1 Foot Locker Northeast New Yo… New Y… Men's S… 50 1200
## 2 Foot Locker Northeast New Yo… New Y… Men's A… 50 1000
## 3 Foot Locker Northeast New Yo… New Y… Women's… 40 1000
## 4 Foot Locker Northeast New Yo… New Y… Women's… 45 850
## 5 Foot Locker Northeast New Yo… New Y… Men's A… 60 900
## 6 Foot Locker Northeast New Yo… New Y… Women's… 50 1000
## # ℹ 4 more variables: ventas_total <dbl>, utilidad_operativa <dbl>,
## # margen_operativo <dbl>, metodo_venta <chr>
dim(df)
## [1] 9648 11
names(df)
## [1] "distribuidor" "region" "estado"
## [4] "ciudad" "producto" "precio_unidad"
## [7] "unidades_vendidas" "ventas_total" "utilidad_operativa"
## [10] "margen_operativo" "metodo_venta"
PASO 3. Indicadores estadísticos básicos
Medidas de tendencia central y de variabilidad de las cinco variables numéricas clave.
variables_numericas <- df %>%
select(precio_unidad,
unidades_vendidas,
ventas_total,
utilidad_operativa,
margen_operativo)
indicadores <- data.frame(
Variable = names(variables_numericas),
Media = round(sapply(variables_numericas, mean), 2),
Mediana = round(sapply(variables_numericas, median), 2),
Desviacion_Estandar = round(sapply(variables_numericas, sd), 2),
Minimo = sapply(variables_numericas, min),
Maximo = sapply(variables_numericas, max),
CV_pct = round(sapply(variables_numericas, function(x) sd(x) / mean(x) * 100), 2)
)
kable(indicadores, row.names = FALSE,
caption = "Indicadores estadísticos básicos (CV% = coeficiente de variación)") %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"), full_width = FALSE)
| Variable | Media | Mediana | Desviacion_Estandar | Minimo | Maximo | CV_pct |
|---|---|---|---|---|---|---|
| precio_unidad | 45.22 | 45.00 | 14.71 | 7.0 | 110.0 | 32.52 |
| unidades_vendidas | 256.93 | 176.00 | 214.25 | 0.0 | 1275.0 | 83.39 |
| ventas_total | 12455.08 | 7803.50 | 12716.39 | 0.0 | 82500.0 | 102.10 |
| utilidad_operativa | 4894.79 | 3262.98 | 4866.46 | 0.0 | 39000.0 | 99.42 |
| margen_operativo | 0.42 | 0.41 | 0.10 | 0.1 | 0.8 | 22.98 |
PASO 4. Análisis de variables categóricas
table(df$region)
##
## Midwest Northeast South Southeast West
## 1872 2376 1728 1224 2448
table(df$ciudad)
##
## Albany Albuquerque Anchorage Atlanta Baltimore
## 144 216 144 216 144
## Billings Birmingham Boise Boston Burlington
## 144 216 216 216 216
## Charleston Charlotte Cheyenne Chicago Columbus
## 288 144 144 144 144
## Dallas Denver Des Moines Detroit Fargo
## 216 144 144 144 144
## Hartford Honolulu Houston Indianapolis Jackson
## 216 144 216 144 216
## Knoxville Las Vegas Little Rock Los Angeles Louisville
## 216 216 216 216 144
## Manchester Miami Milwaukee Minneapolis New Orleans
## 216 144 144 144 216
## New York Newark Oklahoma City Omaha Orlando
## 216 144 216 144 216
## Philadelphia Phoenix Portland Providence Richmond
## 216 216 360 216 216
## Salt Lake City San Francisco Seattle Sioux Falls St. Louis
## 216 216 144 144 144
## Wichita Wilmington
## 144 144
table(df$metodo_venta)
##
## In-store Online Outlet
## 1740 4889 3019
table(df$producto)
##
## Men's Apparel Men's Athletic Footwear Men's Street Footwear
## 1606 1610 1610
## Women's Apparel Women's Athletic Footwear Women's Street Footwear
## 1608 1606 1608
p_region <- ggplot(df, aes(x = region)) +
geom_bar(fill = "#1D1D2C", color = "#E63946") +
labs(title = "Cantidad de registros por región",
x = "Región", y = "Cantidad de registros") +
scale_y_continuous(labels = scales::comma) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
ggplotly(p_region)
ggplot(df, aes(x = metodo_venta)) +
geom_bar(fill = "#E63946", color = "#1D1D2C") +
labs(title = "Registros según método de venta",
x = "Método de venta", y = "Cantidad de registros") +
scale_y_continuous(labels = scales::comma) +
theme_minimal()
PASO 5. Distribución de las variables numéricas
Histogramas de las cinco variables numéricas (cada uno se muestra por separado para verlos con mayor detalle).
g1 <- ggplot(df, aes(x = precio_unidad)) +
geom_histogram(bins = 30, fill = "#2EC4B6") +
labs(title = "Distribución del precio por unidad", x = "Precio por unidad", y = "Frecuencia") +
scale_x_continuous(labels = scales::dollar) +
theme_minimal()
g2 <- ggplot(df, aes(x = unidades_vendidas)) +
geom_histogram(bins = 30, fill = "#F4A261") +
labs(title = "Distribución de unidades vendidas", x = "Unidades vendidas", y = "Frecuencia") +
scale_x_continuous(labels = scales::comma) +
theme_minimal()
g3 <- ggplot(df, aes(x = ventas_total)) +
geom_histogram(bins = 30, fill = "#E63946") +
labs(title = "Distribución de ventas totales", x = "Ventas totales", y = "Frecuencia") +
scale_x_continuous(labels = scales::dollar) +
theme_minimal()
g4 <- ggplot(df, aes(x = utilidad_operativa)) +
geom_histogram(bins = 30, fill = "#1D1D2C") +
labs(title = "Distribución de la utilidad operativa", x = "Utilidad operativa", y = "Frecuencia") +
scale_x_continuous(labels = scales::dollar) +
theme_minimal()
g5 <- ggplot(df, aes(x = margen_operativo)) +
geom_histogram(bins = 30, fill = "#8D99AE") +
labs(title = "Distribución del margen operativo", x = "Margen operativo", y = "Frecuencia") +
scale_x_continuous(labels = scales::percent) +
theme_minimal()
g1
g2
g3
g4
g5
Diagramas de caja de las mismas cinco variables, útiles para detectar valores atípicos.
b1 <- ggplot(df, aes(y = precio_unidad)) + geom_boxplot(fill = "#2EC4B6", color = "#1D1D2C") +
labs(title = "Precio por unidad", y = "Precio") +
scale_y_continuous(labels = scales::dollar) + theme_minimal()
b2 <- ggplot(df, aes(y = unidades_vendidas)) + geom_boxplot(fill = "#F4A261", color = "#1D1D2C") +
labs(title = "Unidades vendidas", y = "Unidades") +
scale_y_continuous(labels = scales::comma) + theme_minimal()
b3 <- ggplot(df, aes(y = ventas_total)) + geom_boxplot(fill = "#E63946", color = "#1D1D2C") +
labs(title = "Ventas totales", y = "Ventas") +
scale_y_continuous(labels = scales::dollar) + theme_minimal()
b4 <- ggplot(df, aes(y = utilidad_operativa)) + geom_boxplot(fill = "#1D1D2C", color = "#E63946") +
labs(title = "Utilidad operativa", y = "Utilidad") +
scale_y_continuous(labels = scales::dollar) + theme_minimal()
b5 <- ggplot(df, aes(y = margen_operativo)) + geom_boxplot(fill = "#8D99AE", color = "#1D1D2C") +
labs(title = "Margen operativo", y = "Margen") +
scale_y_continuous(labels = scales::percent) + theme_minimal()
b1
b2
b3
b4
b5
PASO 6. Comparación de ventas por categoría
ventas_region <- df %>%
group_by(region) %>%
summarise(
ventas_totales = sum(ventas_total, na.rm = TRUE),
utilidad_total = sum(utilidad_operativa, na.rm = TRUE),
unidades = sum(unidades_vendidas, na.rm = TRUE),
margen_prom = mean(margen_operativo, na.rm = TRUE)
) %>%
arrange(desc(ventas_totales))
kable(ventas_region, caption = "Ventas, utilidad y margen promedio por región") %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"), full_width = FALSE)
| region | ventas_totales | utilidad_total | unidades | margen_prom |
|---|---|---|---|---|
| West | 36436157 | 13017584 | 686985 | 0.3966912 |
| Northeast | 25078267 | 9732774 | 501279 | 0.4104503 |
| Southeast | 21374436 | 8393059 | 407000 | 0.4191667 |
| South | 20603356 | 9221605 | 492260 | 0.4668981 |
| Midwest | 16674434 | 6859945 | 391337 | 0.4352724 |
p_ventas_region <- ggplot(ventas_region, aes(x = reorder(region, -ventas_totales), y = ventas_totales)) +
geom_col(fill = "#1D1D2C") +
labs(title = "Ventas totales por región", x = "Región", y = "Ventas totales") +
scale_y_continuous(labels = scales::dollar) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
ggplotly(p_ventas_region)
ventas_metodo <- df %>%
group_by(metodo_venta) %>%
summarise(
ventas_totales = sum(ventas_total, na.rm = TRUE),
utilidad_total = sum(utilidad_operativa, na.rm = TRUE),
margen_prom = mean(margen_operativo, na.rm = TRUE)
) %>%
arrange(desc(ventas_totales))
kable(ventas_metodo, caption = "Ventas, utilidad y margen promedio por método de venta") %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"), full_width = FALSE)
| metodo_venta | ventas_totales | utilidad_total | margen_prom |
|---|---|---|---|
| Online | 44965657 | 19552538 | 0.4641522 |
| Outlet | 39536618 | 14913301 | 0.3948758 |
| In-store | 35664375 | 12759129 | 0.3561207 |
ggplot(ventas_metodo, aes(x = reorder(metodo_venta, -ventas_totales), y = ventas_totales)) +
geom_col(fill = "#E63946") +
labs(title = "Ventas totales por método de venta", x = "Método de venta", y = "Ventas totales") +
scale_y_continuous(labels = scales::dollar) +
theme_minimal()
Ventas totales por región y método de venta combinados (combinación producto+método que sugiere la guía, aplicada aquí a región+método):
ventas_region_metodo <- df %>%
group_by(region, metodo_venta) %>%
summarise(ventas_totales = sum(ventas_total, na.rm = TRUE), .groups = "drop")
# Tabla cruzada región x método (una fila por región, una columna por método)
tabla_region_metodo <- ventas_region_metodo %>%
pivot_wider(names_from = metodo_venta, values_from = ventas_totales)
kable(tabla_region_metodo, caption = "Tabla cruzada: ventas totales por región y método de venta") %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"), full_width = FALSE)
| region | In-store | Online | Outlet |
|---|---|---|---|
| Midwest | 5955400 | 7119734 | 3599300 |
| Northeast | 11595075 | 4626777 | 8856415 |
| South | 339375 | 9201010 | 11062971 |
| Southeast | 7236125 | 12067160 | 2071151 |
| West | 10538400 | 11950976 | 13946781 |
ggplot(ventas_region_metodo, aes(x = region, y = ventas_totales, fill = metodo_venta)) +
geom_col(position = "dodge") +
labs(title = "Ventas totales por región y método de venta",
x = "Región", y = "Ventas totales", fill = "Método de venta") +
scale_fill_manual(values = c("In-store" = "#F4A261", "Online" = "#E63946", "Outlet" = "#2EC4B6")) +
scale_y_continuous(labels = scales::dollar) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
PASO 7. Análisis de correlación
La correlación de Pearson permite observar la intensidad y dirección de la relación lineal entre dos variables numéricas. Este análisis no demuestra causalidad.
matriz_correlacion <- cor(variables_numericas, use = "complete.obs", method = "pearson")
kable(round(matriz_correlacion, 2), caption = "Matriz de correlación de Pearson") %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"), full_width = FALSE)
| precio_unidad | unidades_vendidas | ventas_total | utilidad_operativa | margen_operativo | |
|---|---|---|---|---|---|
| precio_unidad | 1.00 | 0.27 | 0.54 | 0.50 | -0.14 |
| unidades_vendidas | 0.27 | 1.00 | 0.92 | 0.87 | -0.31 |
| ventas_total | 0.54 | 0.92 | 1.00 | 0.94 | -0.30 |
| utilidad_operativa | 0.50 | 0.87 | 0.94 | 1.00 | -0.05 |
| margen_operativo | -0.14 | -0.31 | -0.30 | -0.05 | 1.00 |
corrplot(matriz_correlacion, method = "color", type = "upper",
addCoef.col = "black", tl.col = "black", tl.srt = 45,
col = colorRampPalette(c("#1D1D2C", "white", "#E63946"))(200),
title = "Matriz de correlación", mar = c(0, 0, 2, 0))
Correlación entre unidades vendidas y ventas totales:
ggplot(df, aes(x = unidades_vendidas, y = ventas_total)) +
geom_point(alpha = 0.35, color = "#1D1D2C") +
geom_smooth(method = "lm", se = FALSE, color = "#E63946") +
labs(title = "Relación entre unidades vendidas y ventas totales",
x = "Unidades vendidas", y = "Ventas totales") +
scale_x_continuous(labels = scales::comma) +
scale_y_continuous(labels = scales::dollar) +
theme_minimal()
cor.test(df$unidades_vendidas, df$ventas_total, method = "pearson")
##
## Pearson's product-moment correlation
##
## data: df$unidades_vendidas and df$ventas_total
## t = 229.48, df = 9646, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.9161919 0.9223725
## sample estimates:
## cor
## 0.9193389
Correlación entre ventas totales y utilidad operativa:
p_vu <- ggplot(df, aes(x = ventas_total, y = utilidad_operativa)) +
geom_point(alpha = 0.35, color = "#1D1D2C") +
geom_smooth(method = "lm", se = FALSE, color = "#E63946") +
labs(title = "Relación entre ventas totales y utilidad operativa",
x = "Ventas totales", y = "Utilidad operativa") +
scale_x_continuous(labels = scales::dollar) +
scale_y_continuous(labels = scales::dollar) +
theme_minimal()
ggplotly(p_vu)
cor.test(df$ventas_total, df$utilidad_operativa, method = "pearson")
##
## Pearson's product-moment correlation
##
## data: df$ventas_total and df$utilidad_operativa
## t = 259.76, df = 9646, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.9328283 0.9378218
## sample estimates:
## cor
## 0.9353717
Correlación entre precio por unidad y unidades vendidas:
p_pu <- ggplot(df, aes(x = precio_unidad, y = unidades_vendidas)) +
geom_point(alpha = 0.35, color = "#1D1D2C") +
geom_smooth(method = "lm", se = FALSE, color = "#E63946") +
labs(title = "Relación entre precio por unidad y unidades vendidas",
x = "Precio por unidad", y = "Unidades vendidas") +
scale_x_continuous(labels = scales::dollar) +
scale_y_continuous(labels = scales::comma) +
theme_minimal()
ggplotly(p_pu)
cor.test(df$precio_unidad, df$unidades_vendidas, method = "pearson")
##
## Pearson's product-moment correlation
##
## data: df$precio_unidad and df$unidades_vendidas
## t = 27.087, df = 9646, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.2472257 0.2843146
## sample estimates:
## cor
## 0.2658685
PASO 8. Conclusiones descriptivas
A partir de los resultados obtenidos, se deben identificar: