La primer función es la correspondiente a la tendencia, la cuál recide dos parámetros:
serie_tiempo:Objeto de la clase ts.
periodos:Corresponde al orden de las medias móviles.
Tendencia<-function(serie_tiempo,periodos) {
if(class(serie_tiempo)=="ts"){
mean_number<-ma(serie_tiempo, periodos, centre=TRUE)
pasos<-na.omit(mean_number)
mean_number<-as.numeric(mean_number)
serie_t<-as.numeric(serie_tiempo)
pasos<-c()
for (i in 1:floor(length(serie_tiempo)/periodos)) {
pasos[i]<-mean(serie_tiempo[(periodos*(i-1)):(periodos*i)] %>% t() %>% as.vector(),
na.rm = T)
}
if(pasos[1]<pasos[length(pasos)]){Tendencia <-"La tendencia es creciente"
}
else if(pasos[1]>pasos[length(pasos)]){Tendencia <- "La tendencia es decreciente"
}
else{ Tendencia<-"La tendencia es constante"}
Contador<-c(1:length(serie_tiempo))
Contador1<-c(1:length(mean_number))
Grafica<-ggplot()+geom_line(aes(x=Contador, y=mean_number), color="deepskyblue", size=1.5)+labs(x="Tiempo", y="Tendencia")+ ggtitle("Tendencia con Medias Moviles", subtitle = Tendencia)+theme_bw()
return(list(Grafica,mean_number))
}else{
stop(ggplot()+
annotate(geom = "text", x = 1, y = 1, label = "Error!\n, class(s1) is not ts. ", size = 12,
colour = "#FF0000")+
theme(axis.title = element_blank(),
axis.text = element_blank(), axis.ticks = element_blank(),
panel.background = element_blank()))
}
}
Tendencia(JohnsonJohnson,7)
## [[1]]
##
## [[2]]
## [1] NA NA NA 0.6928571 0.6700000 0.6828571
## [7] 0.6714286 0.7400000 0.7385714 0.7585714 0.7414286 0.8057143
## [13] 0.8128571 0.8342857 0.8457143 0.9371429 0.9614286 1.0128571
## [19] 1.0557143 1.1528571 1.2000000 1.2371429 1.2571429 1.3800000
## [25] 1.4371429 1.4700000 1.4900000 1.5728571 1.6585714 1.6800000
## [31] 1.7100000 1.8214286 1.9242857 2.0057143 2.0914286 2.2114286
## [37] 2.3142857 2.4171429 2.5714286 2.7771429 2.9828571 3.1500000
## [43] 3.3814286 3.6771429 3.8571429 4.0628571 4.2557143 4.4614286
## [49] 4.5771429 4.7571429 4.9757143 5.3357143 5.4000000 5.5414286
## [55] 5.7342857 6.0942857 6.1328571 6.2871429 6.4542857 6.8142857
## [61] 6.8271429 7.0200000 7.3028571 7.6500000 7.6371429 7.8942857
## [67] 8.2414286 8.7300000 8.8714286 9.2957143 9.8357143 10.5942857
## [73] 10.5042857 11.0442857 11.5328571 12.4071429 12.1371429 12.7285714
## [79] 13.0885714 14.1042857 13.7571429 NA NA NA
Tendencia(AirPassengers,10)
## [[1]]
##
## [[2]]
## [1] NA NA NA NA NA 129.40 129.00 128.15 127.15 128.00
## [11] 129.00 127.85 126.75 128.50 132.75 138.00 141.45 142.15 142.80 143.70
## [21] 144.65 148.05 151.40 152.20 152.70 155.15 160.50 167.30 171.90 173.05
## [31] 173.90 174.35 174.85 176.75 177.95 177.30 177.45 180.70 187.00 194.15
## [41] 198.55 199.85 200.60 201.45 202.35 205.75 209.25 210.05 210.05 212.85
## [51] 219.65 226.95 231.05 231.10 230.55 229.20 225.25 223.20 222.70 220.40
## [61] 218.50 221.35 228.70 236.75 242.10 243.45 245.45 247.85 248.50 250.45
## [71] 252.35 251.00 250.50 256.85 268.00 279.35 287.05 289.05 291.05 294.15
## [81] 295.40 298.15 300.40 298.00 297.05 303.45 315.05 327.50 334.80 335.55
## [91] 336.35 337.70 337.00 338.30 338.90 334.70 332.65 339.00 352.55 367.25
## [101] 375.95 377.50 378.75 379.70 377.40 376.25 372.90 364.10 357.40 360.15
## [111] 372.40 385.25 391.35 391.00 390.45 391.30 390.90 392.75 392.95 387.45
## [121] 382.25 387.80 405.00 422.65 433.80 437.40 440.65 444.35 444.65 444.35
## [131] 443.75 439.40 434.40 441.15 459.05 476.30 486.40 487.85 488.55 NA
## [141] NA NA NA NA
serie1<-c(as.numeric(fechas[123], "%y"),as.numeric(fechas[123], "%m"))
s1<-ts(series[450,-c(1:7)] %>% t() %>% na.omit(),frequency = 12, start=serie1[1])
Tendencia(s1,4)
## [[1]]
##
## [[2]]
## [1] NA NA 6003.648 5997.125 5988.085 5766.894 5635.071
## [8] 5678.440 5787.720 6102.629 6414.677 6499.012 6536.316 6552.108
## [15] 6458.847 6362.154 6288.346 6058.229 5888.530 5995.523 6149.431
## [22] 6466.744 6845.390 6926.863 6917.249 6928.119 6858.775 6827.422
## [29] 6814.379 6569.844 6352.656 6422.114 6553.249 6906.377 7327.935
## [36] 7409.524 7430.695 7490.656 7406.550 7342.240 7279.531 6941.506
## [43] 6723.862 6850.651 7029.048 7438.363 7820.557 7838.409 7857.632
## [50] 7867.130 7732.218 7642.619 7494.204 7140.731 6966.113 7086.835
## [57] 7288.574 7747.093 8171.967 8251.954 8277.243 8230.669 8110.861
## [64] 8057.309 7913.243 7575.790 7377.026 7495.002 7769.404 8263.396
## [71] 8728.094 8824.786 8831.653 8841.836 8758.417 8721.229 8624.994
## [78] 8301.731 8025.499 8104.225 8392.014 8844.581 9307.561 9370.611
## [85] 9261.331 9227.003 9141.754 9050.211 8965.534 8669.620 8379.084
## [92] 8472.229 8619.958 8880.628 9219.911 9152.170 8996.318 8960.388
## [99] 8763.455 8575.676 8455.639 8038.771 7764.254 7920.449 8116.009
## [106] 8592.951 9120.129 9229.754 9317.750 9368.097 9226.547 9154.571
## [113] 9037.625 8668.362 8479.096 8587.917 8773.292 9304.469 9835.876
## [120] 9957.517 10013.708 9970.004 9671.574 9508.624 9322.671 8856.025
## [127] 8674.655 8747.089 8909.464 9401.739 9877.421 10044.601 10129.618
## [134] 10203.195 NA NA
La función recibe los siguientes parámetros:
s_tiempo:objeto de la clase ts (time series).
modelo: Aquà tendremos dos casos, si se desea aditiva o multiplicativa, en algún otro caso se manda el mensaje de error.
periocidad: Hace referencia a la temporalidad de la serie.
media_movil: Corresponde al orden de las medias móviles.
Estacionalidad <- function(s_tiempo, modelo, periocidad, media_movil){
if(class(s_tiempo)=="ts"){
if(modelo=="aditiva"){
tend_s<-Tendencia(s_tiempo,media_movil)
s<-as.numeric(s_tiempo)
detrend<-s-tend_s[[2]]
matriz<-t(matrix(data = detrend, nrow = 4))
estac_s<-colMeans(matriz, na.rm=TRUE)
estac_s<-rep_len(estac_s, length.out = length(s))
Tabla<-data.frame(Tiempo=c(1:length(s)), Serie=s, Tendencia=tend_s[[2]], Estacionalidad=estac_s)
Grafica2<-ggplot(Tabla)+geom_line(aes(x=Tiempo, y=Estacionalidad),
color="seagreen1",size=1.5)+geom_point(aes(x=Tiempo, y=Estacionalidad), color="mediumblue")+labs(x="Tiempo", y="Estacionalidad")+ ggtitle("Estacionalidad de Serie de Tiempo")+theme_bw()
return(list(Grafica2, Tabla$Estacionalidad))
}else if(modelo=="multiplicativa"){
tend_s<-Tendencia(s_tiempo,media_movil)
s<-as.numeric(s_tiempo)
detrend<-s/tend_s[[2]]
matriz<-t(matrix(data = detrend, nrow = 4))
estac_s<-colMeans(matriz, na.rm=TRUE)
estac_s<-rep_len(estac_s, length.out = length(s))
Tabla<-data.frame(Tiempo=c(1:length(s)), Serie=s, Tendencia=tend_s[[2]], Estacionalidad=estac_s)
g1<-ggplot(Tabla)+geom_line(aes(x=Tiempo, y=Estacionalidad), color="royalblue", size=1.5)+geom_point(aes(x=Tiempo, y=Estacionalidad), color="purple4")+labs(x="Tiempo", y="Estacionalidad")+ ggtitle( "Estacionalidad de la Serie de Tiempo")+theme_bw()
return(list(g1, Tabla$Estacionalidad))
}else
{stop('El parametro no es correcto')}
}else{
stop(ggplot()+
annotate(geom = "text", x = 1, y = 1, label = "Error!\n, class(s1) is not ts. ", size = 12,
colour = "#FF0000")+
theme(axis.title = element_blank(),
axis.text = element_blank(), axis.ticks = element_blank(),
panel.background = element_blank()))
}
}
Estacionalidad(JohnsonJohnson,"aditiva",4,4)
## [[1]]
##
## [[2]]
## [1] 0.2230625 0.2454375 0.3101875 -0.7728750 0.2230625 0.2454375
## [7] 0.3101875 -0.7728750 0.2230625 0.2454375 0.3101875 -0.7728750
## [13] 0.2230625 0.2454375 0.3101875 -0.7728750 0.2230625 0.2454375
## [19] 0.3101875 -0.7728750 0.2230625 0.2454375 0.3101875 -0.7728750
## [25] 0.2230625 0.2454375 0.3101875 -0.7728750 0.2230625 0.2454375
## [31] 0.3101875 -0.7728750 0.2230625 0.2454375 0.3101875 -0.7728750
## [37] 0.2230625 0.2454375 0.3101875 -0.7728750 0.2230625 0.2454375
## [43] 0.3101875 -0.7728750 0.2230625 0.2454375 0.3101875 -0.7728750
## [49] 0.2230625 0.2454375 0.3101875 -0.7728750 0.2230625 0.2454375
## [55] 0.3101875 -0.7728750 0.2230625 0.2454375 0.3101875 -0.7728750
## [61] 0.2230625 0.2454375 0.3101875 -0.7728750 0.2230625 0.2454375
## [67] 0.3101875 -0.7728750 0.2230625 0.2454375 0.3101875 -0.7728750
## [73] 0.2230625 0.2454375 0.3101875 -0.7728750 0.2230625 0.2454375
## [79] 0.3101875 -0.7728750 0.2230625 0.2454375 0.3101875 -0.7728750
Estacionalidad(AirPassengers,"multiplicativa",12,12)
## [[1]]
##
## [[2]]
## [1] 0.9822973 0.9710033 1.0099150 1.0297271 0.9822973 0.9710033 1.0099150
## [8] 1.0297271 0.9822973 0.9710033 1.0099150 1.0297271 0.9822973 0.9710033
## [15] 1.0099150 1.0297271 0.9822973 0.9710033 1.0099150 1.0297271 0.9822973
## [22] 0.9710033 1.0099150 1.0297271 0.9822973 0.9710033 1.0099150 1.0297271
## [29] 0.9822973 0.9710033 1.0099150 1.0297271 0.9822973 0.9710033 1.0099150
## [36] 1.0297271 0.9822973 0.9710033 1.0099150 1.0297271 0.9822973 0.9710033
## [43] 1.0099150 1.0297271 0.9822973 0.9710033 1.0099150 1.0297271 0.9822973
## [50] 0.9710033 1.0099150 1.0297271 0.9822973 0.9710033 1.0099150 1.0297271
## [57] 0.9822973 0.9710033 1.0099150 1.0297271 0.9822973 0.9710033 1.0099150
## [64] 1.0297271 0.9822973 0.9710033 1.0099150 1.0297271 0.9822973 0.9710033
## [71] 1.0099150 1.0297271 0.9822973 0.9710033 1.0099150 1.0297271 0.9822973
## [78] 0.9710033 1.0099150 1.0297271 0.9822973 0.9710033 1.0099150 1.0297271
## [85] 0.9822973 0.9710033 1.0099150 1.0297271 0.9822973 0.9710033 1.0099150
## [92] 1.0297271 0.9822973 0.9710033 1.0099150 1.0297271 0.9822973 0.9710033
## [99] 1.0099150 1.0297271 0.9822973 0.9710033 1.0099150 1.0297271 0.9822973
## [106] 0.9710033 1.0099150 1.0297271 0.9822973 0.9710033 1.0099150 1.0297271
## [113] 0.9822973 0.9710033 1.0099150 1.0297271 0.9822973 0.9710033 1.0099150
## [120] 1.0297271 0.9822973 0.9710033 1.0099150 1.0297271 0.9822973 0.9710033
## [127] 1.0099150 1.0297271 0.9822973 0.9710033 1.0099150 1.0297271 0.9822973
## [134] 0.9710033 1.0099150 1.0297271 0.9822973 0.9710033 1.0099150 1.0297271
## [141] 0.9822973 0.9710033 1.0099150 1.0297271
ejemplo3<-c(as.numeric(fechas[123], "%y"),as.numeric(fechas[123], "%m"))
s3<-ts(series[511,-c(1:7)] %>% t() %>% na.omit(),frequency = 12, start=ejemplo3[1])
Estacionalidad(s3,"aditiva",6,6)
## [[1]]
##
## [[2]]
## [1] 117.47126 46.77874 -26.97126 -129.69167 117.47126 46.77874
## [7] -26.97126 -129.69167 117.47126 46.77874 -26.97126 -129.69167
## [13] 117.47126 46.77874 -26.97126 -129.69167 117.47126 46.77874
## [19] -26.97126 -129.69167 117.47126 46.77874 -26.97126 -129.69167
## [25] 117.47126 46.77874 -26.97126 -129.69167 117.47126 46.77874
## [31] -26.97126 -129.69167 117.47126 46.77874 -26.97126 -129.69167
## [37] 117.47126 46.77874 -26.97126 -129.69167 117.47126 46.77874
## [43] -26.97126 -129.69167 117.47126 46.77874 -26.97126 -129.69167
## [49] 117.47126 46.77874 -26.97126 -129.69167 117.47126 46.77874
## [55] -26.97126 -129.69167 117.47126 46.77874 -26.97126 -129.69167
## [61] 117.47126 46.77874 -26.97126 -129.69167 117.47126 46.77874
## [67] -26.97126 -129.69167 117.47126 46.77874 -26.97126 -129.69167
## [73] 117.47126 46.77874 -26.97126 -129.69167 117.47126 46.77874
## [79] -26.97126 -129.69167 117.47126 46.77874 -26.97126 -129.69167
## [85] 117.47126 46.77874 -26.97126 -129.69167 117.47126 46.77874
## [91] -26.97126 -129.69167 117.47126 46.77874 -26.97126 -129.69167
## [97] 117.47126 46.77874 -26.97126 -129.69167 117.47126 46.77874
## [103] -26.97126 -129.69167 117.47126 46.77874 -26.97126 -129.69167
## [109] 117.47126 46.77874 -26.97126 -129.69167 117.47126 46.77874
## [115] -26.97126 -129.69167 117.47126 46.77874 -26.97126 -129.69167
## [121] 117.47126 46.77874 -26.97126
La función recibe los siguientes parámetros:
s_tiempo:objeto de la clase ts (time series).
modelo: Aquà tendremos dos casos, si se desea aditiva o multiplicativa, en algún otro caso se manda el mensaje de error.
periocidad: Hace referencia a la temporalidad de la serie.
media_movil: Corresponde al orden de las medias móviles.
Error_aleatorio<-function(serie_tiempo, modelo, periocidad, media_movil){
if(class(serie_tiempo)=="ts"){
if(modelo=="aditiva"){
tend_s<-Tendencia(serie_tiempo,media_movil)
est_s<-Estacionalidad(serie_tiempo, modelo, periocidad, media_movil)
s<-as.numeric(serie_tiempo)
error_a<-s-tend_s[[2]]-est_s[[2]]
Tabla<-data.frame(Tiempo=c(1:length(s)), Error=error_a)
Grafica3<-ggplot(Tabla)+geom_line(aes(x=Tiempo, y=Error), color="cyan4", size=1.1)+geom_point(aes(x=Tiempo, y=Error), color="darkgreen")+ labs(x="Tiempo", y="Error Aleatorio")+ ggtitle( "Error Aleatorio de la Serie de Tiempo")+theme_bw()
return(list(Grafica3,error_a))
}else if(modelo=="multiplicativa"){
tend_s<-Tendencia(serie_tiempo,media_movil)
estac_s <-Estacionalidad(serie_tiempo, modelo, periocidad, media_movil)
s<-as.numeric(serie_tiempo)
error_a<-s/(tend_s[[2]]*estac_s[[2]])
Tabla<-data.frame(Tiempo=c(1:length(s)), Error=as.numeric(error_a))
Grafica3<-ggplot(Tabla)+geom_line(aes(x=Tiempo, y=Error), color="deeppink1", size=1)+geom_point(aes(x=Tiempo, y=Error), color="darkviolet")+ labs(x="Tiempo", y="Error Aleatorio", title = "Error Aleatorio de la Serie de Tiempo")+theme_bw()
return(list(Grafica3,error_a))
}else(stop('El parametro no es correcto'))
}else{
stop(ggplot()+
annotate(geom = "text", x = 1, y = 1, label = "Error!\n, class(s1) is not ts. ", size = 12,
colour = "#FF0000")+
theme(axis.title = element_blank(),
axis.text = element_blank(), axis.ticks = element_blank(),
panel.background = element_blank()))
}
}
Ejercicio1<-c(as.numeric(fechas[123], "%y"),as.numeric(fechas[123], "%m"))
s1<-ts(series[450,-c(1:7)] %>% t() %>% na.omit(),frequency = 12, start=Ejercicio1[1])
Error_aleatorio(s1,"multiplicativa",4,4)
## [[1]]
##
## [[2]]
## [1] NA NA 1.0187994 1.0479890 0.9682871 1.0503179 0.9890105
## [8] 0.8802966 1.0293806 1.0222686 0.9744146 1.0668312 1.0119055 0.9533744
## [15] 1.0119038 1.0625514 0.9606204 1.0001055 1.0432991 0.8732887 0.9958093
## [22] 1.0540271 0.9634379 1.0731705 1.0205676 0.9415654 0.9932986 1.0679632
## [29] 0.9752452 1.0182072 1.0300935 0.8583793 1.0058508 1.0553446 0.9518022
## [36] 1.0845068 1.0177527 0.9293770 1.0083834 1.0911492 0.9509503 1.0220525
## [43] 1.0176442 0.8504560 1.0305020 1.0445970 0.9677694 1.0852972 0.9872390
## [50] 0.9403943 1.0465396 1.0499111 0.9533060 1.0231000 1.0074378 0.8734617
## [57] 1.0258473 1.0225632 0.9773439 1.0937705 0.9886035 0.9533177 1.0185353
## [64] 1.0346713 0.9959098 1.0199563 0.9825405 0.8768983 1.0243278 1.0378106
## [71] 0.9802114 1.0588532 1.0233221 0.9341555 1.0191363 1.0355820 1.0054016
## [78] 1.0036955 1.0088560 0.8871690 0.9784138 1.0638646 0.9970893 1.0398310
## [85] 1.0235468 0.9364379 1.0054313 1.0551495 1.0026109 0.9701183 1.0449818
## [92] 0.8870162 0.9970975 1.0736068 0.9535859 1.0577762 1.0282440 0.9318206
## [99] 0.9988409 1.0983012 0.9393839 1.0173074 1.0279697 0.8488578 1.0215737
## [106] 1.0540399 0.9471513 1.0889406 1.0029328 0.9465939 1.0317796 1.0407952
## [113] 0.9644857 1.0300523 1.0070863 0.8686435 1.0431805 1.0048136 0.9735322
## [120] 1.0883675 1.0178639 0.9295401 1.0442104 1.0416339 0.9343188 1.0687790
## [127] 0.9728718 0.8852549 1.0432258 1.0053099 0.9850120 1.0472363 1.0322935
## [134] 0.9414855 NA NA
Error_aleatorio(JohnsonJohnson, "aditiva", 12,12)
## [[1]]
##
## [[2]]
## [1] NA NA NA NA NA
## [6] NA 0.001435185 0.504722222 -0.113217593 -0.211250000
## [11] -0.070648148 0.468888889 -0.101967593 -0.292083333 -0.111898148
## [16] 0.503055556 -0.165717593 -0.262916667 -0.050648148 0.552638889
## [21] -0.121967593 -0.178750000 -0.069814815 0.565555556 -0.248217593
## [26] -0.316666667 0.129351852 0.667222222 -0.206550926 -0.353333333
## [31] -0.175231481 0.658888889 -0.524467593 -0.176250000 0.029768519
## [36] 0.696388889 -0.321967593 -0.330000000 -0.201481481 0.053888889
## [41] -0.356967593 -0.012500000 0.087268519 0.661388889 -0.319467593
## [46] 0.085000000 -0.103981481 0.293888889 0.108032407 -0.031250000
## [51] -0.186481481 -0.118611111 0.081782407 0.040000000 0.616018519
## [56] 0.042638889 -0.240717593 -0.196250000 0.199768519 -0.212361111
## [61] -0.105717593 0.441250000 0.338518519 -0.793611111 -0.218217593
## [66] 0.595000000 -0.343981481 -1.284861111 0.280532407 0.576250000
## [71] -0.505231481 -0.861111111 1.071782407 0.718750000 0.417268519
## [76] -2.398611111 1.503032407 -0.095000000 NA NA
## [81] NA NA NA NA
Error_aleatorio(AirPassengers, "multiplicativa", 12,12)
## [[1]]
##
## [[2]]
## [1] NA NA NA NA NA NA 1.1558093
## [8] 1.1294884 1.0820003 0.9531069 0.7982866 0.8831866 0.8919810 0.9750484
## [15] 1.0348292 0.9610460 0.9260356 1.1059426 1.1945429 1.1531475 1.1039001
## [22] 0.9228865 0.7448828 0.8788043 0.9394632 0.9682699 1.0890986 0.9644744
## [29] 1.0505985 1.0841728 1.1506353 1.1133274 1.0675811 0.9434746 0.8119820
## [36] 0.8947702 0.9506169 0.9955262 1.0109157 0.9188833 0.9623658 1.1464344
## [43] 1.1499698 1.1765393 1.0522145 0.9537152 0.8094005 0.8829499 0.9244738
## [50] 0.9238127 1.0577882 1.0237718 1.0403584 1.1136954 1.1600953 1.1722529
## [57] 1.0725149 0.9675741 0.7940575 0.8654602 0.9108616 0.8401266 1.0019068
## [64] 0.9424158 1.0110009 1.1435699 1.2433891 1.1663525 1.0667605 0.9424118
## [71] 0.7929271 0.8649062 0.9409087 0.8998425 0.9751173 0.9492236 0.9869511
## [78] 1.1505485 1.2613347 1.1646860 1.0831127 0.9495761 0.7796452 0.8838339
## [85] 0.9327647 0.9073055 0.9851324 0.9447211 0.9976300 1.1775856 1.2409518
## [92] 1.1852579 1.0805463 0.9336269 0.7879782 0.8636457 0.9208237 0.8781549
## [99] 0.9856831 0.9351881 0.9914890 1.1836642 1.2462428 1.2217350 1.1050984
## [106] 0.9595767 0.8102096 0.8733357 0.9223915 0.8665834 0.9445218 0.8893516
## [113] 0.9706693 1.1759561 1.2732768 1.2782481 1.0641159 0.9471922 0.7776794
## [120] 0.8210001 0.9104345 0.8650341 0.9760584 0.9237019 1.0168113 1.1424094
## [127] 1.2598315 1.2476007 1.0768451 0.9505526 0.8039911 0.8728057 0.9302741
## [134] 0.8727744 0.8918293 0.9538880 1.0164067 1.1598488 NA NA
## [141] NA NA NA NA
La función recibe los siguientes parámetros:
s_tiempo:objeto de la clase ts (time series).
modelo: Aquà tendremos dos casos, si se desea aditiva o multiplicativa, en algún otro caso se manda el mensaje de error.
periocidad: Hace referencia a la temporalidad de la serie.
media_movil: Corresponde al orden de las medias móviles.
Descomponer_serie <- function(serie_tiempo, modelo, periocidad, media_movil){
if(class(serie_tiempo)=="ts"){
if(modelo=="aditiva" | modelo=="multiplicativa"){
tend_s<-Tendencia(serie_tiempo,media_movil)
estac_s<-Estacionalidad(serie_tiempo, modelo, periocidad, media_movil)
error <- Error_aleatorio(serie_tiempo, modelo, periocidad, media_movil)
Tabla<-data.frame(Tiempo=c(1:length(serie_tiempo)), Serie=as.numeric(serie_tiempo), Tendencia=tend_s[[2]], Estacionalidad=estac_s[[2]], Comp_Aleatorio=error[[2]])
g1<-ggplot(Tabla)+geom_line(aes(x=Tiempo, y=Serie), color="deepskyblue", size=1.2)+geom_point(aes(x=Tiempo, y=Serie), color="darkorange2")+labs(x="Tiempo", y="Observaciones", title = "Serie de Tiempo")+ theme_bw()
plots<-grid.arrange(g1, tend_s[[1]], estac_s[[1]], error[[1]], ncol=2)
return(list(Tabla,plots))
} else{
stop('El parámetro no es correcto')
}
}else{
stop(ggplot()+
annotate(geom = "text", x = 1, y = 1, label = "Error!\n, class(s1) is not ts. ", size = 12,
colour = "#FF0000")+
theme(axis.title = element_blank(),
axis.text = element_blank(), axis.ticks = element_blank(),
panel.background = element_blank()))
}
}
Descomponer_serie(JohnsonJohnson, "aditiva",6 ,6)
## [[1]]
## Tiempo Serie Tendencia Estacionalidad Comp_Aleatorio
## 1 1 0.71 NA 0.2267500 NA
## 2 2 0.63 NA 0.3166667 NA
## 3 3 0.85 NA 0.3048684 NA
## 4 4 0.44 0.6725000 -0.8560833 0.623583333
## 5 5 0.61 0.6833333 0.2267500 -0.300083333
## 6 6 0.69 0.6658333 0.3166667 -0.292500000
## 7 7 0.92 0.6825000 0.3048684 -0.067368421
## 8 8 0.55 0.7358333 -0.8560833 0.670250000
## 9 9 0.72 0.7541667 0.2267500 -0.260916667
## 10 10 0.77 0.7391667 0.3166667 -0.285833333
## 11 11 0.92 0.7525000 0.3048684 -0.137368421
## 12 12 0.60 0.7966667 -0.8560833 0.659416667
## 13 13 0.83 0.8200000 0.2267500 -0.216750000
## 14 14 0.80 0.8200000 0.3166667 -0.336666667
## 15 15 1.00 0.8533333 0.3048684 -0.158201754
## 16 16 0.77 0.9208333 -0.8560833 0.705250000
## 17 17 0.92 0.9716667 0.2267500 -0.278416667
## 18 18 1.00 1.0016667 0.3166667 -0.318333333
## 19 19 1.24 1.0591667 0.3048684 -0.124035088
## 20 20 1.00 1.1475000 -0.8560833 0.708583333
## 21 21 1.16 1.2125000 0.2267500 -0.279250000
## 22 22 1.30 1.2350000 0.3166667 -0.251666667
## 23 23 1.45 1.2683333 0.3048684 -0.123201754
## 24 24 1.25 1.3583333 -0.8560833 0.747750000
## 25 25 1.26 1.4383333 0.2267500 -0.405083333
## 26 26 1.38 1.4666667 0.3166667 -0.403333333
## 27 27 1.86 1.5016667 0.3048684 0.053464912
## 28 28 1.56 1.5775000 -0.8560833 0.838583333
## 29 29 1.53 1.6650000 0.2267500 -0.361750000
## 30 30 1.59 1.6775000 0.3166667 -0.404166667
## 31 31 1.83 1.6925000 0.3048684 -0.167368421
## 32 32 1.86 1.8025000 -0.8560833 0.913583333
## 33 33 1.53 1.9250000 0.2267500 -0.621750000
## 34 34 2.07 2.0075000 0.3166667 -0.254166667
## 35 35 2.34 2.0825000 0.3048684 -0.047368421
## 36 36 2.25 2.2275000 -0.8560833 0.878583333
## 37 37 2.16 2.3400000 0.2267500 -0.406750000
## 38 38 2.43 2.3925000 0.3166667 -0.279166667
## 39 39 2.70 2.5275000 0.3048684 -0.132368421
## 40 40 2.25 2.7525000 -0.8560833 0.353583333
## 41 41 2.79 2.9775000 0.2267500 -0.414250000
## 42 42 3.42 3.1500000 0.3166667 -0.046666667
## 43 43 3.69 3.3975000 0.3048684 -0.012368421
## 44 44 3.60 3.6975000 -0.8560833 0.758583333
## 45 45 3.60 3.8775000 0.2267500 -0.504250000
## 46 46 4.32 4.0275000 0.3166667 -0.024166667
## 47 47 4.32 4.2450000 0.3048684 -0.229868421
## 48 48 4.05 4.4850000 -0.8560833 0.421083333
## 49 49 4.86 4.6125000 0.2267500 0.020750000
## 50 50 5.04 4.7250000 0.3166667 -0.001666667
## 51 51 5.04 4.9800000 0.3048684 -0.244868421
## 52 52 4.41 5.2725000 -0.8560833 -0.006416667
## 53 53 5.58 5.4375000 0.2267500 -0.084250000
## 54 54 5.85 5.5425000 0.3166667 -0.009166667
## 55 55 6.57 5.7900000 0.3048684 0.475131579
## 56 56 5.31 6.0675000 -0.8560833 0.098583333
## 57 57 6.03 6.1800000 0.2267500 -0.376750000
## 58 58 6.39 6.2100000 0.3166667 -0.136666667
## 59 59 6.93 6.4425000 0.3048684 0.182631579
## 60 60 5.85 6.7950000 -0.8560833 -0.088916667
## 61 61 6.93 6.9225000 0.2267500 -0.219250000
## 62 62 7.74 6.9675000 0.3166667 0.455833333
## 63 63 7.83 7.2900000 0.3048684 0.235131579
## 64 64 6.12 7.6575000 -0.8560833 -0.681416667
## 65 65 7.74 7.6950000 0.2267500 -0.181750000
## 66 66 8.91 7.7625000 0.3166667 0.830833333
## 67 67 8.28 8.2500000 0.3048684 -0.274868421
## 68 68 6.84 8.7450000 -0.8560833 -1.048916667
## 69 69 9.54 8.8800000 0.2267500 0.433250000
## 70 70 10.26 9.1650000 0.3166667 0.778333333
## 71 71 9.54 9.9000000 0.3048684 -0.664868421
## 72 72 8.73 10.5525000 -0.8560833 -0.966416667
## 73 73 11.88 10.6575000 0.2267500 0.995750000
## 74 74 12.06 10.9200000 0.3166667 0.823333333
## 75 75 12.15 11.6475000 0.3048684 0.197631579
## 76 76 8.91 12.2475000 -0.8560833 -2.481416667
## 77 77 14.04 12.3225000 0.2267500 1.490750000
## 78 78 12.96 12.4875000 0.3166667 0.155833333
## 79 79 14.85 13.3050000 0.3048684 1.240131579
## 80 80 9.99 13.9500000 -0.8560833 -3.103916667
## 81 81 16.20 14.0025000 0.2267500 1.970750000
## 82 82 14.67 NA 0.3166667 NA
## 83 83 16.02 NA 0.3048684 NA
## 84 84 11.61 NA -0.8560833 NA
##
## [[2]]
## TableGrob (2 x 2) "arrange": 4 grobs
## z cells name grob
## 1 1 (1-1,1-1) arrange gtable[layout]
## 2 2 (1-1,2-2) arrange gtable[layout]
## 3 3 (2-2,1-1) arrange gtable[layout]
## 4 4 (2-2,2-2) arrange gtable[layout]
ejercicio2<-c(as.numeric(fechas[123], "%y"),as.numeric(fechas[123], "%m"))
s1<-ts(series[500,-c(1:7)] %>% t() %>% na.omit(),frequency = 12, start=ejercicio2[1])
Descomponer_serie(s1,"multiplicativa",10,10)
## [[1]]
## Tiempo Serie Tendencia Estacionalidad Comp_Aleatorio
## 1 1 54 NA 1.0216519 NA
## 2 2 151 NA 0.9971538 NA
## 3 3 346 NA 0.9631256 NA
## 4 4 407 NA 1.0343934 NA
## 5 5 540 NA 1.0216519 NA
## 6 6 811 662.85 0.9971538 1.2269968
## 7 7 930 768.80 0.9631256 1.2559914
## 8 8 860 881.25 1.0343934 0.9434385
## 9 9 1088 983.30 1.0216519 1.0830286
## 10 10 944 1084.70 0.9971538 0.8727708
## 11 11 1049 1167.85 0.9631256 0.9326217
## 12 12 1275 1222.35 1.0343934 1.0083908
## 13 13 1471 1278.05 1.0216519 1.1265796
## 14 14 1323 1328.25 0.9971538 0.9988905
## 15 15 1652 1366.65 0.9631256 1.2550754
## 16 16 1362 1398.40 1.0343934 0.9415859
## 17 17 1469 1416.35 1.0216519 1.0151922
## 18 18 1435 1398.95 0.9971538 1.0286972
## 19 19 1517 1381.05 0.9631256 1.1404947
## 20 20 1283 1364.25 1.0343934 0.9091739
## 21 21 1345 1348.15 1.0216519 0.9765200
## 22 22 1338 1330.30 0.9971538 1.0086590
## 23 23 1060 1304.15 0.9631256 0.8439086
## 24 24 1376 1271.10 1.0343934 1.0465331
## 25 25 1263 1230.65 1.0216519 1.0045368
## 26 26 1429 1199.95 0.9971538 1.1942821
## 27 27 1045 1168.40 0.9631256 0.9286281
## 28 28 1336 1149.40 1.0343934 1.1236978
## 29 29 955 1114.35 1.0216519 0.8388394
## 30 30 1036 1065.30 0.9971538 0.9752718
## 31 31 978 1018.15 0.9631256 0.9973422
## 32 32 1074 987.20 1.0343934 1.0517521
## 33 33 944 965.95 1.0216519 0.9565648
## 34 34 791 942.70 0.9971538 0.8414742
## 35 35 867 924.90 0.9631256 0.9732881
## 36 36 882 907.65 1.0343934 0.9394300
## 37 37 973 886.10 1.0216519 1.0747988
## 38 38 983 856.75 0.9971538 1.1506341
## 39 39 843 848.65 0.9631256 1.0313737
## 40 40 792 841.65 1.0343934 0.9097203
## 41 41 877 815.95 1.0216519 1.0520421
## 42 42 744 788.15 0.9971538 0.9466772
## 43 43 687 762.50 0.9631256 0.9354789
## 44 44 886 748.05 1.0343934 1.1450313
## 45 45 632 738.90 1.0216519 0.8371986
## 46 46 603 723.10 0.9971538 0.8362898
## 47 47 696 715.50 0.9631256 1.0099891
## 48 48 747 716.60 1.0343934 1.0077622
## 49 49 790 706.35 1.0216519 1.0947229
## 50 50 662 700.50 0.9971538 0.9477367
## 51 51 691 709.65 0.9631256 1.0109995
## 52 52 778 713.20 1.0343934 1.0545872
## 53 53 675 704.70 1.0216519 0.9375546
## 54 54 693 683.85 0.9971538 1.0162726
## 55 55 708 675.25 0.9631256 1.0886437
## 56 56 710 686.70 1.0343934 0.9995524
## 57 57 660 697.20 1.0216519 0.9265815
## 58 58 613 702.20 0.9971538 0.8754624
## 59 59 507 708.45 0.9631256 0.7430463
## 60 60 773 709.70 1.0343934 1.0529771
## 61 61 809 704.10 1.0216519 1.1246341
## 62 62 870 705.20 0.9971538 1.2372139
## 63 63 683 710.25 0.9631256 0.9984505
## 64 64 810 720.85 1.0343934 1.0863115
## 65 65 616 719.20 1.0216519 0.8383553
## 66 66 690 698.75 0.9971538 0.9902962
## 67 67 702 680.70 0.9631256 1.0707756
## 68 68 672 678.10 1.0343934 0.9580535
## 69 69 660 678.80 1.0216519 0.9516980
## 70 70 587 675.95 0.9971538 0.8708861
## 71 71 586 682.85 0.9631256 0.8910240
## 72 72 732 682.65 1.0343934 1.0366383
## 73 73 769 676.95 1.0216519 1.1119028
## 74 74 738 678.60 0.9971538 1.0906373
## 75 75 631 691.65 0.9631256 0.9472401
## 76 76 813 712.65 1.0343934 1.1028806
## 77 77 575 720.40 1.0216519 0.7812521
## 78 78 685 711.60 0.9971538 0.9653671
## 79 79 680 708.35 0.9631256 0.9967313
## 80 80 828 724.80 1.0343934 1.1044000
## 81 81 765 740.50 1.0216519 1.0111916
## 82 82 708 756.90 0.9971538 0.9380643
## 83 83 617 779.80 0.9631256 0.8215217
## 84 84 825 799.10 1.0343934 0.9980839
## 85 85 873 808.35 1.0216519 1.0570898
## 86 86 885 819.05 0.9971538 1.0836043
## 87 87 831 835.95 0.9631256 1.0321381
## 88 88 887 862.70 1.0343934 0.9939810
## 89 89 864 882.90 1.0216519 0.9578539
## 90 90 829 879.40 0.9971538 0.9453789
## 91 91 978 877.00 0.9631256 1.1578608
## 92 92 833 892.75 1.0343934 0.9020475
## 93 93 1027 917.20 1.0216519 1.0959821
## 94 94 819 935.45 0.9971538 0.8780135
## 95 95 809 953.25 0.9631256 0.8811682
## 96 96 901 968.30 1.0343934 0.8995579
## 97 97 1130 991.80 1.0216519 1.1151965
## 98 98 1077 1017.25 0.9971538 1.0617588
## 99 99 1039 1035.90 0.9631256 1.0413934
## 100 100 1010 1064.80 1.0343934 0.9169963
## 101 101 1098 1094.30 1.0216519 0.9821165
## 102 102 1183 1102.75 0.9971538 1.0758347
## 103 103 1186 1109.95 0.9631256 1.1094261
## 104 104 1033 1137.80 1.0343934 0.8777051
## 105 105 1173 1169.30 1.0216519 0.9819042
## 106 106 1127 1198.75 0.9971538 0.9428295
## 107 107 1073 1230.50 0.9631256 0.9053890
## 108 108 1278 1248.35 1.0343934 0.9897118
## 109 109 1395 1280.10 1.0216519 1.0666633
## 110 110 1284 NA 0.9971538 NA
## 111 111 1413 NA 0.9631256 NA
## 112 112 1503 NA 1.0343934 NA
## 113 113 1223 NA 1.0216519 NA
## 114 114 1631 NA 0.9971538 NA
##
## [[2]]
## TableGrob (2 x 2) "arrange": 4 grobs
## z cells name grob
## 1 1 (1-1,1-1) arrange gtable[layout]
## 2 2 (1-1,2-2) arrange gtable[layout]
## 3 3 (2-2,1-1) arrange gtable[layout]
## 4 4 (2-2,2-2) arrange gtable[layout]
Descomponer_serie(AirPassengers, "aditiva",6 ,6)
## [[1]]
## Tiempo Serie Tendencia Estacionalidad Comp_Aleatorio
## 1 1 112 NA -5.788095 NA
## 2 2 118 NA -9.424020 NA
## 3 3 132 NA 6.009804 NA
## 4 4 129 127.5000 11.700000 -10.2000000
## 5 5 121 133.0000 -5.788095 -6.2119048
## 6 6 135 135.8333 -9.424020 8.5906863
## 7 7 148 135.3333 6.009804 6.6568627
## 8 8 148 133.0833 11.700000 3.2166667
## 9 9 136 130.2500 -5.788095 11.5380952
## 10 10 119 126.0833 -9.424020 2.3406863
## 11 11 104 121.5000 6.009804 -23.5098039
## 12 12 118 120.0833 11.700000 -13.7833333
## 13 13 115 121.8333 -5.788095 -1.0452381
## 14 14 126 124.9167 -9.424020 10.5073529
## 15 15 141 129.2500 6.009804 5.7401961
## 16 16 135 136.4167 11.700000 -13.1166667
## 17 17 125 144.6667 -5.788095 -13.8785714
## 18 18 149 149.7500 -9.424020 8.6740196
## 19 19 170 151.0000 6.009804 12.9901961
## 20 20 170 149.9167 11.700000 8.3833333
## 21 21 158 148.2500 -5.788095 15.5380952
## 22 22 133 145.4167 -9.424020 -2.9926471
## 23 23 114 141.6667 6.009804 -33.6764706
## 24 24 140 141.6667 11.700000 -13.3666667
## 25 25 145 145.8333 -5.788095 4.9547619
## 26 26 150 153.1667 -9.424020 6.2573529
## 27 27 178 161.1667 6.009804 10.8235294
## 28 28 163 168.8333 11.700000 -17.5333333
## 29 29 172 177.4167 -5.788095 0.3714286
## 30 30 178 182.0000 -9.424020 5.4240196
## 31 31 199 182.4167 6.009804 10.5735294
## 32 32 199 180.1667 11.700000 7.1333333
## 33 33 184 177.0000 -5.788095 12.7880952
## 34 34 162 173.6667 -9.424020 -2.2426471
## 35 35 146 169.7500 6.009804 -29.7598039
## 36 36 166 168.9167 11.700000 -14.6166667
## 37 37 171 171.2500 -5.788095 5.5380952
## 38 38 180 175.9167 -9.424020 13.5073529
## 39 39 193 183.3333 6.009804 3.6568627
## 40 40 181 192.5833 11.700000 -23.2833333
## 41 41 183 202.6667 -5.788095 -13.8785714
## 42 42 218 209.1667 -9.424020 18.2573529
## 43 43 230 211.3333 6.009804 12.6568627
## 44 44 242 211.2500 11.700000 19.0500000
## 45 45 209 208.3333 -5.788095 6.4547619
## 46 46 191 203.5000 -9.424020 -3.0759804
## 47 47 172 196.8333 6.009804 -30.8431373
## 48 48 194 195.2500 11.700000 -12.9500000
## 49 49 196 201.1667 -5.788095 0.6214286
## 50 50 196 209.5833 -9.424020 -4.1593137
## 51 51 236 218.4167 6.009804 11.5735294
## 52 52 235 228.1667 11.700000 -4.8666667
## 53 53 229 240.1667 -5.788095 -5.3785714
## 54 54 243 246.5833 -9.424020 5.8406863
## 55 55 264 244.6667 6.009804 13.3235294
## 56 56 272 238.5833 11.700000 21.7166667
## 57 57 237 231.0000 -5.788095 11.7880952
## 58 58 211 222.5000 -9.424020 -2.0759804
## 59 59 180 210.5000 6.009804 -36.5098039
## 60 60 201 203.3333 11.700000 -14.0333333
## 61 61 204 204.5000 -5.788095 5.2880952
## 62 62 188 210.3333 -9.424020 -12.9093137
## 63 63 235 220.0833 6.009804 8.9068627
## 64 64 227 233.5000 11.700000 -18.2000000
## 65 65 234 250.4167 -5.788095 -10.6285714
## 66 66 264 261.1667 -9.424020 12.2573529
## 67 67 302 263.3333 6.009804 32.6568627
## 68 68 293 260.9167 11.700000 20.3833333
## 69 69 259 255.4167 -5.788095 9.3714286
## 70 70 229 247.5000 -9.424020 -9.0759804
## 71 71 203 237.5000 6.009804 -40.5098039
## 72 72 229 233.1667 11.700000 -15.8666667
## 73 73 242 237.1667 -5.788095 10.6214286
## 74 74 233 246.0833 -9.424020 -3.6593137
## 75 75 267 258.8333 6.009804 2.1568627
## 76 76 269 276.1667 11.700000 -18.8666667
## 77 77 270 295.8333 -5.788095 -20.0452381
## 78 78 315 309.0833 -9.424020 15.3406863
## 79 79 364 313.2500 6.009804 44.7401961
## 80 80 347 310.9167 11.700000 24.3833333
## 81 81 312 305.0833 -5.788095 12.7047619
## 82 82 274 295.3333 -9.424020 -11.9093137
## 83 83 237 282.8333 6.009804 -51.8431373
## 84 84 278 277.4167 11.700000 -11.1166667
## 85 85 284 281.0833 -5.788095 8.7047619
## 86 86 277 291.0833 -9.424020 -4.6593137
## 87 87 317 305.8333 6.009804 5.1568627
## 88 88 313 324.5833 11.700000 -23.2833333
## 89 89 318 346.0000 -5.788095 -22.2119048
## 90 90 374 359.8333 -9.424020 23.5906863
## 91 91 413 362.4167 6.009804 44.5735294
## 92 92 405 357.9167 11.700000 35.3833333
## 93 93 355 348.3333 -5.788095 12.4547619
## 94 94 306 334.5000 -9.424020 -19.0759804
## 95 95 271 317.6667 6.009804 -52.6764706
## 96 96 306 309.0833 11.700000 -14.7833333
## 97 97 315 312.6667 -5.788095 8.1214286
## 98 98 301 323.1667 -9.424020 -12.7426471
## 99 99 356 339.8333 6.009804 10.1568627
## 100 100 348 362.0000 11.700000 -25.7000000
## 101 101 355 388.3333 -5.788095 -27.5452381
## 102 102 422 406.1667 -9.424020 25.2573529
## 103 103 465 410.0833 6.009804 48.9068627
## 104 104 467 405.8333 11.700000 49.4666667
## 105 105 404 394.5000 -5.788095 15.2880952
## 106 106 347 376.9167 -9.424020 -20.4926471
## 107 107 305 354.0833 6.009804 -55.0931373
## 108 108 336 338.1667 11.700000 -13.8666667
## 109 109 340 334.7500 -5.788095 11.0380952
## 110 110 318 339.6667 -9.424020 -12.2426471
## 111 111 362 352.7500 6.009804 3.2401961
## 112 112 348 373.5833 11.700000 -37.2833333
## 113 113 363 401.7500 -5.788095 -32.9619048
## 114 114 435 420.8333 -9.424020 23.5906863
## 115 115 491 425.2500 6.009804 59.7401961
## 116 116 505 421.7500 11.700000 71.5500000
## 117 117 404 409.1667 -5.788095 0.6214286
## 118 118 359 390.0833 -9.424020 -21.6593137
## 119 119 310 365.5833 6.009804 -61.5931373
## 120 120 337 352.1667 11.700000 -26.8666667
## 121 121 360 355.4167 -5.788095 10.3714286
## 122 122 342 367.6667 -9.424020 -16.2426471
## 123 123 406 388.0833 6.009804 11.9068627
## 124 124 396 415.0000 11.700000 -30.7000000
## 125 125 420 448.7500 -5.788095 -22.9619048
## 126 126 472 471.5833 -9.424020 9.8406863
## 127 127 548 477.2500 6.009804 64.7401961
## 128 128 559 473.3333 11.700000 73.9666667
## 129 129 463 462.9167 -5.788095 5.8714286
## 130 130 407 446.4167 -9.424020 -29.9926471
## 131 131 362 421.5000 6.009804 -65.5098039
## 132 132 405 403.8333 11.700000 -10.5333333
## 133 133 417 404.6667 -5.788095 18.1214286
## 134 134 391 418.3333 -9.424020 -17.9093137
## 135 135 419 438.3333 6.009804 -25.3431373
## 136 136 461 466.2500 11.700000 -16.9500000
## 137 137 472 501.2500 -5.788095 -23.4619048
## 138 138 535 526.5833 -9.424020 17.8406863
## 139 139 622 534.0000 6.009804 81.9901961
## 140 140 606 527.1667 11.700000 67.1333333
## 141 141 508 511.7500 -5.788095 2.0380952
## 142 142 461 NA -9.424020 NA
## 143 143 390 NA 6.009804 NA
## 144 144 432 NA 11.700000 NA
##
## [[2]]
## TableGrob (2 x 2) "arrange": 4 grobs
## z cells name grob
## 1 1 (1-1,1-1) arrange gtable[layout]
## 2 2 (1-1,2-2) arrange gtable[layout]
## 3 3 (2-2,1-1) arrange gtable[layout]
## 4 4 (2-2,2-2) arrange gtable[layout]