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

library(readxl)
## Warning: package 'readxl' was built under R version 4.0.2
preciopm <- read_excel("E:/SERIES DE TIEMPO/UNIDAD I/TAREA 01/preciopm.xls")
preciopm
## # A tibble: 352 x 1
##    Precio
##     <dbl>
##  1  14.9 
##  2  13.9 
##  3  12.3 
##  4  10.9 
##  5  10.5 
##  6   9.82
##  7  13.2 
##  8  20.8 
##  9  26.2 
## 10  25.5 
## # ... with 342 more rows
attach(preciopm)
names(preciopm)
## [1] "Precio"
#file.choose()
#View(Arimar)
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.5     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.5
## 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

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

print(Arimar.ts)
##             Jan        Feb        Mar        Apr        May        Jun
## 1993  14.897350  13.856284  12.298178  10.927095  10.452086   9.817348
## 1994  13.795437  10.845017  11.366329  12.068577  11.991099  11.930451
## 1995  10.079426  10.222971  10.928616  12.263161  13.882490  14.717433
## 1996  11.803837  12.505585  12.812625  12.873810  12.220930  11.106039
## 1997  10.227919   9.954569  10.307307  11.943946  12.818879  14.009499
## 1998  14.282580  14.800758  15.121214  16.031652  16.052267  14.611588
## 1999  14.640357  15.096147  16.682205  17.461726  16.069193  15.561163
## 2000  18.060119  15.709637  14.482912  14.259241  14.834376  13.795084
## 2001   9.713509   8.882054   7.631504   9.058311   9.082049   8.297230
## 2002   7.392438   7.126746   9.856138  12.350982  12.237096  13.074408
## 2003  21.348773  23.547154  22.935065  20.260149  23.193243  24.744170
## 2004  18.309991  18.799571  16.851520  16.360149  18.199624  18.789678
## 2005  14.032789  15.278664  19.405427  21.897267  21.987875  21.507413
## 2006  27.074817  27.012354  23.663690  20.458351  21.904876  23.570076
## 2007  25.316733  25.162286  26.962854  28.014928  30.823501  29.251947
## 2008  30.012074  32.092964  36.346343  38.118350  38.912862  43.204432
## 2009  47.779745  46.563260  49.381610  54.956204  55.260986  53.833091
## 2010  42.745920  47.033095  48.943810  52.898664  54.958534  58.682133
## 2011  78.879230  79.714309  87.177370  93.028973 102.884604 112.291047
## 2012  37.230832  37.848297  41.678599  47.490271  56.016166  63.641365
## 2013  70.866452  69.134714  70.093660  72.292077  66.101763  65.540751
## 2014  84.324640  89.161930  99.432700 106.104005 101.835090 100.828631
## 2015 107.455818 108.451418 109.604263 104.893764 100.233358  90.181843
## 2016  97.982782 103.352938 100.770902  97.755385  97.906861  96.993153
## 2017  88.310808  90.822240  91.395224  93.888630  95.086309  97.011046
## 2018  40.451272  45.986060  46.017331  49.325551  52.039105  52.825725
## 2019  23.102382  23.729330  28.326563  31.335818  36.726361  39.807146
## 2020  44.500081  44.174971  41.932428  43.221180  43.850742  41.151050
## 2021  57.453995  56.156762  57.224584  58.159732  62.899289  64.636865
## 2022  54.056660  57.379810  59.464492  62.077030                      
##             Jul        Aug        Sep        Oct        Nov        Dec
## 1993  13.188972  20.825314  26.238379  25.461774  23.590050  19.221472
## 1994  12.852894  12.724638  13.357160  14.094443  11.643019  10.081917
## 1995  14.694666  14.592165  15.002168  15.047736  13.499847  11.999895
## 1996  11.024373  11.150484  11.278000  11.218994   9.715306   9.101101
## 1997  14.525943  12.763118  12.527938  13.827166  14.100563  13.831668
## 1998  13.256370  13.449261  13.363175  12.991089  13.580205  15.116482
## 1999  15.978825  17.149939  19.308046  20.562573  18.913614  19.316873
## 2000  14.158890  14.821528  14.748565  15.700761  14.316305  11.438379
## 2001   8.804921   8.239905   9.465377   9.128815   7.865962   6.349166
## 2002  15.319650  17.430406  19.077463  18.434955  19.961135  20.140117
## 2003  22.778127  24.355191  25.822586  25.011949  24.065706  17.812948
## 2004  18.433045  19.274814  19.325549  15.571857  13.259511  13.662242
## 2005  22.389223  23.253909  24.514419  22.826465  20.001942  23.282675
## 2006  24.909283  25.174429  22.513946  24.135865  24.170211  24.437509
## 2007  30.770350  32.403355  33.356997  37.293723  30.656835  26.902246
## 2008  44.357927  47.831527  48.880013  45.437368  41.707053  42.989153
## 2009  56.818337  57.866679  49.690903  46.744199  45.794018  47.642082
## 2010  63.212159  62.190669  66.439359  71.018809  78.145548  77.690069
## 2011 118.026634 105.209472  83.841054  58.685407  40.592316  32.916936
## 2012  60.191980  66.404843  64.108802  67.933160  71.534239  68.493029
## 2013  66.923774  68.245485  68.332474  72.942531  75.758392  80.208666
## 2014 102.599593  96.988597  97.634395  99.366005 105.806100 105.126973
## 2015  94.012683  99.512167 100.076628  97.108629  93.682388  94.939944
## 2016  99.672555  99.244140  98.145716  93.873293  88.485898  89.409659
## 2017  93.320166  89.507449  84.128243  72.509358  63.479800  50.181375
## 2018  45.165949  38.419266  36.290362  34.860768  32.508829  26.196096
## 2019  37.971526  37.740903  36.826661  40.668356  38.361623  42.325962
## 2020  43.877714  45.554842  48.162370  48.891135  53.345048  54.055756
## 2021  66.421619  64.265666  68.363482  71.153304  59.825830  51.868669
## 2022
class(Arimar.ts)
## [1] "ts"
start(Arimar.ts)
## [1] 1993    1
end(Arimar.ts)
## [1] 2022    4
plot(Arimar.ts,  main="Serie de tiempo", ylab="Precio", col="red")

serielog=log(Arimar.ts)
serielog
##           Jan      Feb      Mar      Apr      May      Jun      Jul      Aug
## 1993 2.701183 2.628739 2.509451 2.391245 2.346802 2.284151 2.579381 3.036169
## 1994 2.624338 2.383706 2.430655 2.490605 2.484165 2.479094 2.553569 2.543540
## 1995 2.310496 2.324637 2.391385 2.506600 2.630628 2.689033 2.687485 2.680485
## 1996 2.468425 2.526175 2.550431 2.555195 2.503150 2.407489 2.400109 2.411483
## 1997 2.325121 2.298032 2.332853 2.480225 2.550919 2.639736 2.675936 2.546560
## 1998 2.659041 2.694678 2.716099 2.774565 2.775850 2.681815 2.584478 2.598924
## 1999 2.683782 2.714440 2.814343 2.860011 2.776904 2.744778 2.771264 2.841995
## 2000 2.893706 2.754274 2.672969 2.657405 2.696947 2.624312 2.650343 2.696081
## 2001 2.273518 2.184033 2.032285 2.203683 2.206300 2.115922 2.175311 2.108989
## 2002 2.000458 1.963855 2.288094 2.513736 2.504472 2.570657 2.729136 2.858216
## 2003 3.060994 3.159005 3.132667 3.008656 3.143861 3.208590 3.125801 3.192745
## 2004 2.907447 2.933834 2.824441 2.794848 2.901401 2.933308 2.914145 2.958799
## 2005 2.641397 2.726457 2.965553 3.086362 3.090491 3.068398 3.108580 3.146473
## 2006 3.298604 3.296294 3.163942 3.018391 3.086709 3.159978 3.215241 3.225829
## 2007 3.231466 3.225346 3.294460 3.332738 3.428277 3.375946 3.426552 3.478262
## 2008 3.401600 3.468637 3.593094 3.640696 3.661325 3.765943 3.792291 3.867685
## 2009 3.866602 3.840812 3.899578 4.006537 4.012067 3.985888 4.039859 4.058142
## 2010 3.755274 3.850852 3.890673 3.968378 4.006579 4.072135 4.146497 4.130205
## 2011 4.367918 4.378449 4.467945 4.532911 4.633608 4.721094 4.770910 4.655953
## 2012 3.617137 3.633586 3.729988 3.860525 4.025640 4.153264 4.097539 4.195770
## 2013 4.260797 4.236057 4.249832 4.280715 4.191195 4.182672 4.203554 4.223111
## 2014 4.434674 4.490454 4.599481 4.664420 4.623355 4.613422 4.630834 4.574593
## 2015 4.677080 4.686302 4.696876 4.652948 4.607501 4.501828 4.543430 4.600280
## 2016 4.584792 4.638150 4.612850 4.582468 4.584017 4.574640 4.601890 4.597583
## 2017 4.480862 4.508904 4.515193 4.542109 4.554785 4.574825 4.536036 4.494322
## 2018 3.700098 3.828338 3.829018 3.898442 3.951995 3.966998 3.810343 3.648559
## 2019 3.139936 3.166712 3.343800 3.444762 3.603495 3.684046 3.636837 3.630744
## 2020 3.795491 3.788158 3.736059 3.766331 3.780792 3.717249 3.781407 3.818917
## 2021 4.050985 4.028147 4.046984 4.063193 4.141535 4.168785 4.196023 4.163026
## 2022 3.990033 4.049692 4.085379 4.128376                                    
##           Sep      Oct      Nov      Dec
## 1993 3.267223 3.237178 3.160825 2.956028
## 1994 2.592053 2.645781 2.454707 2.310743
## 1995 2.708195 2.711228 2.602678 2.484898
## 1996 2.422854 2.417608 2.273703 2.208395
## 1997 2.527961 2.626635 2.646215 2.626961
## 1998 2.592503 2.564264 2.608613 2.715786
## 1999 2.960522 3.023473 2.939882 2.960979
## 2000 2.691146 2.753709 2.661399 2.436974
## 2001 2.247641 2.211436 2.062545 1.848323
## 2002 2.948508 2.914249 2.993787 3.002714
## 2003 3.251250 3.219354 3.180788 2.879926
## 2004 2.961428 2.745465 2.584715 2.614636
## 2005 3.199261 3.127921 2.995829 3.147710
## 2006 3.114135 3.183699 3.185121 3.196119
## 2007 3.507268 3.618825 3.422856 3.292210
## 2008 3.889369 3.816335 3.730670 3.760948
## 2009 3.905822 3.844690 3.824153 3.863716
## 2010 4.196290 4.262945 4.358573 4.352727
## 2011 4.428923 4.072191 3.703579 3.493987
## 2012 4.160582 4.218524 4.270176 4.226732
## 2013 4.224385 4.289672 4.327549 4.384632
## 2014 4.581230 4.598810 4.661608 4.655169
## 2015 4.605936 4.575830 4.539910 4.553245
## 2016 4.586453 4.541946 4.482843 4.493229
## 2017 4.432342 4.283716 4.150722 3.915644
## 2018 3.591552 3.551362 3.481512 3.265610
## 2019 3.606222 3.705450 3.647058 3.745401
## 2020 3.874578 3.889596 3.976781 3.990016
## 2021 4.224839 4.264837 4.091438 3.948715
## 2022
plot(serielog)

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

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

adf.test(Arimar.ts)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  Arimar.ts
## Dickey-Fuller = -2.2091, Lag order = 7, p-value = 0.4886
## alternative hypothesis: stationary
seriedif=diff(Arimar.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 = -7.452, Lag order = 7, p-value = 0.01
## alternative hypothesis: stationary
#Prueba de Dickey Fuller con dos diferencias


seriedif2=diff(Arimar.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.192, Lag order = 7, 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(Arimar.ts,order=c(1,2,1))
summary(modelo1)
## 
## Call:
## arima(x = Arimar.ts, order = c(1, 2, 1))
## 
## Coefficients:
##          ar1      ma1
##       0.5335  -1.0000
## s.e.  0.0454   0.0077
## 
## sigma^2 estimated as 10.76:  log likelihood = -914.68,  aic = 1835.36
## 
## Training set error measures:
##                      ME     RMSE      MAE       MPE     MAPE      MASE
## Training set 0.03286225 3.270376 2.232341 0.2781591 6.525319 0.8900024
##                   ACF1
## Training set 0.0383441
tsdiag(modelo1)

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

#Pronosticos Arima
pronostico=forecast::forecast(modelo1,h=10)
pronostico
##          Point Forecast    Lo 80    Hi 80    Lo 95     Hi 95
## May 2022       63.53557 59.32651 67.74463 57.09837  69.97277
## Jun 2022       64.37841 56.66265 72.09417 52.57817  76.17865
## Jul 2022       64.89276 54.00425 75.78128 48.24021  81.54531
## Aug 2022       65.23185 51.51413 78.94958 44.25240  86.21131
## Sep 2022       65.47744 49.22923 81.72564 40.62795  90.32692
## Oct 2022       65.67313 47.14035 84.20591 37.32969  94.01658
## Nov 2022       65.84221 45.22406 86.46035 34.30948  97.37494
## Dec 2022       65.99708 43.45509 88.53908 31.52208 100.47209
## Jan 2023       66.14438 41.81057 90.47820 28.92903 103.35974
## Feb 2023       66.28764 40.27120 92.30408 26.49893 106.07635
plot(pronostico)