Cargar series

library(readxl)
obs1 <- read_excel("C:/Users/wsand/Dropbox/Centro Oriente/Julio/obs1.xlsx")
library(DT)
DT::datatable(obs1)

Serie mnatural

library(zoo)
library(xts)
mnatural <- xts::xts(x = obs1$mnatural, order.by = obs1$fecha)
summary(mnatural)
##      Index                        mnatural     
##  Min.   :2015-01-01 00:00:00   Min.   : 53.00  
##  1st Qu.:2016-04-01 06:00:00   1st Qu.: 73.00  
##  Median :2017-07-01 12:00:00   Median : 79.00  
##  Mean   :2017-07-01 12:00:00   Mean   : 79.71  
##  3rd Qu.:2018-09-30 18:00:00   3rd Qu.: 86.00  
##  Max.   :2019-12-31 00:00:00   Max.   :120.00
library(TSstudio)
ts_plot(mnatural, slider = TRUE)
library(ggplot2)
library(forecast)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
ggtsdisplay(mnatural)

ggtsdisplay(diff(mnatural))
## Warning: Removed 1 rows containing missing values (`geom_point()`).

library(tseries)
adf.test(mnatural)
## Warning in adf.test(mnatural): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  mnatural
## Dickey-Fuller = -6.7566, Lag order = 12, p-value = 0.01
## alternative hypothesis: stationary
pp.test(mnatural)
## Warning in pp.test(mnatural): p-value smaller than printed p-value
## 
##  Phillips-Perron Unit Root Test
## 
## data:  mnatural
## Dickey-Fuller Z(alpha) = -2208.5, Truncation lag parameter = 8, p-value
## = 0.01
## alternative hypothesis: stationary
mnatural=xts_to_ts(mnatural, start = as.Date("2015-01-01"),   frequency = 365)
ts_decompose(mnatural, type = "both")
ts_cor(mnatural)
ts_heatmap(mnatural)
ts_seasonal(mnatural,type = "all")
## Warning: No trace type specified and no positional attributes specified
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode

Test de Dickey Fuller y Phillips Perron

library(tseries)
adf.test(mnatural)
## Warning in adf.test(mnatural): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  mnatural
## Dickey-Fuller = -6.7566, Lag order = 12, p-value = 0.01
## alternative hypothesis: stationary
pp.test(mnatural)
## Warning in pp.test(mnatural): p-value smaller than printed p-value
## 
##  Phillips-Perron Unit Root Test
## 
## data:  mnatural
## Dickey-Fuller Z(alpha) = -2208.5, Truncation lag parameter = 8, p-value
## = 0.01
## alternative hypothesis: stationary

Dickey Fuller y Phillips Perron con una diferencia

adf.test(diff(mnatural))
## Warning in adf.test(diff(mnatural)): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  diff(mnatural)
## Dickey-Fuller = -20.453, Lag order = 12, p-value = 0.01
## alternative hypothesis: stationary
pp.test(diff(mnatural))
## Warning in pp.test(diff(mnatural)): p-value smaller than printed p-value
## 
##  Phillips-Perron Unit Root Test
## 
## data:  diff(mnatural)
## Dickey-Fuller Z(alpha) = -2227.4, Truncation lag parameter = 8, p-value
## = 0.01
## alternative hypothesis: stationary

Serie Mcardio

mcardio <- xts::xts(x = obs1$mcardio, order.by = obs1$fecha)
ts_plot(mcardio, slider = TRUE)
library(ggplot2)
library(forecast)
ggtsdisplay(mcardio)

ggtsdisplay(diff(mcardio))
## Warning: Removed 1 rows containing missing values (`geom_point()`).

library(tseries)
adf.test(mcardio)
## Warning in adf.test(mcardio): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  mcardio
## Dickey-Fuller = -9.8494, Lag order = 12, p-value = 0.01
## alternative hypothesis: stationary
pp.test(mcardio)
## Warning in pp.test(mcardio): p-value smaller than printed p-value
## 
##  Phillips-Perron Unit Root Test
## 
## data:  mcardio
## Dickey-Fuller Z(alpha) = -1999.7, Truncation lag parameter = 8, p-value
## = 0.01
## alternative hypothesis: stationary
mcardio=xts_to_ts(mcardio, start = as.Date("2015-01-01"),   frequency = 365)
summary(mcardio)
##     Series 1    
##  Min.   :14.00  
##  1st Qu.:24.00  
##  Median :27.00  
##  Mean   :27.53  
##  3rd Qu.:31.00  
##  Max.   :49.00
ts_decompose(mcardio, type = "both")
ts_cor(mcardio)
ts_heatmap(mcardio)
ts_seasonal(mcardio,type = "all")
## Warning: No trace type specified and no positional attributes specified
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode

Test de Dickey Fuller y Phillips Perron

library(tseries)
adf.test(mcardio)
## Warning in adf.test(mcardio): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  mcardio
## Dickey-Fuller = -9.8494, Lag order = 12, p-value = 0.01
## alternative hypothesis: stationary
pp.test(mcardio)
## Warning in pp.test(mcardio): p-value smaller than printed p-value
## 
##  Phillips-Perron Unit Root Test
## 
## data:  mcardio
## Dickey-Fuller Z(alpha) = -1999.7, Truncation lag parameter = 8, p-value
## = 0.01
## alternative hypothesis: stationary

Dickey Fuller y Phillips Perron con una diferencia

adf.test(diff(mcardio))
## Warning in adf.test(diff(mcardio)): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  diff(mcardio)
## Dickey-Fuller = -18.866, Lag order = 12, p-value = 0.01
## alternative hypothesis: stationary
pp.test(diff(mcardio))
## Warning in pp.test(diff(mcardio)): p-value smaller than printed p-value
## 
##  Phillips-Perron Unit Root Test
## 
## data:  diff(mcardio)
## Dickey-Fuller Z(alpha) = -2160.7, Truncation lag parameter = 8, p-value
## = 0.01
## alternative hypothesis: stationary

Serie PM2.5

PM2.5 <- xts::xts(x = obs1$PM2.5, order.by = obs1$fecha)
summary(PM2.5)
##      Index                         PM2.5       
##  Min.   :2015-01-01 00:00:00   Min.   : 4.867  
##  1st Qu.:2016-04-01 06:00:00   1st Qu.:12.371  
##  Median :2017-07-01 12:00:00   Median :16.740  
##  Mean   :2017-07-01 12:00:00   Mean   :18.556  
##  3rd Qu.:2018-09-30 18:00:00   3rd Qu.:23.571  
##  Max.   :2019-12-31 00:00:00   Max.   :56.508
ts_plot(PM2.5, slider = TRUE)
library(ggplot2)
library(forecast)
dias_semana <- c("lunes", "martes", "miercoles", "jueves", "viernes", "sabado", "domingo")


obs1$diasem <- factor(obs1$diasem, levels = dias_semana)
ggplot(obs1, aes(x=diasem, y=PM2.5)) + 
  geom_boxplot() +
  xlab("Día de la semana") +
  geom_jitter(width=0.3, size=1, alpha=0.5) +
  ylab("Valor") +
  ggtitle("Gráfico de cajas por día de la semana")

ggplot(obs1, aes(x=as.factor(tipodia), y=PM2.5)) + 
  geom_boxplot() +
  xlab("Día de la semana") +
  geom_jitter(width=0.3, size=1, alpha=0.5) +
  ylab("Valor") +
  ggtitle("Gráfico de cajas por día de la semana")

ggtsdisplay(PM2.5)

ggtsdisplay(diff(PM2.5))
## Warning: Removed 1 rows containing missing values (`geom_point()`).

adf.test(PM2.5)
## Warning in adf.test(PM2.5): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  PM2.5
## Dickey-Fuller = -5.9578, Lag order = 12, p-value = 0.01
## alternative hypothesis: stationary
pp.test(PM2.5)
## Warning in pp.test(PM2.5): p-value smaller than printed p-value
## 
##  Phillips-Perron Unit Root Test
## 
## data:  PM2.5
## Dickey-Fuller Z(alpha) = -439.27, Truncation lag parameter = 8, p-value
## = 0.01
## alternative hypothesis: stationary
PM2.5=xts_to_ts(PM2.5, start = as.Date("2015-01-01"),   frequency = 365)
ts_decompose(PM2.5, type = "both")
ts_cor(PM2.5)
ts_heatmap(PM2.5)
ts_seasonal(PM2.5,type = "all")
## Warning: No trace type specified and no positional attributes specified
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode

Test de Dickey Fuller y Phillips Perron

library(tseries)
adf.test(PM2.5)
## Warning in adf.test(PM2.5): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  PM2.5
## Dickey-Fuller = -5.9578, Lag order = 12, p-value = 0.01
## alternative hypothesis: stationary
pp.test(PM2.5)
## Warning in pp.test(PM2.5): p-value smaller than printed p-value
## 
##  Phillips-Perron Unit Root Test
## 
## data:  PM2.5
## Dickey-Fuller Z(alpha) = -439.27, Truncation lag parameter = 8, p-value
## = 0.01
## alternative hypothesis: stationary

Dickey Fuller y Phillips Perron con una diferencia

adf.test(diff(PM2.5))
## Warning in adf.test(diff(PM2.5)): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  diff(PM2.5)
## Dickey-Fuller = -19.16, Lag order = 12, p-value = 0.01
## alternative hypothesis: stationary
pp.test(diff(PM2.5))
## Warning in pp.test(diff(PM2.5)): p-value smaller than printed p-value
## 
##  Phillips-Perron Unit Root Test
## 
## data:  diff(PM2.5)
## Dickey-Fuller Z(alpha) = -1444.1, Truncation lag parameter = 8, p-value
## = 0.01
## alternative hypothesis: stationary

Serie OZONO

OZONO <- xts::xts(x = obs1$OZONO, order.by = obs1$fecha)
summary(OZONO)
##      Index                         OZONO       
##  Min.   :2015-01-01 00:00:00   Min.   : 5.842  
##  1st Qu.:2016-04-01 06:00:00   1st Qu.:16.570  
##  Median :2017-07-01 12:00:00   Median :21.527  
##  Mean   :2017-07-01 12:00:00   Mean   :22.678  
##  3rd Qu.:2018-09-30 18:00:00   3rd Qu.:27.773  
##  Max.   :2019-12-31 00:00:00   Max.   :60.838
ts_plot(OZONO, slider = TRUE)
library(ggplot2)
library(forecast)
dias_semana <- c("lunes", "martes", "miercoles", "jueves", "viernes", "sabado", "domingo")


obs1$diasem <- factor(obs1$diasem, levels = dias_semana)
ggplot(obs1, aes(x=diasem, y=OZONO)) + 
  geom_boxplot() +
  xlab("Día de la semana") +
  geom_jitter(width=0.3, size=1, alpha=0.5) +
  ylab("Valor") +
  ggtitle("Gráfico de cajas por día de la semana")

ggplot(obs1, aes(x=as.factor(tipodia), y=OZONO)) + 
  geom_boxplot() +
  xlab("Día de la semana") +
  geom_jitter(width=0.3, size=1, alpha=0.5) +
  ylab("Valor") +
  ggtitle("Gráfico de cajas por día de la semana")

ggtsdisplay(OZONO)

ggtsdisplay(diff(OZONO))
## Warning: Removed 1 rows containing missing values (`geom_point()`).

adf.test(OZONO)
## Warning in adf.test(OZONO): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  OZONO
## Dickey-Fuller = -5.2544, Lag order = 12, p-value = 0.01
## alternative hypothesis: stationary
pp.test(OZONO)
## Warning in pp.test(OZONO): p-value smaller than printed p-value
## 
##  Phillips-Perron Unit Root Test
## 
## data:  OZONO
## Dickey-Fuller Z(alpha) = -408.68, Truncation lag parameter = 8, p-value
## = 0.01
## alternative hypothesis: stationary
OZONO=xts_to_ts(OZONO, start = as.Date("2015-01-01"),   frequency = 365)
ts_decompose(OZONO, type = "both")
ts_cor(OZONO)
ts_heatmap(OZONO)
ts_seasonal(OZONO,type = "all")
## Warning: No trace type specified and no positional attributes specified
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode

Test de Dickey Fuller y Phillips Perron

library(tseries)
adf.test(OZONO)
## Warning in adf.test(OZONO): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  OZONO
## Dickey-Fuller = -5.2544, Lag order = 12, p-value = 0.01
## alternative hypothesis: stationary
pp.test(OZONO)
## Warning in pp.test(OZONO): p-value smaller than printed p-value
## 
##  Phillips-Perron Unit Root Test
## 
## data:  OZONO
## Dickey-Fuller Z(alpha) = -408.68, Truncation lag parameter = 8, p-value
## = 0.01
## alternative hypothesis: stationary

Dickey Fuller y Phillips Perron con una diferencia

adf.test(diff(OZONO))
## Warning in adf.test(diff(OZONO)): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  diff(OZONO)
## Dickey-Fuller = -17.836, Lag order = 12, p-value = 0.01
## alternative hypothesis: stationary
pp.test(diff(OZONO))
## Warning in pp.test(diff(OZONO)): p-value smaller than printed p-value
## 
##  Phillips-Perron Unit Root Test
## 
## data:  diff(OZONO)
## Dickey-Fuller Z(alpha) = -1476.3, Truncation lag parameter = 8, p-value
## = 0.01
## alternative hypothesis: stationary

Serie Temperatura

temperatura <- xts::xts(x = obs1$temperatura, order.by = obs1$fecha)
summary(temperatura)
##      Index                      temperatura   
##  Min.   :2015-01-01 00:00:00   Min.   :12.21  
##  1st Qu.:2016-04-01 06:00:00   1st Qu.:14.26  
##  Median :2017-07-01 12:00:00   Median :14.74  
##  Mean   :2017-07-01 12:00:00   Mean   :14.75  
##  3rd Qu.:2018-09-30 18:00:00   3rd Qu.:15.24  
##  Max.   :2019-12-31 00:00:00   Max.   :17.43
ts_plot(temperatura, slider = TRUE)
library(ggplot2)
library(forecast)
dias_semana <- c("lunes", "martes", "miercoles", "jueves", "viernes", "sabado", "domingo")


obs1$diasem <- factor(obs1$diasem, levels = dias_semana)
ggplot(obs1, aes(x=diasem, y=temperatura)) + 
  geom_boxplot() +
  xlab("Día de la semana") +
  geom_jitter(width=0.3, size=1, alpha=0.5) +
  ylab("Valor") +
  ggtitle("Gráfico de cajas por día de la semana")

ggplot(obs1, aes(x=as.factor(tipodia), y=temperatura)) + 
  geom_boxplot() +
  xlab("Día de la semana") +
  geom_jitter(width=0.3, size=1, alpha=0.5) +
  ylab("Valor") +
  ggtitle("Gráfico de cajas por día de la semana")

ggtsdisplay(temperatura)

ggtsdisplay(diff(temperatura))
## Warning: Removed 1 rows containing missing values (`geom_point()`).

adf.test(temperatura)
## Warning in adf.test(temperatura): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  temperatura
## Dickey-Fuller = -6.3103, Lag order = 12, p-value = 0.01
## alternative hypothesis: stationary
pp.test(temperatura)
## Warning in pp.test(temperatura): p-value smaller than printed p-value
## 
##  Phillips-Perron Unit Root Test
## 
## data:  temperatura
## Dickey-Fuller Z(alpha) = -840.45, Truncation lag parameter = 8, p-value
## = 0.01
## alternative hypothesis: stationary
temperatura=xts_to_ts(temperatura, start = as.Date("2015-01-01"),   frequency = 365)
ts_decompose(temperatura, type = "both")
ts_cor(temperatura)
ts_heatmap(temperatura)
ts_seasonal(temperatura,type = "all")
## Warning: No trace type specified and no positional attributes specified
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode

Test de Dickey Fuller y Phillips Perron

library(tseries)
adf.test(temperatura)
## Warning in adf.test(temperatura): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  temperatura
## Dickey-Fuller = -6.3103, Lag order = 12, p-value = 0.01
## alternative hypothesis: stationary
pp.test(temperatura)
## Warning in pp.test(temperatura): p-value smaller than printed p-value
## 
##  Phillips-Perron Unit Root Test
## 
## data:  temperatura
## Dickey-Fuller Z(alpha) = -840.45, Truncation lag parameter = 8, p-value
## = 0.01
## alternative hypothesis: stationary

Dickey Fuller y Phillips Perron con una diferencia

adf.test(diff(temperatura))
## Warning in adf.test(diff(temperatura)): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  diff(temperatura)
## Dickey-Fuller = -17.643, Lag order = 12, p-value = 0.01
## alternative hypothesis: stationary
pp.test(diff(temperatura))
## Warning in pp.test(diff(temperatura)): p-value smaller than printed p-value
## 
##  Phillips-Perron Unit Root Test
## 
## data:  diff(temperatura)
## Dickey-Fuller Z(alpha) = -1811.7, Truncation lag parameter = 8, p-value
## = 0.01
## alternative hypothesis: stationary
series=cbind(temperatura, PM2.5, OZONO, mnatural, mcardio)
plot(series)

Serie NO2

NO2<- xts::xts(x = obs1$NO2, order.by = obs1$fecha)
summary(NO2)
##      Index                          NO2        
##  Min.   :2015-01-01 00:00:00   Min.   : 8.687  
##  1st Qu.:2016-04-01 06:00:00   1st Qu.:24.821  
##  Median :2017-07-01 12:00:00   Median :30.953  
##  Mean   :2017-07-01 12:00:00   Mean   :32.140  
##  3rd Qu.:2018-09-30 18:00:00   3rd Qu.:38.148  
##  Max.   :2019-12-31 00:00:00   Max.   :91.999
ts_plot(NO2, slider = TRUE)
library(ggplot2)
library(forecast)
dias_semana <- c("lunes", "martes", "miercoles", "jueves", "viernes", "sabado", "domingo")


obs1$diasem <- factor(obs1$diasem, levels = dias_semana)
ggplot(obs1, aes(x=diasem, y=NO2)) + 
  geom_boxplot() +
  xlab("Día de la semana") +
  geom_jitter(width=0.3, size=1, alpha=0.5) +
  ylab("Valor") +
  ggtitle("Gráfico de cajas por día de la semana")

ggplot(obs1, aes(x=as.factor(tipodia), y=NO2)) + 
  geom_boxplot() +
  xlab("Día de la semana") +
  geom_jitter(width=0.3, size=1, alpha=0.5) +
  ylab("Valor") +
  ggtitle("Gráfico de cajas por día de la semana")

ggtsdisplay(NO2)

ggtsdisplay(diff(NO2))
## Warning: Removed 1 rows containing missing values (`geom_point()`).

adf.test(NO2)
## Warning in adf.test(NO2): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  NO2
## Dickey-Fuller = -5.6848, Lag order = 12, p-value = 0.01
## alternative hypothesis: stationary
pp.test(NO2)
## Warning in pp.test(NO2): p-value smaller than printed p-value
## 
##  Phillips-Perron Unit Root Test
## 
## data:  NO2
## Dickey-Fuller Z(alpha) = -457.32, Truncation lag parameter = 8, p-value
## = 0.01
## alternative hypothesis: stationary
NO2=xts_to_ts(NO2, start = as.Date("2015-01-01"),   frequency = 365)
ts_decompose(NO2, type = "both")
ts_cor(NO2)
ts_heatmap(NO2)
ts_seasonal(NO2,type = "all")
## Warning: No trace type specified and no positional attributes specified
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode

Test de Dickey Fuller y Phillips Perron

library(tseries)
adf.test(NO2)
## Warning in adf.test(NO2): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  NO2
## Dickey-Fuller = -5.6848, Lag order = 12, p-value = 0.01
## alternative hypothesis: stationary
pp.test(NO2)
## Warning in pp.test(NO2): p-value smaller than printed p-value
## 
##  Phillips-Perron Unit Root Test
## 
## data:  NO2
## Dickey-Fuller Z(alpha) = -457.32, Truncation lag parameter = 8, p-value
## = 0.01
## alternative hypothesis: stationary

Dickey Fuller y Phillips Perron con una diferencia

adf.test(diff(NO2))
## Warning in adf.test(diff(NO2)): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  diff(NO2)
## Dickey-Fuller = -19.372, Lag order = 12, p-value = 0.01
## alternative hypothesis: stationary
pp.test(diff(NO2))
## Warning in pp.test(diff(NO2)): p-value smaller than printed p-value
## 
##  Phillips-Perron Unit Root Test
## 
## data:  diff(NO2)
## Dickey-Fuller Z(alpha) = -1391.8, Truncation lag parameter = 8, p-value
## = 0.01
## alternative hypothesis: stationary
lag.plot(temperatura,lags=9,layout=c(3,3))