# UNIVERSIDAD NACIONAL DEL ALTIPLANO
# INGENIERIA ESTADISTICA E INFORMATICA
# CURSO: SERIES DE TIEMPO
# TRABAJO ENCARGADO 01

library(readxl)
## Warning: package 'readxl' was built under R version 4.0.2
SerieA <- read_excel("E:/SERIES DE TIEMPO/TRABAJO 01/SerieA.xlsx")
attach(SerieA)
## The following object is masked _by_ .GlobalEnv:
## 
##     SerieA
names(SerieA)
## [1] "SerieA"
#file.choose()
#View(SerieA)
library(astsa)
## Warning: package 'astsa' was built under R version 4.0.5
library(tseries)
## Warning: package 'tseries' was built under R version 4.0.5
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(lubridate)
## Warning: package 'lubridate' was built under R version 4.0.5
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.5
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.3     v purrr   0.3.4
## v tibble  3.1.5     v dplyr   1.0.7
## v tidyr   1.1.4     v stringr 1.4.0
## v readr   2.0.2     v forcats 0.5.1
## Warning: package 'ggplot2' was built under R version 4.0.3
## Warning: package 'tibble' was built under R version 4.0.5
## Warning: package 'tidyr' was built under R version 4.0.5
## Warning: package 'readr' was built under R version 4.0.5
## Warning: package 'dplyr' was built under R version 4.0.5
## Warning: package 'forcats' was built under R version 4.0.5
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x lubridate::as.difftime() masks base::as.difftime()
## x lubridate::date()        masks base::date()
## x dplyr::filter()          masks stats::filter()
## x lubridate::intersect()   masks base::intersect()
## x dplyr::lag()             masks stats::lag()
## x lubridate::setdiff()     masks base::setdiff()
## x lubridate::union()       masks base::union()
library(forecast)
## Warning: package 'forecast' was built under R version 4.0.5
## 
## Attaching package: 'forecast'
## The following object is masked from 'package:astsa':
## 
##     gas
#Series de Tiempo Univariadas 

#Paso 1. Convertir a objeto de Serie de Tiempo en R

SerieA.ts=ts(SerieA, start=c(1984,1), frequency = 12)

print(SerieA.ts)
##         Jan    Feb    Mar    Apr    May    Jun    Jul    Aug    Sep    Oct
## 1984  9.500 10.138 10.720 10.967 10.911 10.801 10.670 10.540 10.398 10.312
## 1985 10.533 10.770 11.097 11.267 11.332 11.273 11.152 11.010 10.919 10.845
## 1986 11.333 11.664 12.245 12.543 12.521 12.336 12.048 11.879 11.749 11.618
## 1987 11.644 11.902 11.875 11.772 11.633 11.457 11.352 11.173 11.056 10.937
## 1988 10.844 11.044 11.159 11.553 11.564 11.427 11.282 11.134 10.987 10.880
## 1989 10.721 10.829 10.928 11.022 10.982 10.869 10.749 10.619 10.509 10.396
## 1990 10.275 10.327 10.288 10.226 10.128 10.063  9.956  9.840  9.735  9.690
## 1991  9.786  9.829  9.931 10.079 10.039  9.955  9.857  9.718  9.615  9.515
## 1992  9.452  9.590  9.629  9.535  9.413  9.267  9.152  9.040  8.985  8.894
## 1993  8.868  9.122  9.217  9.286  9.257  9.131  9.025  8.905  8.822  8.776
## 1994  8.894  9.213  9.423  9.523  9.522  9.415  9.299  9.173  9.104  9.009
## 1995  8.971  9.004  9.193  9.250  9.139  9.015  8.896  8.778  8.687  8.585
## 1996  8.500  8.776  8.844  8.874  8.783  8.659  8.520  8.416  8.306  8.196
## 1997  8.287  8.716  9.183  9.362  9.331  9.209  9.098  9.005  8.915  8.875
## 1998  8.807  8.878  8.979  9.039  8.915  8.768  8.663  8.543  8.420  8.305
## 1999  8.177  8.287  8.595  8.920  8.950  8.828  8.703  8.586  8.497  8.477
## 2000  8.337  8.634  8.961  9.005  8.906  8.801  8.674  8.576  8.478  8.425
## 2001  8.620  9.257  9.907 10.180 10.141 10.047  9.947  9.854  9.787  9.716
## 2002  9.578  9.777 10.138 10.323 10.373 10.278 10.153 10.074 10.000  9.947
## 2003 10.052 10.386 10.714 10.895 10.854 10.739 10.611 10.492 10.375 10.267
## 2004 10.325 10.760 10.883 10.901 10.801 10.640 10.535 10.420 10.337 10.222
## 2005 10.043 10.184 10.354 10.366 10.244 10.099  9.961  9.827  9.715  9.656
## 2006  9.750 10.131 10.208 10.347 10.260 10.132 10.010  9.873  9.766  9.668
## 2007  9.702  9.745  9.931 10.112 10.081  9.935  9.826  9.704  9.655  9.563
## 2008  9.527  9.766  9.930  9.901  9.782  9.650  9.526  9.393  9.277  9.168
##         Nov    Dec
## 1984 10.282 10.334
## 1985 10.821 10.993
## 1986 11.472 11.421
## 1987 10.863 10.813
## 1988 10.749 10.624
## 1989 10.276 10.183
## 1990  9.718  9.747
## 1991  9.436  9.372
## 1992  8.818  8.806
## 1993  8.759  8.795
## 1994  8.930  8.882
## 1995  8.484  8.435
## 1996  8.135  8.117
## 1997  8.837  8.773
## 1998  8.237  8.191
## 1999  8.396  8.301
## 2000  8.356  8.293
## 2001  9.645  9.563
## 2002  9.910  9.947
## 2003 10.174 10.068
## 2004 10.100 10.014
## 2005  9.585  9.552
## 2006  9.620  9.597
## 2007  9.455  9.452
## 2008  9.071  9.023
class(SerieA.ts)
## [1] "ts"
start(SerieA.ts)
## [1] 1984    1
end(SerieA.ts)
## [1] 2008   12
plot(SerieA.ts,  main="Serie de tiempo", ylab="Precio", col="red")

serielog=log(SerieA.ts)
serielog
##           Jan      Feb      Mar      Apr      May      Jun      Jul      Aug
## 1984 2.251292 2.316291 2.372111 2.394891 2.389771 2.379639 2.367436 2.355178
## 1985 2.354513 2.376764 2.406675 2.421878 2.427631 2.422410 2.411619 2.398804
## 1986 2.427719 2.456507 2.505118 2.529163 2.527407 2.512522 2.488899 2.474772
## 1987 2.454791 2.476706 2.474435 2.465724 2.453846 2.438601 2.429394 2.413500
## 1988 2.383612 2.401887 2.412246 2.446945 2.447897 2.435979 2.423209 2.410003
## 1989 2.372204 2.382228 2.391328 2.399893 2.396258 2.385915 2.374813 2.362645
## 1990 2.329714 2.334762 2.330978 2.324933 2.315304 2.308865 2.298175 2.286456
## 1991 2.280953 2.285337 2.295661 2.310454 2.306478 2.298075 2.288182 2.273980
## 1992 2.246226 2.260721 2.264779 2.254969 2.242092 2.226460 2.213972 2.201659
## 1993 2.182449 2.210689 2.221050 2.228508 2.225380 2.211675 2.199999 2.186613
## 1994 2.185377 2.220616 2.243154 2.253710 2.253605 2.242304 2.229907 2.216264
## 1995 2.193997 2.197669 2.218442 2.224624 2.212551 2.198890 2.185602 2.172249
## 1996 2.140066 2.172021 2.179739 2.183126 2.172818 2.158599 2.142416 2.130135
## 1997 2.114688 2.165160 2.217354 2.236659 2.233342 2.220181 2.208055 2.197780
## 1998 2.175547 2.183576 2.194889 2.201549 2.187735 2.171109 2.159061 2.145112
## 1999 2.101325 2.114688 2.151181 2.188296 2.191654 2.177928 2.163668 2.150133
## 2000 2.120703 2.155708 2.192882 2.197780 2.186725 2.174865 2.160330 2.148968
## 2001 2.154085 2.225380 2.293242 2.320425 2.316587 2.307274 2.297271 2.287877
## 2002 2.259469 2.280033 2.316291 2.334374 2.339206 2.330006 2.317769 2.309958
## 2003 2.307772 2.340459 2.371551 2.388304 2.384534 2.373882 2.361891 2.350613
## 2004 2.334568 2.375836 2.387202 2.388855 2.379639 2.364620 2.354703 2.343727
## 2005 2.306876 2.320818 2.337373 2.338531 2.326692 2.312436 2.298677 2.285134
## 2006 2.277267 2.315600 2.323172 2.336697 2.328253 2.315699 2.303585 2.289804
## 2007 2.272332 2.276754 2.295661 2.313723 2.310652 2.296064 2.285032 2.272538
## 2008 2.254130 2.278907 2.295560 2.292636 2.280544 2.266958 2.254025 2.239965
##           Sep      Oct      Nov      Dec
## 1984 2.341613 2.333308 2.330395 2.335439
## 1985 2.390504 2.383704 2.381489 2.397259
## 1986 2.463768 2.452556 2.439909 2.435454
## 1987 2.402973 2.392152 2.385363 2.380749
## 1988 2.396713 2.386926 2.374813 2.363116
## 1989 2.352232 2.341421 2.329811 2.320720
## 1990 2.275728 2.271094 2.273980 2.276960
## 1991 2.263324 2.252870 2.244532 2.237727
## 1992 2.195557 2.185377 2.176795 2.175433
## 1993 2.177249 2.172021 2.170082 2.174183
## 1994 2.208714 2.198224 2.189416 2.184027
## 1995 2.161828 2.150016 2.138182 2.132390
## 1996 2.116978 2.103646 2.096176 2.093961
## 1997 2.187735 2.183238 2.178947 2.171679
## 1998 2.130610 2.116858 2.108636 2.103036
## 1999 2.139713 2.137357 2.127755 2.116376
## 2000 2.137475 2.131203 2.122980 2.115412
## 2001 2.281055 2.273774 2.266440 2.257901
## 2002 2.302585 2.297271 2.293544 2.297271
## 2003 2.339399 2.328935 2.319835 2.309362
## 2004 2.335730 2.324542 2.312535 2.303984
## 2005 2.273671 2.267579 2.260199 2.256751
## 2006 2.278907 2.268821 2.263844 2.261451
## 2007 2.267476 2.257901 2.246544 2.246226
## 2008 2.227538 2.215719 2.205083 2.199777
plot(serielog)

#Stationarity: To know the number of differences that are required to achieve that the series
#be stationary

ndiffs(SerieA.ts)
## [1] 1
#Paso 2.Prueba de DickeyFuller

adf.test(SerieA.ts)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  SerieA.ts
## Dickey-Fuller = -1.2334, Lag order = 6, p-value = 0.8994
## alternative hypothesis: stationary
seriedif=diff(SerieA.ts)
plot(seriedif)

acf(seriedif)

ndiffs(seriedif)
## [1] 0
adf.test(seriedif)
## Warning in adf.test(seriedif): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  seriedif
## Dickey-Fuller = -10.613, Lag order = 6, p-value = 0.01
## alternative hypothesis: stationary
#Prueba de Dickey Fuller con dos diferencias


seriedif2=diff(SerieA.ts, differences =2)
plot(seriedif2)

adf.test(seriedif2)
## Warning in adf.test(seriedif2): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  seriedif2
## Dickey-Fuller = -11.117, Lag order = 6, p-value = 0.01
## alternative hypothesis: stationary
#Paso 4: Analisis visual de as graficas


plot(seriedif2, type="o", lty="dashed",main="Serie de Tiempo",col="red")

par(mfrow=c(2,1), mar=c(2,2,2,1)+.1)
acf(seriedif2)
pacf(seriedif2)

acf(ts(seriedif2, frequency=1))
pacf(ts(seriedif2, frequency=1))

#Modelo Arima

modelo1=arima(SerieA.ts,order=c(1,2,1))
summary(modelo1)
## 
## Call:
## arima(x = SerieA.ts, order = c(1, 2, 1))
## 
## Coefficients:
##          ar1     ma1
##       0.0191  0.2969
## s.e.  0.1602  0.1504
## 
## sigma^2 estimated as 0.01243:  log likelihood = 230.89,  aic = -455.78
## 
## Training set error measures:
##                        ME     RMSE        MAE           MPE      MAPE      MASE
## Training set -0.001708912 0.111109 0.07294355 -0.0009988697 0.7348677 0.5865616
##                     ACF1
## Training set 0.001412786
tsdiag(modelo1)

Box.test(residuals(modelo1),type="Ljung-Box")
## 
##  Box-Ljung test
## 
## data:  residuals(modelo1)
## X-squared = 0.0006048, df = 1, p-value = 0.9804
error=residuals(modelo1)
plot(error)

#Pronosticos Arima
pronostico=forecast::forecast(modelo1,h=10)
pronostico
##          Point Forecast    Lo 80     Hi 80    Lo 95     Hi 95
## Jan 2009       8.989383 8.846517  9.132249 8.770888  9.207878
## Feb 2009       8.956041 8.595642  9.316440 8.404858  9.507224
## Mar 2009       8.922704 8.290233  9.555174 7.955424  9.889984
## Apr 2009       8.889367 7.939540  9.839193 7.436733 10.342001
## May 2009       8.856030 7.549241 10.162819 6.857468 10.854592
## Jun 2009       8.822693 7.123294 10.522092 6.223686 11.421700
## Jul 2009       8.789356 6.664667 10.914045 5.539925 12.038787
## Aug 2009       8.756019 6.175691 11.336347 4.809748 12.702290
## Sep 2009       8.722682 5.658261 11.787103 4.036054 13.409310
## Oct 2009       8.689345 5.113955 12.264736 3.221257 14.157433
plot(pronostico)