# Ruido blanco para t=0,...,100
# mu = 0
# sigma^2 = 1--->sigma(desviacion estandar) = 1
Zt <- rnorm(100, 0, 1)
ts.plot(Zt, main="Ruido blanco")
grid()
Zt[2]
v1 <- c(0)
# acceder al primer elemento del vector
v1[2] <- 7
v1[3] <- 700
v1[4] <- 7000
v1
# Alpha
a0 = 2
# alpha1 --> (-1, 1)
a1 = 0.5
# inicializar el modelo
# t=0
Xt <- c(0)
# rellenar el resto de valores Xt
for (t in 2:100){
Xt[t] = a0 + a1 * Xt[t-1] + Zt[t]
}
ts.plot(Xt, main="AR(1)")
# Alpha
a0 = 2
# alpha1 --> (-1, 1)
a1 = 0.5
a2 = -0.8
# inicializar el modelo
# t=0
Xt <- c(0,0)
# rellenar el resto de valores Xt
for (t in 3:100){
Xt[t] = a0 + a1 * Xt[t-1] + a2 * Xt[t-2] + Zt[t]
}
ts.plot(Xt, main="AR(2)")
# beta1 (-1,1)
b1 = 0.5
# inicializar el modelo
# t=0
Xt <- c(0)
# rellenar el resto de valores Xt
for (t in 2:100){
Xt[t] = Zt[t] + b1 * Zt[t-1]
}
ts.plot(Xt, main="MA(1)")
# alpha's
a0 = 2
a1 = -0.8
# beta1 (-1,1)
b1 = 0.5
# inicializar el modelo
# t=0
Xt <- c(0)
# rellenar el resto de valores Xt
for (t in 2:100){
Xt[t] = a0 + a1 * Xt[t-1] + Zt[t] + b1 * Zt[t-1]
}
ts.plot(Xt, main="ARMA(p=1,q=1)")
# p,d,q alpha1 beta1
xt <- arima.sim(model= list(c(1,0,1), ar=-0.9, ma=0.5) , n=100)
ts.plot(xt, main="ARMA(1,1), alpha1=-0.9, beta1=0.5")
# Si vemos tendencia y ciclo estacional:
# d=2
# p,d,q alpha1 beta1
#xt <- arima.sim(model= list(c(1,2,1), ar=-0.9, ma=0.5) , n=100)
#ts.plot(xt, main="ARMA(1,1), alpha1=-0.9, beta1=0.5")
# Consideremos
Xt <- AirPassengers
Xt
Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
1949 | 112 | 118 | 132 | 129 | 121 | 135 | 148 | 148 | 136 | 119 | 104 | 118 |
1950 | 115 | 126 | 141 | 135 | 125 | 149 | 170 | 170 | 158 | 133 | 114 | 140 |
1951 | 145 | 150 | 178 | 163 | 172 | 178 | 199 | 199 | 184 | 162 | 146 | 166 |
1952 | 171 | 180 | 193 | 181 | 183 | 218 | 230 | 242 | 209 | 191 | 172 | 194 |
1953 | 196 | 196 | 236 | 235 | 229 | 243 | 264 | 272 | 237 | 211 | 180 | 201 |
1954 | 204 | 188 | 235 | 227 | 234 | 264 | 302 | 293 | 259 | 229 | 203 | 229 |
1955 | 242 | 233 | 267 | 269 | 270 | 315 | 364 | 347 | 312 | 274 | 237 | 278 |
1956 | 284 | 277 | 317 | 313 | 318 | 374 | 413 | 405 | 355 | 306 | 271 | 306 |
1957 | 315 | 301 | 356 | 348 | 355 | 422 | 465 | 467 | 404 | 347 | 305 | 336 |
1958 | 340 | 318 | 362 | 348 | 363 | 435 | 491 | 505 | 404 | 359 | 310 | 337 |
1959 | 360 | 342 | 406 | 396 | 420 | 472 | 548 | 559 | 463 | 407 | 362 | 405 |
1960 | 417 | 391 | 419 | 461 | 472 | 535 | 622 | 606 | 508 | 461 | 390 | 432 |
ts.plot(Xt)
# importacion necesaria
library(forecast)
fit <- auto.arima(Xt)
fit
Registered S3 method overwritten by 'quantmod': method from as.zoo.data.frame zoo
Series: Xt ARIMA(2,1,1)(0,1,0)[12] Coefficients: ar1 ar2 ma1 0.5960 0.2143 -0.9819 s.e. 0.0888 0.0880 0.0292 sigma^2 = 132.3: log likelihood = -504.92 AIC=1017.85 AICc=1018.17 BIC=1029.35
# pronostico a 12 tiempos futuros (un año)
pronostico <- forecast(fit,12,level=95)
# graficamos
plot(pronostico, main="Pronóstico con auto.arima()")
grid()
pronosticos_t <- data.frame(pronostico$mean,pronostico$lower,pronostico$upper)
pronosticos_t
pronostico.mean | X95. | X95..1 |
---|---|---|
<ts> | <dbl> | <dbl> |
445.6349 | 423.0851 | 468.1847 |
420.3950 | 393.9304 | 446.8596 |
449.1983 | 419.4892 | 478.9074 |
491.8399 | 460.0092 | 523.6707 |
503.3945 | 469.9953 | 536.7937 |
566.8625 | 532.3007 | 601.4242 |
654.2602 | 618.8122 | 689.7081 |
638.5975 | 602.4630 | 674.7320 |
540.8837 | 504.2081 | 577.5594 |
494.1266 | 457.0177 | 531.2356 |
423.3327 | 385.8715 | 460.7940 |
465.5076 | 427.7556 | 503.2596 |
# Xt: Ts, St, Zt
plot(decompose(Xt))