ventas = read_excel("/Users/angeliknorato/Documents/Javeriana /Series de Tiempo/Series_de_tiempo/Ventas_t.xlsx")
View(ventas)
# Asegurarse de que la columna 'semana' tiene formato fecha
ventas$Fecha <- as.Date(ventas$Fecha, format = "%Y-%m-%d")
# Verificar el formato de la columna
str(ventas$Fecha)
## Date[1:174], format: "2021-01-10" "2021-01-17" "2021-01-24" "2021-01-31" "2021-02-07" ...
Hemos detectado numeros negativos en la base de datos, estos son valores de devoluciones. Sin embargo, como nuestro objetivo es hacer modelacion de las ventas, convertiremos estos valores a cero.
# Convertir todos los valores negativos a ceros
ventas[ventas < 0] = 0
Debido a que nuestra nuestra base de datos contiene 80 items, decidimos hacer una agrupacion por clase de item, obteniendo las siguientes categorias = - AVR (Auto regulador de Voltaje). - Data_Center (Accesorio para equipos). - Line UPS (Baja capacidad, uso hogar). - Online UPS (Alta capacidad, uso empresarial). - Solar (Inversores Solares). - Storage_Battery (Almacenadores de Energia). - Surge (Multitomas). - Accesorios.
AVR = ventas%>% select(`Fecha`, `Item 10`, `Item 26`, `Item 30`, `Item 39`, `Item 45`,
`Item 49`, `Item 55`, `Item 62`, `Item 66`, `Item 79`)
Data_Center = ventas%>% select(`Fecha`, `Item 53`)
Line_UPS = ventas%>% select(`Fecha`, `Item 1`, `item 2`, `Item 3`, `Item 6`, `Item 9`,
`Item 15`, `Item 17`, `Item 22`, `Item 34`, `Item 48`,
`Item 51`, `Item 59`, `Item 63`, `Item 68`, `Item 70`,
`Item 71`, `Item 75`)
Online_UPS = ventas%>% select(`Fecha`, `Item 4`, `Item 5`, `Item 7`, `Item 8`, `Item 12`,
`Item 13`, `Item 14`, `Item 16`, `Item 19`, `Item 20`,
`Item 21`, `Item 23`, `Item 25`, `Item 27`, `Item 28`,
`Item 29`, `Item 31`, `Item 32`, `Item 33`, `Item 38`, `Item 41`,
`Item 43`, `Item 44`, `Item 46`, `Item 50`, `Item 56`, `Item 57`,
`Item 58`, `Item 60`, `Item 65`, `Item 72`, `Item 76`, `Item 77`,
`Item 80`)
Solar = ventas%>% select(`Fecha`, `Item 36`, `Item 52`, `Item 69`, `Item 74`)
Storage_Battery = ventas%>% select(`Fecha`, `Item 11`, `Item 18`, `Item 24`, `Item 35`,
`Item 37`, `Item 40`, `Item 64`, `Item 67`, `Item 78`)
Surge = ventas%>% select(`Fecha`, `Item 47`, `Item 54`, `Item 61`)
Accessories = ventas%>% select(`Fecha`, `Item 42`, `Item 73`)
Hallamos el valor total por categoria :
# Crear una columna del total de items por catgoria
AVR = AVR%>% mutate(Total = rowSums(select(.,`Item 10`, `Item 26`, `Item 30`, `Item 39`,
`Item 45`, `Item 49`,`Item 55`, `Item 62`,
`Item 66`, `Item 79`), na.rm = TRUE))
Data_Center = Data_Center%>% mutate(Total = rowSums(select(.,`Item 53`), na.rm = TRUE))
Line_UPS = Line_UPS%>% mutate(Total = rowSums(select(.,`Item 1`, `item 2`, `Item 3`,
`Item 6`, `Item 9`, `Item 15`, `Item 17`,
`Item 22`, `Item 34`, `Item 48`,
`Item 51`, `Item 59`, `Item 63`,
`Item 68`, `Item 70`,
`Item 71`, `Item 75`), na.rm = TRUE))
Online_UPS = Online_UPS%>% mutate(Total = rowSums(select(.,`Item 4`, `Item 5`, `Item 7`,
`Item 8`, `Item 12`, `Item 13`,
`Item 14`, `Item 16`, `Item 19`,
`Item 20`, `Item 21`, `Item 23`,
`Item 25`, `Item 27`, `Item 28`,
`Item 29`, `Item 31`, `Item 32`,
`Item 33`, `Item 38`, `Item 41`,
`Item 43`, `Item 44`, `Item 46`,
`Item 50`, `Item 56`, `Item 57`,
`Item 58`, `Item 60`, `Item 65`,
`Item 72`, `Item 76`, `Item 77`,
`Item 80`), na.rm = TRUE))
Solar = Solar%>% mutate(Total = rowSums(select(.,`Item 36`, `Item 52`, `Item 69`, `Item 74`),
na.rm = TRUE))
Storage_Battery = Storage_Battery%>% mutate(Total = rowSums(select(.,`Item 11`, `Item 18`,
`Item 24`, `Item 35`,
`Item 37`, `Item 40`,
`Item 64`, `Item 67`,
`Item 78`), na.rm = TRUE))
Surge = Surge%>% mutate(Total = rowSums(select(., `Item 47`, `Item 54`, `Item 61`),
na.rm = TRUE))
Accessories = Accessories%>% mutate(Total = rowSums(select(., `Item 42`, `Item 73`),
na.rm = TRUE))
DespuĆ©s de un anĆ”lisis de los datos, decidimos enfocarnos en cuatro categorĆas: AVR, Line UPS, Online UPS y Battery Storage, que en total suman 70 Ćtems. No se han considerado las categorĆas de Data Center, Solar, Surge y Accesorios, ya que hemos observado una gran cantidad de ceros en estas, lo que sugiere que podemos incluir estos Ćtems para venta solo por pedido especial y no es necesario mantenerlos en inventario.
# Convertir a serie de tiempo
AVR_ts <- ts(AVR$Total, start = c(2021, 2), frequency = 52)
# Verificar la clase del objeto
class(AVR_ts)
## [1] "ts"
# plot
plot(AVR_ts, main= "AVR ", ylab= "Ventas", xlab= "Semana")
media_movil_AVR = stats::filter(AVR_ts, rep(1/4, 4), sides = 2)
print(media_movil_AVR)
## Time Series:
## Start = c(2021, 2)
## End = c(2024, 19)
## Frequency = 52
## [1] NA 1357.50 1173.25 992.50 988.25 877.75 3653.25 4403.50 4548.00
## [10] 4300.00 1692.75 627.75 2499.25 3745.25 3676.75 4477.50 2381.25 1262.00
## [19] 3956.75 7287.75 7300.00 7149.25 4391.25 5846.50 5959.50 5936.00 5920.50
## [28] 323.25 197.25 349.00 318.00 260.50 524.00 419.50 1025.00 1959.00
## [37] 2070.75 1990.75 1437.75 958.00 1336.75 2016.00 2462.25 2375.50 1639.25
## [46] 1391.25 889.75 2404.75 2648.00 2195.00 3355.00 2536.25 3706.25 3733.25
## [55] 2596.25 1809.75 399.75 429.00 858.50 531.25 862.50 866.25 575.75
## [64] 1007.25 3396.00 3504.25 8022.75 9842.75 7471.50 7486.25 2834.75 588.75
## [73] 314.25 553.00 2223.00 2520.50 2430.25 2069.00 712.00 2952.50 2963.00
## [82] 2962.75 2813.25 611.50 698.25 774.75 874.75 883.25 3159.75 3037.50
## [91] 3471.75 3369.50 1362.00 1520.25 913.00 2746.00 2447.25 3598.75 3567.75
## [100] 1762.00 1931.00 661.75 675.25 475.75 419.25 369.25 1734.00 2363.75
## [109] 2302.00 3359.75 2378.50 1877.50 1819.25 916.00 705.00 949.00 977.00
## [118] 1084.75 1095.50 673.50 583.50 679.00 460.50 2423.00 2463.50 2277.75
## [127] 2718.00 2809.50 2772.00 4586.50 4205.00 2415.25 3716.75 2130.50 2135.25
## [136] 2339.75 1527.50 1785.50 1806.50 1911.75 3484.75 2955.00 2966.50 2757.25
## [145] 1853.25 2510.75 2836.50 2788.50 1765.00 1744.25 1374.75 1334.00 2292.75
## [154] 1525.50 1416.75 1736.75 766.25 894.00 1160.25 644.25 2157.75 3676.50
## [163] 3548.00 3519.00 2229.50 883.25 1107.75 1044.75 761.25 1016.50 957.50
## [172] 2620.00 NA NA
plot(media_movil_AVR, main= "AVR Media Movil ", ylab= "Ventas", xlab= "Semana")
# lag plot grafica de resagos
lag.plot(AVR_ts, 20, main = "AVR", do.lines = FALSE)
Escribir conclusionā¦..
# Convertir a serie de tiempo
Line_UPS_ts <- ts(Line_UPS$Total, start = c(2021, 2), frequency = 52)
class(Line_UPS_ts)
## [1] "ts"
# plot
plot(Line_UPS_ts, main= "Line UPS ", ylab= "Ventas", xlab= "Semana")
media_movil_Line_UPS = stats::filter(Line_UPS_ts, rep(1/4, 4), sides = 2)
print(media_movil_Line_UPS)
## Time Series:
## Start = c(2021, 2)
## End = c(2024, 19)
## Frequency = 52
## [1] NA 5796.50 5185.25 4117.25 5881.25 4409.75 5183.75 4928.50 4007.50
## [10] 3705.75 4025.75 4081.00 4710.25 5521.50 6385.00 5820.50 4633.00 3693.75
## [19] 2146.75 2891.25 3103.50 3307.25 3064.00 3648.25 3519.75 2733.00 3071.00
## [28] 3749.50 3377.50 4039.75 4061.00 2744.25 5773.25 5124.75 5976.75 5456.50
## [37] 3681.75 3789.00 2510.25 2852.50 2970.50 3198.75 4681.00 5290.50 6068.00
## [46] 6069.00 4812.25 4145.25 2589.00 1905.75 2172.50 2156.50 3019.25 3432.75
## [55] 3473.25 4021.50 3675.25 3771.75 3537.25 2278.50 1802.50 2427.50 2833.50
## [64] 3017.25 2606.50 1927.25 2853.50 2625.50 2957.00 3826.75 2293.25 3035.00
## [73] 2842.75 2391.75 3437.25 3565.00 3327.75 2976.75 2821.25 3183.25 3083.25
## [82] 3325.00 2862.75 2175.50 4051.50 3647.00 3537.00 3885.00 2920.50 3821.00
## [91] 4565.25 4000.00 5584.00 4876.50 3991.50 4552.75 2824.25 3568.00 3391.75
## [100] 3188.75 2891.50 2248.50 2474.50 1786.25 2279.25 1803.25 3282.00 3856.25
## [109] 2926.50 3584.25 2266.00 2219.50 2784.00 2595.00 3066.25 2672.00 2234.25
## [118] 2835.25 2814.50 2674.50 2654.50 2782.00 1960.00 4046.00 3878.75 2587.00
## [127] 4025.25 2372.25 2375.00 4050.00 3267.75 3551.50 4746.25 4175.50 3577.75
## [136] 3321.75 4293.25 4353.25 4636.50 5884.25 5243.25 4438.25 4222.50 3255.50
## [145] 3240.75 4170.00 4707.25 4501.25 3991.00 4317.25 3866.25 3740.50 3232.75
## [154] 1534.00 1451.50 2533.25 2216.50 3026.50 3525.00 2310.25 3319.75 4723.75
## [163] 4366.75 4851.00 4896.00 4411.50 4472.00 3632.00 2778.75 1959.75 3162.50
## [172] 4890.25 NA NA
plot(media_movil_Line_UPS, main= "Line UPS Media Movil ", ylab= "Ventas", xlab= "Semana")
# lag plot grafica de resagos
lag.plot(Line_UPS_ts, 20, main = "Line UPS", do.lines = FALSE)
Conclusionā¦.
# Convertir a serie de tiempo
Online_UPS_ts <- ts(Online_UPS$Total, start = c(2021, 2), frequency = 52)
class(Online_UPS_ts)
## [1] "ts"
# plot
plot(Online_UPS_ts, main= "Online UPS ", ylab= "Ventas", xlab= "Semana")
## Media móvil (4 perĆodos)
media_movil_Online_UPS = stats::filter(Online_UPS_ts, rep(1/4, 4), sides = 2)
print(media_movil_Online_UPS)
## Time Series:
## Start = c(2021, 2)
## End = c(2024, 19)
## Frequency = 52
## [1] NA 186.50 143.75 138.75 115.25 107.00 126.50 141.00 132.00 123.75
## [11] 119.50 93.25 107.75 137.75 160.00 143.50 119.00 97.50 89.00 103.00
## [21] 137.75 118.50 97.75 98.25 88.50 148.00 156.50 169.50 157.25 108.50
## [31] 110.00 96.50 89.75 68.50 92.75 98.50 110.25 112.25 76.50 80.00
## [41] 100.25 100.50 120.00 140.50 171.50 190.75 170.25 149.00 97.75 74.00
## [51] 94.00 99.25 135.25 166.75 144.25 158.25 134.25 145.00 247.25 214.00
## [61] 224.50 179.00 95.25 113.00 89.50 88.25 104.25 110.75 112.00 165.50
## [71] 154.50 128.25 124.25 145.25 131.00 161.75 180.50 139.00 176.25 248.00
## [81] 224.25 229.00 232.75 186.75 202.50 195.00 155.50 144.00 178.25 149.75
## [91] 167.25 124.00 169.00 188.50 169.75 183.50 96.25 160.25 155.25 219.00
## [101] 237.75 155.50 146.25 115.00 113.25 169.25 285.00 325.25 309.75 320.00
## [111] 247.50 160.50 161.00 132.00 138.50 150.25 143.00 153.00 149.00 128.50
## [121] 133.50 143.25 111.75 199.75 196.00 163.25 205.25 208.50 202.25 204.00
## [131] 202.00 188.75 210.75 218.25 201.50 161.75 202.00 230.00 205.25 243.75
## [141] 208.75 163.75 162.75 140.00 193.00 251.50 269.00 249.50 211.25 258.25
## [151] 253.50 251.75 219.00 88.25 151.25 165.25 160.25 188.25 163.50 146.75
## [161] 180.25 312.50 299.75 323.25 335.75 218.75 224.50 171.00 128.00 145.25
## [171] 111.25 214.25 NA NA
plot(media_movil_Online_UPS, main= "Online UPS Media Movil ", ylab= "Ventas", xlab= "Semana")
# lag plot grafica de resagos
lag.plot(Online_UPS_ts, 20, main = "Online UPS", do.lines = FALSE)
Conclusionesā¦.
# Convertir a serie de tiempo
Storage_Battery_ts <- ts(Storage_Battery$Total, start = c(2021, 2), frequency = 52)
class(Storage_Battery_ts)
## [1] "ts"
plot(Storage_Battery_ts, main= "Storage Battery ", ylab= "Ventas", xlab= "Semana")
media_movil_Storage_Battery = stats::filter(Storage_Battery_ts, rep(1/4, 4), sides = 2)
print(media_movil_Storage_Battery)
## Time Series:
## Start = c(2021, 2)
## End = c(2024, 19)
## Frequency = 52
## [1] NA 618.75 943.00 1082.50 1031.50 915.75 705.50 1003.00 1080.00
## [10] 815.50 726.00 254.50 139.25 165.25 276.25 215.25 387.75 385.75
## [19] 263.25 280.25 153.00 127.00 359.50 342.50 612.50 812.75 567.75
## [28] 567.75 205.25 452.75 450.25 450.25 612.25 172.00 169.50 172.00
## [37] 47.50 40.00 40.00 255.00 292.50 292.50 297.50 120.00 202.50
## [46] 240.00 252.25 304.75 338.25 310.75 453.50 914.75 943.75 1096.25
## [55] 1056.25 536.75 319.25 199.25 273.50 402.25 489.75 773.75 1100.00
## [64] 1057.00 988.50 915.00 645.00 638.50 819.50 862.00 670.25 599.75
## [73] 535.00 585.00 1068.00 1165.00 1232.50 1094.25 715.00 805.75 600.50
## [82] 792.50 1131.00 1147.50 1967.75 1874.00 1439.75 1300.50 1169.25 888.75
## [91] 872.00 782.50 328.50 303.50 262.00 803.50 1044.75 1044.75 1211.25
## [100] 665.75 189.50 328.25 157.50 1098.00 1233.50 1119.75 1121.50 1450.50
## [109] 1772.00 2289.50 2483.50 1208.00 1289.25 2189.00 2522.00 2545.50 2363.75
## [118] 972.00 459.00 713.25 371.75 633.25 648.25 907.25 1015.75 1099.50
## [127] 2417.75 2283.25 2493.25 2106.75 1505.50 1311.50 2088.50 2440.00 3432.75
## [136] 4238.75 3621.00 3967.00 2329.25 2046.50 1783.75 1105.50 1496.00 1018.75
## [145] 863.75 1600.00 2219.75 2595.00 3026.50 2622.75 1658.00 1045.25 625.00
## [154] 489.00 343.75 1014.25 1089.75 1927.50 2610.25 1914.50 2079.50 1210.75
## [163] 680.50 849.50 977.25 1184.50 1445.50 1351.50 1724.25 1529.25 1485.00
## [172] 2624.00 NA NA
plot(media_movil_Storage_Battery, main= "Online UPS Media Movil ", ylab= "Ventas", xlab= "Semana")
lag.plot(Storage_Battery_ts, 20, main = "Storage Battery", do.lines = FALSE)
library(tseries)
## Warning: package 'tseries' was built under R version 4.3.3
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
# Perform the Augmented Dickey-Fuller test
adf_AVR <- adf.test(AVR_ts)
## Warning in adf.test(AVR_ts): p-value smaller than printed p-value
# Print the results
print(adf_AVR)
##
## Augmented Dickey-Fuller Test
##
## data: AVR_ts
## Dickey-Fuller = -5.3614, Lag order = 5, p-value = 0.01
## alternative hypothesis: stationary
La base de datos AVR presenta estacionalidad.
# Perform the Augmented Dickey-Fuller test
adf_Line_UPS <- adf.test(Line_UPS_ts)
## Warning in adf.test(Line_UPS_ts): p-value smaller than printed p-value
# Print the results
print(adf_Line_UPS)
##
## Augmented Dickey-Fuller Test
##
## data: Line_UPS_ts
## Dickey-Fuller = -4.8432, Lag order = 5, p-value = 0.01
## alternative hypothesis: stationary
La base de datos Line_ UPS presenta estacionalidad.
# Perform the Augmented Dickey-Fuller test
adf_Online_UPS <- adf.test(Online_UPS_ts)
## Warning in adf.test(Online_UPS_ts): p-value smaller than printed p-value
# Print the results
print(adf_Online_UPS)
##
## Augmented Dickey-Fuller Test
##
## data: Online_UPS_ts
## Dickey-Fuller = -6.2101, Lag order = 5, p-value = 0.01
## alternative hypothesis: stationary
La base de datos Online_ UPS presenta estacionalidad.
# Perform the Augmented Dickey-Fuller test
adf_Storage_Battery <- adf.test(Storage_Battery_ts)
## Warning in adf.test(Storage_Battery_ts): p-value smaller than printed p-value
# Print the results
print(adf_Storage_Battery)
##
## Augmented Dickey-Fuller Test
##
## data: Storage_Battery_ts
## Dickey-Fuller = -5.3686, Lag order = 5, p-value = 0.01
## alternative hypothesis: stationary
La base de datos Online_ UPS presenta estacionalidad.
Podemos observar que en las 4 categorias el test Dickey-Fuller, arroja un p-value menor a 0.05, lo que nos confirma la estacionalidad de los datos.
library(forecast)
## Warning: package 'forecast' was built under R version 4.3.3
# Graficar ACF y PACF
par(mfrow=c(1,2))
Acf(AVR_ts, main='Función de Autocorrelación (ACF)')
Pacf(AVR_ts, main='Función de Autocorrelación Parcial (PACF)')
library(stats)
# Descomposición aditiva
descomposicion_aditiva_AVR <- decompose(AVR_ts)
# Graficar los componentes
plot(descomposicion_aditiva_AVR)
Podemos observar que la base de datos AVR no sigue un modelo aditivo, ya que la varianza no es constate a lo largo del tiempo, no presenta una tendencia definida y los residuos no se aproximan a cero.
# Descomposición multiplicativa
descomposicion_multiplicativa_AVR <- decompose(AVR_ts, type = "multiplicative")
# Graficar los componentes
plot(descomposicion_multiplicativa_AVR)
A medida que el tiempo avanza, la varianza tiende a disminuir. Al analizar los residuos, tambiƩn se observa que la varianza no se mantiene constante a lo largo del tiempo, lo que sugiere que se trata de un modelo multiplicativo.
Debido a la gran cantidad de ceros no se puede aplicar logaritmos para transformar el modelo de multiplicativo a aditivo.