Ejercicio Video
Ejercicio Video
2023-01-22
#Ejecutamos los paquetes
library(readxl)
library(tseries)
library(astsa)
library(forecast)
##
## Attaching package: 'forecast'
library(tidyverse)
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
library(lubridate)
library(foreign)
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
##
## Attaching package: 'xts'
##
## The following objects are masked from 'package:dplyr':
##
## first, last
##
## Loading required package: TTR
## # A tibble: 87 × 1
## Precio
## <dbl>
## 1 98.0
## 2 103.
## 3 101.
## 4 97.8
## 5 97.9
## 6 97.0
## 7 99.7
## 8 99.2
## 9 98.1
## 10 93.9
## # … with 77 more rows
#Graficamos la serie
plot(Arimar.ts)
# Comprobamos si es estacionaria
adf.test(Arimar.ts,alternative = "stationary")
##
## Augmented Dickey-Fuller Test
##
## data: Arimar.ts
## Dickey-Fuller = -1.6185, Lag order = 4, p-value = 0.7328
## alternative hypothesis: stationary
##
## Augmented Dickey-Fuller Test
##
## data: seriedif
## Dickey-Fuller = -3.3327, Lag order = 4, p-value = 0.07162
## alternative hypothesis: stationary
##
## Call:
## arima(x = Arimar.ts, order = c(1, 2, 1))
##
## Coefficients:
## ar1 ma1
## 0.4173 -1.000
## s.e. 0.1100 0.047
##
## sigma^2 estimated as 17.51: log likelihood = -244.07, aic = 494.14
tsdiag(modelo1)
#Verificamos el ajuste de los datos al modelo Arima
Box.test(residuals(modelo1), type = "Ljung-Box")
##
## Box-Ljung test
##
## data: residuals(modelo1)
## X-squared = 0.055051, df = 1, p-value = 0.8145
error=residuals(modelo1)
#Graficamos el error
plot(error)
#Realizamos el pronóstico con el paquete forecast:
pronostico = forecast ::forecast(modelo1, h=10, level=95)
pronostico
## Point Forecast Lo 95 Hi 95
## Apr 2020 21.856654 13.608341 30.10497
## May 2020 18.398192 4.014833 32.78155
## Jun 2020 16.445341 -3.178974 36.06966
## Jul 2020 15.120795 -9.040917 39.28251
## Aug 2020 14.058445 -14.118113 42.23500
## Sep 2020 13.105513 -18.699183 44.91021
## Oct 2020 12.198241 -22.943549 47.34003
## Nov 2020 11.310024 -26.944576 49.56462
## Dec 2020 10.429758 -30.760523 51.62004
## Jan 2021 9.552811 -34.430032 53.53565
#Graficamos
plot(pronostico)
#Programos los nombres de los ejes y el título del gráfico.
plot(pronostico, main="Pronóstico del precio del petróleo", sub= "tiempo",
ylab= "precio del petróleo")