# install.packages("readxl") #leer documentos de excel
library(readxl)
# install.packages("tidyverse") #manipular datos
library(tidyverse)
# install.packages("ggplot2") #gráficas
library(ggplot2)
# file.choose()
df<- read_excel("/Users/jorgearthur/Library/Mobile Documents/com~apple~CloudDocs/Clases/5to semestre/Otros/Bootcamp Programación/Base de datos.xlsx")
str(df)
## tibble [466,509 × 25] (S3: tbl_df/tbl/data.frame)
## $ ID : num [1:466509] 1 2 3 4 5 6 7 8 9 10 ...
## $ Año : num [1:466509] 2016 2016 2016 2016 2016 ...
## $ Territorio : chr [1:466509] "Guadalajara" "Guadalajara" "Guadalajara" "Guadalajara" ...
## $ Sub Territorio : chr [1:466509] "Belenes" "Belenes" "Belenes" "Belenes" ...
## $ CEDI : chr [1:466509] "Suc. Belenes" "Suc. Belenes" "Suc. Belenes" "Suc. Belenes" ...
## $ Cliente : chr [1:466509] "77737" "77737" "77737" "77737" ...
## $ Nombre : chr [1:466509] "ABARR" "ABARR" "ABARR" "ABARR" ...
## $ Tamaño Cte Industria: chr [1:466509] "Extra Grande" "Extra Grande" "Extra Grande" "Extra Grande" ...
## $ Segmento Det : chr [1:466509] "Agua Mineral" "Agua Purificada" "Agua Purificada" "Agua Saborizada" ...
## $ Marca : chr [1:466509] "Topo Chico A.M." "Ciel Agua Purificada" "Ciel Agua Purificada" "Ciel Exprim" ...
## $ Presentacion : chr [1:466509] "600 ml NR" "1 Ltro. N.R." "1.5 Lts. NR" "600 ml NR" ...
## $ Tamaño : chr [1:466509] "Individual" "Individual" "Individual" "Individual" ...
## $ Retornable_NR : chr [1:466509] "No Retornable" "No Retornable" "No Retornable" "No Retornable" ...
## $ Enero : num [1:466509] NA NA NA NA NA NA 1 NA 3 NA ...
## $ Febrero : num [1:466509] NA 2 NA NA NA NA NA 1 3 NA ...
## $ Marzo : num [1:466509] NA 8 3 NA NA 1 NA NA 4 NA ...
## $ Abril : num [1:466509] NA 4 6 NA NA NA NA 1 4 NA ...
## $ Mayo : num [1:466509] NA 4 3 NA NA NA 0 NA 4 NA ...
## $ Junio : num [1:466509] NA 2 3 NA NA NA NA 1 4 0 ...
## $ Julio : num [1:466509] NA 2 3 NA NA NA 0 NA 4 NA ...
## $ Agosto : num [1:466509] NA 2 3 NA NA NA NA 1 7 NA ...
## $ Septiembre : num [1:466509] NA 2 3 NA NA NA NA 1 4 NA ...
## $ Octubre : num [1:466509] NA 2 3 NA NA NA 0 NA 3 NA ...
## $ Noviembre : num [1:466509] NA 4 3 NA 0 NA NA NA 1 NA ...
## $ Diciembre : num [1:466509] 1 2 3 1 NA NA NA NA 3 NA ...
## Revisar NAs en la base de datos
# Cuántos NAs tengo en la base de datos?
sum(is.na(df))
## [1] 3148501
# Cuántos NAs tengo por variable?
sapply(df,function(x) sum(is.na(x)))
## ID Año Territorio
## 0 0 0
## Sub Territorio CEDI Cliente
## 0 0 0
## Nombre Tamaño Cte Industria Segmento Det
## 0 0 0
## Marca Presentacion Tamaño
## 0 0 0
## Retornable_NR Enero Febrero
## 0 233480 231213
## Marzo Abril Mayo
## 227420 224057 216910
## Junio Julio Agosto
## 215753 223411 220242
## Septiembre Octubre Noviembre
## 337314 338386 338460
## Diciembre
## 341855
# Opción 1. Eliminar NAs
# df1 <- na.omit(df)
# Opción 2. Reemplazar NAs con CEROS
df1 <- df
df1[is.na(df1)] <- 0
sum(is.na(df1))
## [1] 0
# Opción 3. Reemplazar NAs con PROMEDIO
# df1 <- df
#df1$Enero[is.na(df1$Enero)]<- mean(df1$Enero, na.rm=TRUE)
boxplot(df1$Enero)
# Eliminar renglón de totales
df1 <- df1[df1$Enero < 6000,]
# Adecuar nombres con gsub
names(df1) <- gsub(" ","_",names(df1))
#Muestra las Ventas de Enero a Junio por Cedi
df2<- select(df1,c(CEDI,Enero:Julio))
# Muestra los movimientos por cedi y tamaño de tienda
df3<- df1 %>% filter(Tamaño_Cte_Industria== "Grande")
# Ordena la base de datos por cedi, marca y presentación
df4 <- df1 %>% arrange (CEDI, Marca, Presentacion)
# Agrega un campo calculado con las ventas del primer semestre y muestra las ventas del primer semestre por marca.
df5 <- df1 %>% mutate(Ventas_Sem1 = Enero + Febrero + Marzo + Abril + Mayo + Junio)
Ventas_Sem1_por_marca <- df5 %>% group_by(Marca,Año) %>% summarise(Ventas_Sem1=sum(Ventas_Sem1))
#Obtén la media de las ventas del primer semestre agrupado por marca, presentación y tamaño.
df6 <- df5 %>% group_by(Marca, Presentacion, Tamaño) %>% summarise(Ventas_Sem1=mean(Ventas_Sem1))
# Calcular medidas de tendencia central
summary(df1)
## ID Año Territorio Sub_Territorio
## Min. : 1 Min. :2016 Length:466508 Length:466508
## 1st Qu.:116628 1st Qu.:2017 Class :character Class :character
## Median :233256 Median :2018 Mode :character Mode :character
## Mean :233255 Mean :2018
## 3rd Qu.:349882 3rd Qu.:2019
## Max. :466509 Max. :2019
## CEDI Cliente Nombre Tamaño_Cte_Industria
## Length:466508 Length:466508 Length:466508 Length:466508
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
## Segmento_Det Marca Presentacion Tamaño
## Length:466508 Length:466508 Length:466508 Length:466508
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
## Retornable_NR Enero Febrero Marzo
## Length:466508 Min. : -19.000 Min. : -11.000 Min. : -32.000
## Class :character 1st Qu.: 0.000 1st Qu.: 0.000 1st Qu.: 0.000
## Mode :character Median : 0.000 Median : 0.000 Median : 0.000
## Mean : 4.951 Mean : 4.829 Mean : 5.729
## 3rd Qu.: 2.000 3rd Qu.: 2.000 3rd Qu.: 3.000
## Max. :5333.000 Max. :4995.000 Max. :5636.000
## Abril Mayo Junio Julio
## Min. : -70.000 Min. :-106.00 Min. :-211.000 Min. : -60.000
## 1st Qu.: 0.000 1st Qu.: 0.00 1st Qu.: 0.000 1st Qu.: 0.000
## Median : 0.000 Median : 0.00 Median : 0.000 Median : 0.000
## Mean : 5.992 Mean : 6.73 Mean : 6.464 Mean : 6.033
## 3rd Qu.: 3.000 3rd Qu.: 3.00 3rd Qu.: 3.000 3rd Qu.: 3.000
## Max. :6164.000 Max. :6759.00 Max. :6033.000 Max. :6735.000
## Agosto Septiembre Octubre Noviembre
## Min. :-211.000 Min. :-527.000 Min. : -38.000 Min. : -25.00
## 1st Qu.: 0.000 1st Qu.: 0.000 1st Qu.: 0.000 1st Qu.: 0.00
## Median : 0.000 Median : 0.000 Median : 0.000 Median : 0.00
## Mean : 6.235 Mean : 3.625 Mean : 3.674 Mean : 3.57
## 3rd Qu.: 3.000 3rd Qu.: 1.000 3rd Qu.: 1.000 3rd Qu.: 1.00
## Max. :6065.000 Max. :6509.000 Max. :6326.000 Max. :5319.00
## Diciembre
## Min. : -28.000
## 1st Qu.: 0.000
## Median : 0.000
## Mean : 3.858
## 3rd Qu.: 0.000
## Max. :6182.000
# Ventas por tipo de envase
df7 <- df5 %>% group_by(Retornable_NR) %>% summarise((Ventas_Sem1=sum(Ventas_Sem1)))
# Ventas de tamaño tipo familiar
df8<- df5 %>% filter(Tamaño== "Familiar")
# Ventas por presentación
df9<- select(df5,c(Presentacion,Ventas_Sem1))
# Ventas segundo semestre
df10 <- df5 %>% mutate(Ventas_Sem2 = Julio + Agosto + Septiembre + Octubre + Noviembre + Diciembre)
# Ventas segundo semestre por marca
Ventas_Sem2_por_marca <- df10 %>% group_by(Marca) %>% summarise(Ventas_Sem2=sum(Ventas_Sem2))
# Ventas por presentación y tipo de envase
df11 <- df10 %>% arrange (CEDI, Presentacion, Retornable_NR)
# Colapsar meses en una Columna
df12 <- gather(df10, Mes, ventas_totales, Enero:Diciembre)
# agregar una columna con el número de mes
meses <- c("Enero"="01","Febrero"="02","Marzo"="03","Abril"="04","Mayo"="05","Junio"="06","Julio"="07","Agosto"="08","Septiembre"="09","Octubre"="10","Noviembre"="11","Diciembre"="12")
df12$Numero_de_Mes <- meses[df12$Mes]
# Graficar ventas por mes y año
ventas_totales <- df12 %>% group_by(Año,Numero_de_Mes) %>% summarise(Ventas_Totales = sum(ventas_totales)) %>% arrange(Numero_de_Mes)
## `summarise()` has grouped output by 'Año'. You can override using the `.groups`
## argument.
ggplot(ventas_totales, aes(x=Numero_de_Mes, y= Ventas_Totales, group=Año, color= as.factor(Año))) +
geom_line() +
geom_point() +
labs(x= "Mes", y="Ventas Totales(Qty)",color= "Año")
# Ventas Coca-Cola
df13 <- df12 %>% mutate(ventas_totales = Ventas_Sem1 + Ventas_Sem2)
df_coca_cola<- select(df13,c(Marca,Año, ventas_totales))
Ventas_totales_por_marca <- df_coca_cola %>% group_by(Marca,Año) %>% summarise(ventas_totales=sum(ventas_totales)) %>% arrange(Año)
## `summarise()` has grouped output by 'Marca'. You can override using the
## `.groups` argument.
Coca_Cola<- Ventas_totales_por_marca %>% filter(Marca== "Coca-Cola")
ggplot(Coca_Cola, aes(x=Año, y= ventas_totales, group=Marca)) +
geom_line() +
geom_point() +
labs(title = "Ventas Coca-Cola",x= "Año", y="Ventas Totales(Qty)")
# Ventas Retornable
df_retornables<- select(df13,c(Retornable_NR,Año, ventas_totales))
Ventas_retornables <- df_retornables %>% group_by(Retornable_NR,Año) %>% summarise(ventas_totales=sum(ventas_totales)) %>% arrange(Año)
## `summarise()` has grouped output by 'Retornable_NR'. You can override using the
## `.groups` argument.
ggplot(Ventas_retornables, aes(x=Año, y= ventas_totales, group=Retornable_NR, color= as.factor(Retornable_NR))) +
geom_line() +
geom_point() +
labs(title = "Ventas según su envase",x= "Año", y="Ventas Totales(Qty)",color= "Envase")
# Ventas Sucursal
df_sucursal<- select(df13,c(CEDI,Año, ventas_totales))
Ventas_sucursal <- df_sucursal %>% group_by(CEDI,Año) %>% summarise(ventas_totales=sum(ventas_totales)) %>% arrange(Año)
## `summarise()` has grouped output by 'CEDI'. You can override using the
## `.groups` argument.
ggplot(Ventas_sucursal, aes(x=Año, y= ventas_totales, group=CEDI, color= as.factor(CEDI))) +
geom_line() +
geom_point() +
labs(title = "Ventas según su sucursal",x= "Año", y="Ventas Totales(Qty)",color= "sucursal")
# Ventas según tamaño
df_tamaño<- select(df13,c(Tamaño,Año, ventas_totales))
Ventas_tamaño <- df_tamaño %>% group_by(Tamaño,Año) %>% summarise(ventas_totales=sum(ventas_totales)) %>% arrange(Año)
## `summarise()` has grouped output by 'Tamaño'. You can override using the
## `.groups` argument.
ggplot(Ventas_tamaño, aes(x=Año, y= ventas_totales, group=Tamaño, color= as.factor(Tamaño))) +
geom_line() +
geom_point() +
labs(title = "Ventas según su tamaño",x= "Año", y="Ventas Totales(Qty)",color= "Tamaño")
# Ventas según tamaño industria
df_tamaño_industria<- select(df13,c(Tamaño_Cte_Industria, ventas_totales))
Ventas_tamaño_industria <- df_tamaño_industria %>% group_by(Tamaño_Cte_Industria) %>% summarise(ventas_totales=sum(ventas_totales))
ggplot(Ventas_tamaño_industria, aes(x=Tamaño_Cte_Industria, y= ventas_totales, group=Tamaño_Cte_Industria, color= as.factor(Tamaño_Cte_Industria))) +
geom_col() +
labs(title = "Ventas según su tamaño",x= "Año", y="Ventas Totales(Qty)",color= "Tamaño")
# Elaborar regresión lineal
ventas_4567 <- df13 %>% filter(Cliente== "4567")%>% group_by(Año) %>% summarise(ventas_totales=sum(ventas_totales))%>% filter(ventas_totales != 0)
# ventas_4567$Secuencia <- 1:nrow(ventas_4567)
ggplot(ventas_4567, aes(x=Año, y= ventas_totales)) +
geom_line() +
geom_point() +
labs(title = "Ventas cliente 4567",x= "Año", y="Ventas Totales(Qty)")
regresion <- lm(ventas_totales ~ Año, data=ventas_4567)
summary(regresion)
##
## Call:
## lm(formula = ventas_totales ~ Año, data = ventas_4567)
##
## Residuals:
## 1 2 3
## -61108 122216 -61108
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 587024128 213589498 2.748 0.222
## Año -290088 105842 -2.741 0.223
##
## Residual standard error: 149700 on 1 degrees of freedom
## Multiple R-squared: 0.8825, Adjusted R-squared: 0.765
## F-statistic: 7.512 on 1 and 1 DF, p-value: 0.2227
# Ecuación
# y= 48918677 + (-24174) * Año
# R cuadrada ajustada
# 77%
datos<- data.frame(Año=2020:2025)
prediccion<-predict(regresion,datos)
prediccion
## 1 2 3 4 5 6
## 1046368 756280 466192 176104 -113984 -404072
ventas_4567$Tipo_de_Dato <- "Datos Reales"
datos$ventas_totales <- prediccion
datos$Tipo_de_Dato <- "Predicción"
datos_combinados <- rbind(ventas_4567,datos)
ggplot(datos_combinados, aes(x=Año, y= ventas_totales, color=Tipo_de_Dato)) +
geom_point() +
labs(title = "Ventas cliente 4567 a 5 años",x= "Año", y="Ventas Totales(Qty)")