# install.packages("readxl") #leer documentos de excel
library(readxl)
# install.packages("tidyverse") #manipular datos
library(tidyverse)
# install.packages("ggplot2") #gráficas
library(ggplot2)
#install.packages("forecast") #Sereies de Tiempo y pronósticos
library(forecast)
# 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, 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)) %>% 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))
Ventas_totales_por_marca <- df_coca_cola %>% group_by(Marca,Año) %>% summarise(ventas_totales=sum(Ventas)) %>% 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))%>% 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
## -5092 10185 -5092
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 48918677 17799125 2.748 0.222
## Año -24174 8820 -2.741 0.223
##
## Residual standard error: 12470 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
## 87197.333 63023.333 38849.333 14675.333 -9498.667 -33672.667
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)")
Mejores_clientes <- df13 %>% group_by(Cliente, Año) %>% summarise(ventas_totales=sum(ventas_totales)) %>% arrange(ventas_totales)
## `summarise()` has grouped output by 'Cliente'. You can override using the
## `.groups` argument.
# Elaborar regresión lineal
ventas_6114 <- df13 %>% filter(Cliente== "6114")%>% group_by(Año) %>% summarise(ventas_totales=sum(ventas_totales))%>% filter(ventas_totales != 0)
# ventas_77796$Secuencia <- 1:nrow(ventas_77796)
ggplot(ventas_6114, aes(x=Año, y= ventas_totales)) +
geom_line() +
geom_point() +
labs(title = "Ventas cliente 6114",x= "Año", y="Ventas Totales(Qty)")
regresion <- lm(ventas_totales ~ Año, data=ventas_6114)
summary(regresion)
##
## Call:
## lm(formula = ventas_totales ~ Año, data = ventas_6114)
##
## Residuals:
## 1 2 3
## -43248 86496 -43248
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 230393988 151163818 1.524 0.37
## Año -113856 74908 -1.520 0.37
##
## Residual standard error: 105900 on 1 degrees of freedom
## Multiple R-squared: 0.6979, Adjusted R-squared: 0.3958
## F-statistic: 2.31 on 1 and 1 DF, p-value: 0.3705
# 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
## 404868 291012 177156 63300 -50556 -164412
ventas_6114$Tipo_de_Dato <- "Datos Reales"
Datos$ventas_totales <- prediccion
Datos$Tipo_de_Dato <- "Predicción"
Datos_combinados <- rbind(ventas_6114,Datos)
ggplot(Datos_combinados, aes(x=Año, y= ventas_totales, color=Tipo_de_Dato)) +
geom_point() +
labs(title = "Ventas cliente 6114 a 5 años",x= "Año", y="Ventas Totales(Qty)")
# Elaborar regresión lineal
ggplot(Coca_Cola, aes(x=Año, y= ventas_totales)) +
geom_line() +
geom_point() +
labs(title = "Ventas Coca Cola",x= "Año", y="Ventas Totales(Qty)")
regresion <- lm(ventas_totales ~ Año, data=Coca_Cola)
summary(regresion)
##
## Call:
## lm(formula = ventas_totales ~ Año, data = Coca_Cola)
##
## Residuals:
## 1 2 3 4
## 26521 190842 -461246 243883
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -766055930 354845176 -2.159 0.164
## Año 382384 175884 2.174 0.162
##
## Residual standard error: 393300 on 2 degrees of freedom
## Multiple R-squared: 0.7027, Adjusted R-squared: 0.554
## F-statistic: 4.727 on 1 and 2 DF, p-value: 0.1617
# Ecuación
# y= -9.193e+09 + (4.589e+06) * Año
# R cuadrada ajustada
# 55%
datos<- data.frame(Año=2020:2025)
prediccion<-predict(regresion,datos)
prediccion
## 1 2 3 4 5 6
## 6358740 6741124 7123508 7505891 7888274 8270658
Coca_Cola$Tipo_de_Dato <- "Datos Reales"
datos$ventas_totales <- prediccion
datos$Tipo_de_Dato <- "Predicción"
datos_combinados <- rbind(Coca_Cola,datos)
ggplot(datos_combinados, aes(x=Año, y= ventas_totales, color=Tipo_de_Dato)) +
geom_point() +
labs(title = "Ventas de Coca Cola a 5 años",x= "Año", y="Ventas Totales(Qty)")
# Elaborar regresión lineal
ventas_Huentitán <- Ventas_sucursal %>% filter(CEDI== "Suc. Huentitán")
ggplot(ventas_Huentitán, aes(x=Año, y= ventas_totales)) +
geom_line() +
geom_point() +
labs(title = "Ventas de Sucursal Huentitán",x= "Año", y="Ventas Totales(Qty)")
regresion <- lm(ventas_totales ~ Año, data=ventas_Huentitán)
summary(regresion)
##
## Call:
## lm(formula = ventas_totales ~ Año, data = ventas_Huentitán)
##
## Residuals:
## 1 2 3 4
## 3070121 -2711204 -3787954 3429037
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -385274928 4177889972 -0.092 0.935
## Año 202081 2070825 0.098 0.931
##
## Residual standard error: 4631000 on 2 degrees of freedom
## Multiple R-squared: 0.004739, Adjusted R-squared: -0.4929
## F-statistic: 0.009523 on 1 and 2 DF, p-value: 0.9312
# Ecuación
# y= -9.193e+09 + (4.589e+06) * Año
# R cuadrada ajustada
# 55%
datos<- data.frame(Año=2020:2025)
prediccion<-predict(regresion,datos)
prediccion
## 1 2 3 4 5 6
## 22929096 23131177 23333258 23535340 23737421 23939502
ventas_Huentitán$Tipo_de_Dato <- "Datos Reales"
datos$ventas_totales <- prediccion
datos$Tipo_de_Dato <- "Predicción"
datos_combinados <- rbind(ventas_Huentitán,datos)
ggplot(datos_combinados, aes(x=Año, y= ventas_totales, color=Tipo_de_Dato)) +
geom_point() +
labs(title = "Ventas de Sucursal Huentitán a 5 años",x= "Año", y="Ventas Totales(Qty)")
ventas_Belenes <- Ventas_sucursal %>% filter(CEDI== "Suc. Belenes")
ggplot(ventas_Belenes, aes(x=Año, y= ventas_totales)) +
geom_line() +
geom_point() +
labs(title = "entas de Sucursal Belenes",x= "Año", y="Ventas Totales(Qty)")
regresion <- lm(ventas_totales ~ Año, data=ventas_Belenes)
summary(regresion)
##
## Call:
## lm(formula = ventas_totales ~ Año, data = ventas_Belenes)
##
## Residuals:
## 1 2 3 4
## 2538620 1071029 -9757919 6148270
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.976e+10 7.565e+09 -2.612 0.121
## Año 9.809e+06 3.750e+06 2.616 0.120
##
## Residual standard error: 8385000 on 2 degrees of freedom
## Multiple R-squared: 0.7738, Adjusted R-squared: 0.6607
## F-statistic: 6.842 on 1 and 2 DF, p-value: 0.1203
# Ecuación
# y= -9.193e+09 + (4.589e+06) * Año
# R cuadrada ajustada
# 55%
datos<- data.frame(Año=2020:2025)
prediccion<-predict(regresion,datos)
prediccion
## 1 2 3 4 5 6
## 52050486 61859146 71667805 81476465 91285124 101093784
ventas_Belenes$Tipo_de_Dato <- "Datos Reales"
datos$ventas_totales <- prediccion
datos$Tipo_de_Dato <- "Predicción"
datos_combinados <- rbind(ventas_Belenes,datos)
ggplot(datos_combinados, aes(x=Año, y= ventas_totales, color=Tipo_de_Dato)) +
geom_point() +
labs(title = "Ventas de Sucursal Belenes a 5 años",x= "Año", y="Ventas Totales(Qty)")
ventas_mensuales_4567 <- df13 %>% filter(Cliente== "4567")%>% group_by(Año,Numero_de_Mes) %>%summarise(ventas_totales=sum(Ventas))%>% filter(ventas_totales != 0)
# Confirmar que los datos esten ordenados cronológicamente
# Funcion de serie de tiempo MENSUAL, que inicia en ENERO de 2017
ts <- ts(data=ventas_mensuales_4567$ventas_totales, start = c(2017,1), frequency = 12)
# Funcion de serie de tiempo MENSUAL, que inicia en ABRIL de 2017
# ts <- ts(data=ventas_mensuales_4567$ventas_totales, start = c(2017,4), frecuency = 12)
# Funcion de serie de tiempo TRIMESTRAL, que inicia en OCTUBRE de 2017 (Q4)
# ts <- ts(data=ventas_mensuales_4567$ventas_totales, start = c(2017,4), frecuency = 4)
# Funcion de serie de tiempo ANUAL, que inicia en 2017
# ts <- ts(data=ventas_mensuales_4567$ventas_totales, start = 2017, frecuency = 1)
# Crear modelo ARIMA
# Modelo Autoregresivo Integrado de Promedio Movil
arima <- auto.arima(ts, D=1) # D=1 por la temporalidad.
arima
## Series: ts
## ARIMA(0,0,0)(0,1,0)[12]
##
## sigma^2 = 10383172: log likelihood = -189.94
## AIC=381.87 AICc=382.09 BIC=382.87
summary(arima)
## Series: ts
## ARIMA(0,0,0)(0,1,0)[12]
##
## sigma^2 = 10383172: log likelihood = -189.94
## AIC=381.87 AICc=382.09 BIC=382.87
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set -36.98041 2547.446 1572.02 -1.811657 13.08459 0.6269271 0.2519497
# Generar pronóstico de ventas
pronostico <- forecast(arima, level=95, h=36) # Level es el nivel de confibilidad y h es el tiempo a pronosticar
pronostico
## Point Forecast Lo 95 Hi 95
## Sep 2019 12086 5770.4219 18401.58
## Oct 2019 11427 5111.4219 17742.58
## Nov 2019 11270 4954.4219 17585.58
## Dec 2019 12227 5911.4219 18542.58
## Jan 2020 9430 3114.4219 15745.58
## Feb 2020 11312 4996.4219 17627.58
## Mar 2020 12515 6199.4219 18830.58
## Apr 2020 13334 7018.4219 19649.58
## May 2020 16286 9970.4219 22601.58
## Jun 2020 15347 9031.4219 21662.58
## Jul 2020 14005 7689.4219 20320.58
## Aug 2020 14050 7734.4219 20365.58
## Sep 2020 12086 3154.4238 21017.58
## Oct 2020 11427 2495.4238 20358.58
## Nov 2020 11270 2338.4238 20201.58
## Dec 2020 12227 3295.4238 21158.58
## Jan 2021 9430 498.4238 18361.58
## Feb 2021 11312 2380.4238 20243.58
## Mar 2021 12515 3583.4238 21446.58
## Apr 2021 13334 4402.4238 22265.58
## May 2021 16286 7354.4238 25217.58
## Jun 2021 15347 6415.4238 24278.58
## Jul 2021 14005 5073.4238 22936.58
## Aug 2021 14050 5118.4238 22981.58
## Sep 2021 12086 1147.0979 23024.90
## Oct 2021 11427 488.0979 22365.90
## Nov 2021 11270 331.0979 22208.90
## Dec 2021 12227 1288.0979 23165.90
## Jan 2022 9430 -1508.9021 20368.90
## Feb 2022 11312 373.0979 22250.90
## Mar 2022 12515 1576.0979 23453.90
## Apr 2022 13334 2395.0979 24272.90
## May 2022 16286 5347.0979 27224.90
## Jun 2022 15347 4408.0979 26285.90
## Jul 2022 14005 3066.0979 24943.90
## Aug 2022 14050 3111.0979 24988.90
plot(pronostico, main="Ventas Mensuales y Pronóstico a 3 años del Cliente 4567", xlab="Año", ylaab="Ventas (Qty)")
# Elaborar regresión lineal
Familia_Coca_Cola <- Ventas_totales_por_marca %>% filter(Marca%in% c("Coca-Cola", "Coca-Cola Life", "Coca-Cola Light", "Coca-Cola Light Sin", "Coca-Cola Zero"))%>% group_by(Año,Marca)%>% summarise(ventas_totales=sum(ventas_totales)) %>% filter(ventas_totales != 0)
## `summarise()` has grouped output by 'Año'. You can override using the `.groups`
## argument.
ggplot(Familia_Coca_Cola, aes(x=Año, y= ventas_totales, group= Marca, color= as.factor(Marca)))+
geom_line() +
geom_point() +
labs(title = "Ventas Coca Cola",x= "Año", y="Ventas Totales(Qty)")
regresion <- lm(ventas_totales ~ Año, data=Familia_Coca_Cola)
summary(regresion)
##
## Call:
## lm(formula = ventas_totales ~ Año, data = Familia_Coca_Cola)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1742621 -1375116 -1009566 -678444 4477591
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -569158843 1054563725 -0.540 0.597
## Año 282764 522751 0.541 0.596
##
## Residual standard error: 2338000 on 16 degrees of freedom
## Multiple R-squared: 0.01796, Adjusted R-squared: -0.04342
## F-statistic: 0.2926 on 1 and 16 DF, p-value: 0.596
# Ecuación
# y= -9.193e+09 + (4.589e+06) * Año
# R cuadrada ajustada
# 55%
datos<- data.frame(Año=2020:2025)
prediccion<-predict(regresion,datos)
prediccion
## 1 2 3 4 5 6
## 2025414 2308178 2590943 2873707 3156472 3439236
Familia_Coca_Cola$Tipo_de_Dato <- "Datos Reales"
datos$ventas_totales <- prediccion
datos$Tipo_de_Dato <- "Predicción"
datos_combinados <- rbind(Familia_Coca_Cola,datos)
ggplot(datos_combinados, aes(x=Año, y= ventas_totales, color=Tipo_de_Dato)) +
geom_point() +
labs(title = "Ventas de Coca Cola a 5 años",x= "Año", y="Ventas Totales(Qty)")
## 4¿Se ha incrementado la venta de productos en envases retornables en
los últimos dos años?
# 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")
# Elaborar regresión lineal
aguas <- Ventas_totales_por_marca %>% filter(Marca%in% c("Ciel Mini", "Ciel Exprim", "Ciel Agua Purificada", "Ciel Mineralizada", "Ciel Saborizada")) %>% group_by(Año) %>% summarise(ventas_totales=sum(ventas_totales))%>% filter(ventas_totales != 0)
ggplot(aguas, aes(x=Año, y= ventas_totales,)) +
geom_line() +
geom_point() +
labs(title = "Ventas Aguas",x= "Año", y="Ventas Totales(Qty)")
regresion <- lm(ventas_totales ~ Año, data=aguas)
summary(regresion)
##
## Call:
## lm(formula = ventas_totales ~ Año, data = aguas)
##
## Residuals:
## 1 2 3 4
## 22281 -21029 -24786 23534
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -177526112 29284048 -6.062 0.0261 *
## Año 88186 14515 6.076 0.0260 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 32460 on 2 degrees of freedom
## Multiple R-squared: 0.9486, Adjusted R-squared: 0.9229
## F-statistic: 36.91 on 1 and 2 DF, p-value: 0.02604
datos<- data.frame(Año=2020:2025)
prediccion<-predict(regresion,datos)
prediccion
## 1 2 3 4 5 6
## 610011.5 698197.7 786383.9 874570.1 962756.3 1050942.5
aguas$Tipo_de_Dato <- "Datos Reales"
datos$ventas_totales <- prediccion
datos$Tipo_de_Dato <- "Predicción"
datos_combinados <- rbind(aguas,datos)
ggplot(datos_combinados, aes(x=Año, y= ventas_totales, color=Tipo_de_Dato)) +
geom_point() +
labs(title = "Ventas de Aguas a 5 años",x= "Año", y="Ventas Totales(Qty)")
## 6¿El comportamiento de la venta de agua ha incrementado en relación
al de los refrescos o las bebidas isotónicas?
# Elaborar regresión lineal
aguas <- Ventas_totales_por_marca %>% filter(Marca%in% c("Ciel Mini", "Ciel Exprim", "Ciel Agua Purificada", "Ciel Mineralizada", "Ciel Saborizada")) %>% group_by(Año) %>% summarise(ventas_totales=sum(ventas_totales))%>% filter(ventas_totales != 0)
ggplot(aguas, aes(x=Año, y= ventas_totales,)) +
geom_line() +
geom_point() +
labs(title = "Ventas Aguas",x= "Año", y="Ventas Totales(Qty)")
regresion <- lm(ventas_totales ~ Año, data=aguas)
summary(regresion)
##
## Call:
## lm(formula = ventas_totales ~ Año, data = aguas)
##
## Residuals:
## 1 2 3 4
## 22281 -21029 -24786 23534
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -177526112 29284048 -6.062 0.0261 *
## Año 88186 14515 6.076 0.0260 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 32460 on 2 degrees of freedom
## Multiple R-squared: 0.9486, Adjusted R-squared: 0.9229
## F-statistic: 36.91 on 1 and 2 DF, p-value: 0.02604
datos<- data.frame(Año=2020:2025)
prediccion<-predict(regresion,datos)
prediccion
## 1 2 3 4 5 6
## 610011.5 698197.7 786383.9 874570.1 962756.3 1050942.5
aguas$Tipo_de_Dato <- "Datos Reales"
datos$ventas_totales <- prediccion
datos$Tipo_de_Dato <- "Predicción"
datos_combinados <- rbind(aguas,datos)
ggplot(datos_combinados, aes(x=Año, y= ventas_totales, color=Tipo_de_Dato)) +
geom_point() +
labs(title = "Ventas de Aguas a 5 años",x= "Año", y="Ventas Totales(Qty)")
ventas_Coca_Cola_500ml_NR <- df13 %>% filter(Marca== "Coca-Cola", Presentacion== "500 ml NR Vidrio")%>% group_by(Año,Numero_de_Mes) %>% summarise(ventas_totales=sum(Ventas))%>% filter(ventas_totales != 0)
## `summarise()` has grouped output by 'Año'. You can override using the `.groups`
## argument.
# Confirmar que los datos esten ordenados cronológicamente
# Funcion de serie de tiempo MENSUAL, que inicia en ENERO de 2017
ts <- ts(data=ventas_Coca_Cola_500ml_NR$ventas_totales, start = c(2016,1), frequency = 12)
arima <- auto.arima(ts, D=1) # D=1 por la temporalidad.
arima
## Series: ts
## ARIMA(1,0,0)(0,1,0)[12] with drift
##
## Coefficients:
## ar1 drift
## 0.7092 719.6837
## s.e. 0.1328 299.8533
##
## sigma^2 = 41602728: log likelihood = -325.42
## AIC=656.84 AICc=657.7 BIC=661.24
summary(arima)
## Series: ts
## ARIMA(1,0,0)(0,1,0)[12] with drift
##
## Coefficients:
## ar1 drift
## 0.7092 719.6837
## s.e. 0.1328 299.8533
##
## sigma^2 = 41602728: log likelihood = -325.42
## AIC=656.84 AICc=657.7 BIC=661.24
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 48.85447 5325.927 3245.824 -1.37305 8.085494 0.3668297 -0.1551829
# Generar pronóstico de ventas
pronostico <- forecast(arima, level=95, h=36)
pronostico
## Point Forecast Lo 95 Hi 95
## Sep 2019 57009.75 44367.95 69651.56
## Oct 2019 47611.80 32113.20 63110.40
## Nov 2019 43891.36 27138.87 60643.85
## Dec 2019 49988.01 32639.01 67337.02
## Jan 2020 52649.58 35008.14 70291.03
## Feb 2020 59116.44 41329.71 76903.18
## Mar 2020 63214.35 45354.98 81073.72
## Apr 2020 45929.53 28033.74 63825.33
## May 2020 77893.53 59979.44 95807.63
## Jun 2020 66916.74 48993.45 84840.03
## Jul 2020 65103.76 47175.85 83031.68
## Aug 2020 66475.17 48544.93 84405.41
## Sep 2020 65845.94 43788.54 87903.34
## Oct 2020 56389.84 32524.67 80255.02
## Nov 2020 52628.16 27903.54 77352.78
## Dec 2020 58695.56 33549.72 83841.40
## Jan 2021 61336.39 35981.31 86691.47
## Feb 2021 67788.54 42328.85 93248.22
## Mar 2021 71876.01 46363.87 97388.15
## Apr 2021 54583.79 29045.30 80122.28
## May 2021 86542.54 60990.81 112094.27
## Jun 2021 75562.03 50003.64 101120.42
## Jul 2021 73746.41 48184.67 99308.15
## Aug 2021 75115.94 49552.52 100679.37
## Sep 2021 74485.39 45874.03 103096.74
## Oct 2021 65028.35 35000.53 95056.16
## Nov 2021 61265.99 30550.33 91981.65
## Dec 2021 67332.92 36277.02 98388.83
## Jan 2022 69973.41 38747.76 101199.06
## Feb 2022 76425.32 45114.63 107736.02
## Mar 2022 80512.63 49159.24 111866.01
## Apr 2022 63220.29 31845.45 94595.12
## May 2022 95178.95 63793.33 126564.58
## Jun 2022 84198.38 52807.33 115589.43
## Jul 2022 82382.72 50988.94 113776.49
## Aug 2022 83752.22 52357.07 115147.37
plot(pronostico, main="Ventas Mensuales y Pronóstico a 3 años de Coca-Cola 500ml NR", xlab="Año", ylaab="Ventas (Qty)")
## Warning in plot.window(xlim, ylim, log, ...): "ylaab" is not a graphical
## parameter
## Warning in title(main = main, xlab = xlab, ylab = ylab, ...): "ylaab" is not a
## graphical parameter
## Warning in axis(1, ...): "ylaab" is not a graphical parameter
## Warning in axis(2, ...): "ylaab" is not a graphical parameter
## Warning in box(...): "ylaab" is not a graphical parameter