0% found this document useful (0 votes)
49 views12 pages

Ejercicio Video

1) The document loads several packages and loads/formats a time series dataset (Arimar) of stock prices from 2013 to 2020. 2) It performs unit root tests to check for stationarity and takes two differences of the time series to make it stationary. 3) An ARIMA(1,2,1) model is fitted to the stationary time series and diagnostic checks are performed. 4) A 10-period forecast is made using the fitted ARIMA model.

Uploaded by

Ariana J.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views12 pages

Ejercicio Video

1) The document loads several packages and loads/formats a time series dataset (Arimar) of stock prices from 2013 to 2020. 2) It performs unit root tests to check for stationarity and takes two differences of the time series to make it stationary. 3) An ARIMA(1,2,1) model is fitted to the stationary time series and diagnostic checks are performed. 4) A 10-period forecast is made using the fitted ARIMA model.

Uploaded by

Ariana J.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Ejercicio-video

2023-01-22
#Ejecutamos los paquetes
library(readxl)
library(tseries)

## Registered S3 method overwritten by 'quantmod':


## method from
## as.zoo.data.frame zoo

library(astsa)
library(forecast)

##
## Attaching package: 'forecast'

## The following object is masked from 'package:astsa':


##
## gas

library(tidyverse)

## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──

## ✔ ggplot2 3.4.0 ✔ purrr 1.0.0


## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.5.0
## ✔ readr 2.1.3 ✔ forcats 0.5.2
## ── Conflicts ──────────────────────────────────────────
tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()

library(lubridate)

## Loading required package: timechange


##
## Attaching package: 'lubridate'
##
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union

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

#Cargamos la base de datos


Arimar= read_excel("Arimar.xlsx", sheet = "Precios")
Arimar

## # 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

#Programamos para que sea una serie de tiempo


Arimar.ts=ts(Arimar, start=c(2013,1), frequency=12)
Arimar.ts

## Jan Feb Mar Apr May Jun Jul


## 2013 97.98278 103.35294 100.77090 97.75538 97.90686 96.99315 99.67255
## 2014 88.31081 90.82224 91.39522 93.88863 95.08631 97.01105 93.32017
## 2015 40.45127 45.98606 46.01733 49.32555 52.03910 52.82573 45.16595
## 2016 23.10238 23.72933 28.32656 31.33582 36.72636 39.80715 37.97153
## 2017 44.50008 44.17497 41.93243 43.22118 43.85074 41.15105 43.87771
## 2018 57.45399 56.15676 57.22458 58.15973 62.89929 64.63686 66.42162
## 2019 54.05666 57.37981 59.46449 62.07703 60.33674 56.90537 57.87580
## 2020 49.79299 44.63505 28.92302
## Aug Sep Oct Nov Dec
## 2013 99.24414 98.14572 93.87329 88.48590 89.40966
## 2014 89.50745 84.12824 72.50936 63.47980 50.18138
## 2015 38.41927 36.29036 34.86077 32.50883 26.19610
## 2016 37.74090 36.82666 40.66836 38.36162 42.32596
## 2017 45.55484 48.16237 48.89114 53.34505 54.05576
## 2018 64.26567 68.36348 71.15330 59.82583 51.86867
## 2019 49.58496 55.05522 51.00463 50.68969 54.54901
## 2020

#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

#Dado que el valor p-value= 0.7328 es mayor que 0.05 el modelo no es


estacionario por lo tanto debemos diferenciar hasta que tenga un
comportamiento estacionario.
seriedif=diff(Arimar.ts)
seriedif
## Jan Feb Mar Apr May
## 2013 5.37015544 -2.58203596 -3.01551721 0.15147672
## 2014 -1.09885152 2.51143263 0.57298352 2.49340638 1.19767879
## 2015 -9.73010322 5.53478826 0.03127119 3.30821931 2.71355401
## 2016 -3.09371320 0.62694765 4.59723325 3.00925512 5.39054257
## 2017 2.17411883 -0.32511051 -2.24254299 1.28875229 0.62956241
## 2018 3.39823914 -1.29723298 1.06782245 0.93514799 4.73955640
## 2019 2.18799111 3.32314972 2.08468274 2.61253762 -1.74029364
## 2020 -4.75601993 -5.15794324 -15.71202716
## Jun Jul Aug Sep Oct
## 2013 -0.91370875 2.67940211 -0.42841493 -1.09842379 -4.27242329
## 2014 1.92473722 -3.69088007 -3.81271745 -5.37920515 -11.61888564
## 2015 0.78662059 -7.65977611 -6.74668310 -2.12890430 -1.42959370
## 2016 3.08078471 -1.83561950 -0.23062285 -0.91424262 3.84169565
## 2017 -2.69969198 2.72666362 1.67712769 2.60752811 0.72876576
## 2018 1.73757612 1.78475467 -2.15595370 4.09781659 2.78982197
## 2019 -3.43136360 0.97042445 -8.29083642 5.47025681 -4.05059107
## 2020
## Nov Dec
## 2013 -5.38739453 0.92376097
## 2014 -9.02955760 -13.29842500
## 2015 -2.35193927 -6.31273336
## 2016 -2.30673298 3.96433886
## 2017 4.45391253 0.71070753
## 2018 -11.32747391 -7.95716144
## 2019 -0.31493237 3.85931908
## 2020

#Graficamos la primera diferenciación


plot(seriedif)
#Comprobar si se volvio estacionario
adf.test(seriedif,alternative= "stationary")

##
## Augmented Dickey-Fuller Test
##
## data: seriedif
## Dickey-Fuller = -3.3327, Lag order = 4, p-value = 0.07162
## alternative hypothesis: stationary

#De acuerdo a la prueba de Dicky-Fuller aún no es estacionario por lo tanto


se realiza una segunda diferenciación
seriedif2=diff(Arimar.ts, differences=2)
seriedif2

## Jan Feb Mar Apr May


## 2013 -7.95219140 -0.43348124 3.16699393
## 2014 -2.02261249 3.61028415 -1.93844911 1.92042286 -1.29572759
## 2015 3.56832179 15.26489148 -5.50351707 3.27694813 -0.59466530
## 2016 3.21902016 3.72066085 3.97028560 -1.58797814 2.38128745
## 2017 -1.79022004 -2.49922933 -1.91743248 3.53129528 -0.65918988
## 2018 2.68753160 -4.69547212 2.36505543 -0.13267446 3.80440841
## 2019 10.14515255 1.13515861 -1.23846698 0.52785488 -4.35283126
## 2020 -8.61533902 -0.40192331 -10.55408391
## Jun Jul Aug Sep Oct
## 2013 -1.06518548 3.59311086 -3.10781704 -0.67000886 -3.17399950
## 2014 0.72705843 -5.61561728 -0.12183738 -1.56648770 -6.23968049
## 2015 -1.92693342 -8.44639670 0.91309301 4.61777880 0.69931060
## 2016 -2.30975786 -4.91640421 1.60499665 -0.68361976 4.75593827
## 2017 -3.32925439 5.42635559 -1.04953593 0.93040041 -1.87876235
## 2018 -3.00198029 0.04717855 -3.94070837 6.25377029 -1.30799462
## 2019 -1.69106996 4.40178805 -9.26126087 13.76109323 -9.52084788
## 2020
## Nov Dec
## 2013 -1.11497124 6.31115551
## 2014 2.58932805 -4.26886741
## 2015 -0.92234557 -3.96079409
## 2016 -6.14842864 6.27107185
## 2017 3.72514678 -3.74320500
## 2018 -14.11729588 3.37031247
## 2019 3.73565870 4.17425146
## 2020

#Graficamos la segunda diferenciación


plot(seriedif2)

#Comprobar si se volvió estacionario


adf.test(seriedif2,alternative = "stationary")

## Warning in adf.test(seriedif2, alternative = "stationary"): p-value


smaller than
## printed p-value
##
## Augmented Dickey-Fuller Test
##
## data: seriedif2
## Dickey-Fuller = -5.1673, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary

#Ahora determinaremos el número de media móviles para el modelo arima.


acf(seriedif2)

#Ahora determinaremos el número de autoregresiones para el modelo arima.


pacf(seriedif2)
#Ahora programamos la coincidencia de rezago con la frecuencia :1
acf(ts(seriedif2,frequency = 1))
pacf(ts(seriedif2,frequency=1))

#Programamos el modelo Arima


modelo1=arima(Arimar.ts, order=c(1,2,1))
modelo1

##
## 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")

You might also like