
Instalar paquetes y llamar
librerĆas
# install.packages("readxl") # Leer documentos de excel
library(readxl)
# install.packages("tidyverse") # Manipulación de datos
library(tidyverse)
# install.packages("ggplot2") # Generar grƔficos presentables
library(ggplot2)
Actividad Individual 2
Importar la base de datos
# file.choose()
df <- read_excel("/Users/sebastianlopez/Downloads/Datos Arca Continental Original (1).xlsx")
Revisar la estructura de los datos
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)
Detectar valores atĆpicos
boxplot(df1$Enero)

# Eliminar renglón de los totales
df1 <- df1[df1$Enero <6000, ]
boxplot(df1$Enero)

Funciones del paquete dplyr
# Adecuar nombres con gsub
names(df1) <- gsub(" ", "_", names(df1))
# Muestra las ventas de Enero a Junio por CEDI:
df2 <- select(df1,c(CEDI,Enero:Junio))
# Muestra los movimientos por Cedi y tamaƱo de tienda grande:
df3 <- df1 %>% filter(TamaƱo_Cte_Industria == "Grande")
# Ordena la base de datos por Cedi, por marca y por 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) %>%
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))
## `summarise()` has grouped output by 'Marca', 'Presentacion'. You can override
## using the `.groups` argument.
# 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
# Colapsar meses en una columna
df7 <- gather(df1, Mes, Ventas, Enero:Diciembre)
df7
## # A tibble: 5,598,096 Ć 15
## ID AƱo Territorio Sub_Territorio CEDI Cliente Nombre
## <dbl> <dbl> <chr> <chr> <chr> <chr> <chr>
## 1 1 2016 Guadalajara Belenes Suc. Belenes 77737 ABARR
## 2 2 2016 Guadalajara Belenes Suc. Belenes 77737 ABARR
## 3 3 2016 Guadalajara Belenes Suc. Belenes 77737 ABARR
## 4 4 2016 Guadalajara Belenes Suc. Belenes 77737 ABARR
## 5 5 2016 Guadalajara Belenes Suc. Belenes 77737 ABARR
## 6 6 2016 Guadalajara Belenes Suc. Belenes 77737 ABARR
## 7 7 2016 Guadalajara Belenes Suc. Belenes 77737 ABARR
## 8 8 2016 Guadalajara Belenes Suc. Belenes 77737 ABARR
## 9 9 2016 Guadalajara Belenes Suc. Belenes 77737 ABARR
## 10 10 2016 Guadalajara Belenes Suc. Belenes 77737 ABARR
## # ā¹ 5,598,086 more rows
## # ⹠8 more variables: Tamaño_Cte_Industria <chr>, Segmento_Det <chr>,
## # Marca <chr>, Presentacion <chr>, TamaƱo <chr>, Retornable_NR <chr>,
## # Mes <chr>, Ventas <dbl>
Tarea Colaborativa 2
Actividad Individual 3
# 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")
df7$NĆŗmero_de_Mes <- meses[df7$Mes]
# Graficar el total de ventas por mes y aƱo
ventas_totales <- df7 %>%
group_by(Año, Número_de_Mes) %>%
summarise(Ventas_Totales = sum(Ventas)) %>%
arrange(NĆŗmero_de_Mes)
## `summarise()` has grouped output by 'AƱo'. You can override using the `.groups`
## argument.
ggplot(ventas_totales, aes(x=Número_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")

Actividad Colaborativa 3
Importar la base de datos
bd <- read.csv("/Users/sebastianlopez/Downloads/Datos Arca Continental Original (1).xlsx")
## Warning in read.table(file = file, header = header, sep = sep, quote = quote, :
## line 1 appears to contain embedded nulls
## Warning in read.table(file = file, header = header, sep = sep, quote = quote, :
## line 2 appears to contain embedded nulls
## Warning in read.table(file = file, header = header, sep = sep, quote = quote, :
## line 3 appears to contain embedded nulls
## Warning in read.table(file = file, header = header, sep = sep, quote = quote, :
## line 5 appears to contain embedded nulls
## Warning in scan(file = file, what = what, sep = sep, quote = quote, dec = dec,
## : embedded nul(s) found in input
### Limpiar base de datos
summary(df)
## ID AƱo Territorio Sub Territorio
## Min. : 1 Min. :2016 Length:466509 Length:466509
## 1st Qu.:116628 1st Qu.:2017 Class :character Class :character
## Median :233255 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:466509 Length:466509 Length:466509 Length:466509
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
##
## Segmento Det Marca Presentacion TamaƱo
## Length:466509 Length:466509 Length:466509 Length:466509
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
##
## Retornable_NR Enero Febrero Marzo
## Length:466509 Min. : -19.0 Min. : -11.00 Min. : -32.00
## Class :character 1st Qu.: 1.0 1st Qu.: 1.00 1st Qu.: 1.00
## Mode :character Median : 2.0 Median : 2.00 Median : 3.00
## Mean : 10.1 Mean : 9.76 Mean : 11.36
## 3rd Qu.: 6.0 3rd Qu.: 6.00 3rd Qu.: 6.00
## Max. :42736.0 Max. :42767.00 Max. :42795.00
## NA's :233480 NA's :231213 NA's :227420
## Abril Mayo Junio Julio
## Min. : -70.00 Min. : -106.00 Min. : -211.0 Min. : -60.00
## 1st Qu.: 1.00 1st Qu.: 1.00 1st Qu.: 1.0 1st Qu.: 1.00
## Median : 3.00 Median : 3.00 Median : 3.0 Median : 2.00
## Mean : 11.71 Mean : 12.75 Mean : 12.2 Mean : 11.75
## 3rd Qu.: 6.00 3rd Qu.: 7.00 3rd Qu.: 6.0 3rd Qu.: 6.00
## Max. :42826.00 Max. :42856.00 Max. :42887.0 Max. :42917.00
## NA's :224057 NA's :216910 NA's :215753 NA's :223411
## Agosto Septiembre Octubre Noviembre
## Min. : -211.00 Min. : -527.0 Min. : -38.0 Min. : -25.0
## 1st Qu.: 1.00 1st Qu.: 1.0 1st Qu.: 1.0 1st Qu.: 1.0
## Median : 3.00 Median : 3.0 Median : 3.0 Median : 3.0
## Mean : 11.98 Mean : 13.4 Mean : 13.7 Mean : 13.3
## 3rd Qu.: 6.00 3rd Qu.: 7.0 3rd Qu.: 7.0 3rd Qu.: 6.0
## Max. :42948.00 Max. :42979.0 Max. :43009.0 Max. :43040.0
## NA's :220242 NA's :337314 NA's :338386 NA's :338460
## Diciembre
## Min. : -28.0
## 1st Qu.: 1.0
## Median : 3.0
## Mean : 14.8
## 3rd Qu.: 7.0
## Max. :43070.0
## NA's :341855
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 ...
# Convertir tipo de variables
df$Cliente <- (df$Cliente)
df$Enero <- (df$Enero)
df$Febrero <- (df$Febrero)
df$Marzo <- (df$Marzo)
df$Abril <-(df$Abril)
df$Mayo <- (df$Mayo)
df$Junio <- (df$Junio)
df$Julio <- (df$Julio)
df$Agosto <- (df$Agosto)
df$Septiembre <- (df$Septiembre)
df$Octubre <- (df$Octubre)
df$Noviembre <- as.integer(df$Noviembre)
df$Diciembre <- as.integer(df$Diciembre)
### Realizar las grƔficas
library(ggplot2)
ggplot(data = df) +
geom_point(mapping = aes(x = Cliente, y = Enero))
## Warning: Removed 233480 rows containing missing values or values outside the scale range
## (`geom_point()`).

library(ggplot2)
library(dplyr)
# Graficar el total de ventas por mes, aƱo y tamaƱo de cliente
ventas_totales <- df7 %>%
group_by(Año, Número_de_Mes, Tamaño) %>%
summarise(Ventas_Totales = sum(Ventas)) %>%
arrange(NĆŗmero_de_Mes)
## `summarise()` has grouped output by 'Año', 'Número_de_Mes'. You can override
## using the `.groups` argument.
ggplot(ventas_totales, aes(x=Número_de_Mes, y=Tamaño, group=Año, color= as.factor(Año))) +
geom_area() +
labs(x="Mes", y="Ventas Totales (Qty)", color="AƱo")

Actividad Indivudual 4
# Elaborar Regresion lineal
ventas_4567 <- df7 %>%
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_point() +
labs(x="AƱo",y="Ventas Totales(Qty)", title= "ventas totales del cliente 4567")

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 = 48918667 - 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 <- "prediccion"
datos_combinados <- rbind(ventas_4567,Datos)
ggplot(datos_combinados, aes(x=AƱo, y=ventas_totales, color=Tipo_de_Dato))+
geom_point() +
labs(x="AƱo", y="Ventas Totales (Qty)", title = "Pronostico a 5 aƱos de Ventas del cliente 4567")

Actividad Colaborativa 4
# Elaborar Regresion lineal
ventas_7737 <- df7 %>%
filter(Cliente == "7737") %>%
group_by(AƱo) %>%
summarise(ventas_totales = sum(Ventas)) %>%
filter(ventas_totales !=0)
#ventas_7737$Secuencia <- 1: nrow(ventas_7737)
ggplot(ventas_7737, aes(x=AƱo, y=ventas_totales)) +
geom_point() +
labs(x="AƱo",y="Ventas Totales(Qty)", title= "ventas totales del cliente 7737")

regresion <- lm(ventas_totales ~ AƱo, data = ventas_7737)
summary(regresion)
##
## Call:
## lm(formula = ventas_totales ~ AƱo, data = ventas_7737)
##
## Residuals:
## 1 2 3
## -477.7 955.3 -477.7
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1378614.7 1669578.2 0.826 0.561
## AƱo -681.0 827.3 -0.823 0.562
##
## Residual standard error: 1170 on 1 degrees of freedom
## Multiple R-squared: 0.4039, Adjusted R-squared: -0.1922
## F-statistic: 0.6775 on 1 and 1 DF, p-value: 0.5616
#Ecuación
Datos <- data.frame(AƱo= 2020:2025)
prediccion <- predict(regresion,Datos)
prediccion
## 1 2 3 4 5 6
## 2994.6667 2313.6667 1632.6667 951.6667 270.6667 -410.3333
ventas_7737$Tipo_de_Dato <- "Datos Reales"
Datos$ventas_totales <- prediccion
Datos$Tipo_de_Dato <- "prediccion"
datos_combinados <- rbind(ventas_7737,Datos)
ggplot(datos_combinados, aes(x=AƱo, y=ventas_totales, color=Tipo_de_Dato))+
geom_point() +
labs(x="AƱo", y="Ventas Totales (Qty)", title = "Pronostico a 5 aƱos de Ventas del cliente 77737")

# Elaborar Regresion lineal
ventas_totales <- df7 %>%
filter(Cliente == "ventas_totales") %>%
group_by(AƱo) %>%
summarise(ventas_totales = sum(Ventas)) %>%
filter(ventas_totales !=0)
#ventas_4567$Secuencia <- 1: nrow(ventas_4567)
ggplot(ventas_totales, aes(x=AƱo, y=ventas_totales)) +
geom_point() +
labs(x="AƱo",y="Ventas Totales(Qty)", title= "ventas_totales")

regresion <- lm(ventas_totales ~ AƱo, data = ventas_7737)
summary(regresion)
##
## Call:
## lm(formula = ventas_totales ~ AƱo, data = ventas_7737)
##
## Residuals:
## 1 2 3
## -477.7 955.3 -477.7
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1378614.7 1669578.2 0.826 0.561
## AƱo -681.0 827.3 -0.823 0.562
##
## Residual standard error: 1170 on 1 degrees of freedom
## Multiple R-squared: 0.4039, Adjusted R-squared: -0.1922
## F-statistic: 0.6775 on 1 and 1 DF, p-value: 0.5616
#Ecuación
# y = 48918667 - 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
## 2994.6667 2313.6667 1632.6667 951.6667 270.6667 -410.3333
ventas_totales$Tipo_de_Dato <- "Datos Reales"
Datos$ventas_totales <- prediccion
Datos$Tipo_de_Dato <- "prediccion"
datos_combinados <- rbind(ventas_totales,Datos)
ggplot(datos_combinados, aes(x=AƱo, y=ventas_totales, color=Tipo_de_Dato))+
geom_point() +
labs(x="AƱo", y="Ventas Totales (Qty)", title = "Pronostico a 5 aƱos de ventas_totales")

# Elaborar Regresion lineal
ventas_7796 <- df7 %>%
filter(Cliente == "7796") %>%
group_by(AƱo) %>%
summarise(ventas_totales = sum(Ventas)) %>%
filter(ventas_totales !=0)
#ventas_7796$Secuencia <- 1: nrow(ventas_7796)
ggplot(ventas_7796, aes(x=AƱo, y=ventas_totales)) +
geom_point() +
labs(x="AƱo",y="Ventas Totales(Qty)", title= "ventas totales del cliente 7796")

regresion <- lm(ventas_totales ~ AƱo, data = ventas_7796)
summary(regresion)
##
## Call:
## lm(formula = ventas_totales ~ AƱo, data = ventas_7796)
##
## Residuals:
## 1 2 3
## -34.33 68.67 -34.33
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1360302.33 120004.57 11.34 0.0560 .
## AƱo -673.00 59.47 -11.32 0.0561 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 84.1 on 1 degrees of freedom
## Multiple R-squared: 0.9923, Adjusted R-squared: 0.9845
## F-statistic: 128.1 on 1 and 1 DF, p-value: 0.05611
#Ecuación
Datos <- data.frame(AƱo= 2020:2025)
prediccion <- predict(regresion,Datos)
prediccion
## 1 2 3 4 5 6
## 842.3333 169.3333 -503.6667 -1176.6667 -1849.6667 -2522.6667
ventas_7796$Tipo_de_Dato <- "Datos Reales"
Datos$ventas_totales <- prediccion
Datos$Tipo_de_Dato <- "prediccion"
datos_combinados <- rbind(ventas_7796,Datos)
ggplot(datos_combinados, aes(x=AƱo, y=ventas_totales, color=Tipo_de_Dato))+
geom_point() +
labs(x="AƱo", y="Ventas Totales (Qty)", title = "Pronostico a 5 aƱos de Ventas del cliente 7796")
