# UNIVERSIDAD NACIONAL DEL ALTIPLANO
# INGENIERIA ESTADISTICA E INFORMATICA
# CURSO: SERIES DE TIEMPO
# SERIES ESTACIONARIAS Y NO ESTACIONARIAS
library(readxl)
## Warning: package 'readxl' was built under R version 4.0.2
library(knitr)
preciopm <- read_excel("E:\\SERIES DE TIEMPO\\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"
# View(preciopm)
library(xts)
## Warning: package 'xts' was built under R version 4.0.5
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
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 dplyr::filter() masks stats::filter()
## x dplyr::first() masks xts::first()
## x dplyr::lag() masks stats::lag()
## x dplyr::last() masks xts::last()
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(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(astsa)
## Warning: package 'astsa' was built under R version 4.0.5
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
library(foreign)
## Warning: package 'foreign' was built under R version 4.0.3
library(timsac)
## Warning: package 'timsac' was built under R version 4.0.3
library(vars)
## Warning: package 'vars' was built under R version 4.0.5
## Loading required package: MASS
##
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
##
## select
## Loading required package: strucchange
## Warning: package 'strucchange' was built under R version 4.0.5
## Loading required package: sandwich
##
## Attaching package: 'strucchange'
## The following object is masked from 'package:stringr':
##
## boundary
## Loading required package: urca
## Warning: package 'urca' was built under R version 4.0.5
## Loading required package: lmtest
## Warning: package 'lmtest' was built under R version 4.0.2
library(mFilter)
## Warning: package 'mFilter' was built under R version 4.0.5
library(dynlm)
## Warning: package 'dynlm' was built under R version 4.0.5
library(nlme)
## Warning: package 'nlme' was built under R version 4.0.5
##
## Attaching package: 'nlme'
## The following object is masked from 'package:forecast':
##
## getResponse
## The following object is masked from 'package:dplyr':
##
## collapse
# Especificar Periods of Time
preciopma.ts <- ts(Precio, start = c(1990,1), frequency = 12)
start(preciopma.ts); end(preciopma.ts)
## [1] 1990 1
## [1] 2019 4
preciopma.ts
## Jan Feb Mar Apr May Jun
## 1990 14.897350 13.856284 12.298178 10.927095 10.452086 9.817348
## 1991 13.795437 10.845017 11.366329 12.068577 11.991099 11.930451
## 1992 10.079426 10.222971 10.928616 12.263161 13.882490 14.717433
## 1993 11.803837 12.505585 12.812625 12.873810 12.220930 11.106039
## 1994 10.227919 9.954569 10.307307 11.943946 12.818879 14.009499
## 1995 14.282580 14.800758 15.121214 16.031652 16.052267 14.611588
## 1996 14.640357 15.096147 16.682205 17.461726 16.069193 15.561163
## 1997 18.060119 15.709637 14.482912 14.259241 14.834376 13.795084
## 1998 9.713509 8.882054 7.631504 9.058311 9.082049 8.297230
## 1999 7.392438 7.126746 9.856138 12.350982 12.237096 13.074408
## 2000 21.348773 23.547154 22.935065 20.260149 23.193243 24.744170
## 2001 18.309991 18.799571 16.851520 16.360149 18.199624 18.789678
## 2002 14.032789 15.278664 19.405427 21.897267 21.987875 21.507413
## 2003 27.074817 27.012354 23.663690 20.458351 21.904876 23.570076
## 2004 25.316733 25.162286 26.962854 28.014928 30.823501 29.251947
## 2005 30.012074 32.092964 36.346343 38.118350 38.912862 43.204432
## 2006 47.779745 46.563260 49.381610 54.956204 55.260986 53.833091
## 2007 42.745920 47.033095 48.943810 52.898664 54.958534 58.682133
## 2008 78.879230 79.714309 87.177370 93.028973 102.884604 112.291047
## 2009 37.230832 37.848297 41.678599 47.490271 56.016166 63.641365
## 2010 70.866452 69.134714 70.093660 72.292077 66.101763 65.540751
## 2011 84.324640 89.161930 99.432700 106.104005 101.835090 100.828631
## 2012 107.455818 108.451418 109.604263 104.893764 100.233358 90.181843
## 2013 97.982782 103.352938 100.770902 97.755385 97.906861 96.993153
## 2014 88.310808 90.822240 91.395224 93.888630 95.086309 97.011046
## 2015 40.451272 45.986060 46.017331 49.325551 52.039105 52.825725
## 2016 23.102382 23.729330 28.326563 31.335818 36.726361 39.807146
## 2017 44.500081 44.174971 41.932428 43.221180 43.850742 41.151050
## 2018 57.453995 56.156762 57.224584 58.159732 62.899289 64.636865
## 2019 54.056660 57.379810 59.464492 62.077030
## Jul Aug Sep Oct Nov Dec
## 1990 13.188972 20.825314 26.238379 25.461774 23.590050 19.221472
## 1991 12.852894 12.724638 13.357160 14.094443 11.643019 10.081917
## 1992 14.694666 14.592165 15.002168 15.047736 13.499847 11.999895
## 1993 11.024373 11.150484 11.278000 11.218994 9.715306 9.101101
## 1994 14.525943 12.763118 12.527938 13.827166 14.100563 13.831668
## 1995 13.256370 13.449261 13.363175 12.991089 13.580205 15.116482
## 1996 15.978825 17.149939 19.308046 20.562573 18.913614 19.316873
## 1997 14.158890 14.821528 14.748565 15.700761 14.316305 11.438379
## 1998 8.804921 8.239905 9.465377 9.128815 7.865962 6.349166
## 1999 15.319650 17.430406 19.077463 18.434955 19.961135 20.140117
## 2000 22.778127 24.355191 25.822586 25.011949 24.065706 17.812948
## 2001 18.433045 19.274814 19.325549 15.571857 13.259511 13.662242
## 2002 22.389223 23.253909 24.514419 22.826465 20.001942 23.282675
## 2003 24.909283 25.174429 22.513946 24.135865 24.170211 24.437509
## 2004 30.770350 32.403355 33.356997 37.293723 30.656835 26.902246
## 2005 44.357927 47.831527 48.880013 45.437368 41.707053 42.989153
## 2006 56.818337 57.866679 49.690903 46.744199 45.794018 47.642082
## 2007 63.212159 62.190669 66.439359 71.018809 78.145548 77.690069
## 2008 118.026634 105.209472 83.841054 58.685407 40.592316 32.916936
## 2009 60.191980 66.404843 64.108802 67.933160 71.534239 68.493029
## 2010 66.923774 68.245485 68.332474 72.942531 75.758392 80.208666
## 2011 102.599593 96.988597 97.634395 99.366005 105.806100 105.126973
## 2012 94.012683 99.512167 100.076628 97.108629 93.682388 94.939944
## 2013 99.672555 99.244140 98.145716 93.873293 88.485898 89.409659
## 2014 93.320166 89.507449 84.128243 72.509358 63.479800 50.181375
## 2015 45.165949 38.419266 36.290362 34.860768 32.508829 26.196096
## 2016 37.971526 37.740903 36.826661 40.668356 38.361623 42.325962
## 2017 43.877714 45.554842 48.162370 48.891135 53.345048 54.055756
## 2018 66.421619 64.265666 68.363482 71.153304 59.825830 51.868669
## 2019
# Grafico de todos los years 1990-2020
seasonplot(preciopma.ts,col=rainbow(12), year.labels=T)

#Function of autocorrelacion
acf(preciopma.ts) #No es estacionaria

ndiffs(preciopma.ts) # Cuantas diferencias necesitas
## [1] 1
#Usar Diferenciacion "Primera Diferencia de Precio
seriedif<-diff(preciopma.ts)
plot(seriedif)

acf(seriedif) # Estacionaria

ndiffs(seriedif)
## [1] 0
# Analisis visual de los Graficos de Autocorrelacion
par(mfrow=c(2,2), mar=c(2,2,2,1)+ .1)
plot(preciopma.ts, ylab="Precio")
acf(preciopma.ts, main="Serie No Estacionaria")
plot(seriedif)
acf(seriedif, main="Serie Estacionaria")
