Clase Series de tiempo

Published

February 22, 2023

Las paqueterías necesarias.

library(tidyverse)
library(fpp3)

Tarea Ejercicio 4

i)

fma::plastics
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
   Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
1  742  697  776  898 1030 1107 1165 1216 1208 1131  971  783
2  741  700  774  932 1099 1223 1290 1349 1341 1296 1066  901
3  896  793  885 1055 1204 1326 1303 1436 1473 1453 1170 1023
4  951  861  938 1109 1274 1422 1486 1555 1604 1600 1403 1209
5 1030 1032 1126 1285 1468 1637 1611 1608 1528 1420 1119 1013
str(fma::plastics)
 Time-Series [1:60] from 1 to 5.92: 742 697 776 898 1030 ...
class(fma::plastics)
[1] "ts"

Necesitamos convertir a tsibble.

plastico <- as_tsibble(fma::plastics)
plastico
plastico |> 
  autoplot(value)

ii)

plastico_dcmp <- plastico |> 
  model(
    clasica = classical_decomposition(value, type = "multiplicative")
  )

plastico_comp <- plastico_dcmp |> 
  components()

plastico_comp |> 
  autoplot()
Warning: Removed 6 rows containing missing values (`geom_line()`).

iii)

plastico |> 
  autoplot(value, color = "grey") +
  autolayer(plastico_comp, season_adjust, color = "firebrick")

iv)

plastico2 <- plastico

plastico2
plastico2$value[20] <- plastico2$value[20] + 500

plastico2
plastico2 |> 
  autoplot(value)

La descomposición, ahora con la serie con el outlier.

plastico2_dcmp <- plastico2 |> 
  model(
    clasica = classical_decomposition(value, type = "multiplicative")
  )

plastico2_comp <- plastico2_dcmp |> 
  components()

plastico2_comp |> 
  autoplot()
Warning: Removed 6 rows containing missing values (`geom_line()`).

Podemos utilizar la descomposición STL para controlar por el outlier y que no se vea afectada nuestra descomposición.

plastico2_dcmp <- plastico2 |> 
  model(
    stl = STL(value, robust = TRUE)
  )

plastico2_comp <- plastico2_dcmp |> 
  components()

plastico2_comp |> 
  autoplot()

v)

plastico3 <- plastico


plastico3$value[50] <- plastico3$value[50] + 500

plastico3 |> 
  autoplot(value)

plastico3_dcmp <- plastico3 |> 
  model(
    clasica = classical_decomposition(value, type = "multiplicative")
  )

plastico3_dcmp |> 
  components() |> 
  autoplot()
Warning: Removed 6 rows containing missing values (`geom_line()`).

plastico4 <- plastico


plastico4$value[10] <- plastico4$value[10] + 500

plastico4 |> 
  autoplot(value)

plastico4_dcmp <- plastico4 |> 
  model(
    clasica = classical_decomposition(value, type = "multiplicative")
  )

plastico4_dcmp |> 
  components() |> 
  autoplot()
Warning: Removed 6 rows containing missing values (`geom_line()`).

feasts

tourism

Para obtener la estadística descriptiva de cada serie, podríamos utilizar las funciones del tidyverse: group_by() y summarise():

tourism |> 
  as_tibble() |> 
  group_by(Region, State, Purpose) |> 
  summarise(
    Media = mean(Trips),
    SD = sd(Trips),
    Max = max(Trips),
    Min = min(Trips),
    .groups = "drop"
  )

features()

Al trabajar con tsibbles, podemos utilizar la función features() y ahorrarnos código:

tourism |> 
  features(Trips, list(Media = mean,
                       SD = sd,
                       Max = max,
                       Min = min))

Datatables

Convertimos las variables de texto a factores utilizando as_factor().

Esto NO es recomendable:

turismo <- tourism |> 
  mutate(
    Region = as_factor(Region),
    State = as_factor(State),
    Purpose = as_factor(Purpose)
  )
turismo

Mejor utilizar mutate() con across()

turismo <- tourism |> 
  mutate(across(Region:Purpose, as_factor))
turismo
tourism |> 
  rename(Region_key = Region,
         State_key = State,
         Purpose_key = Purpose) |> 
  mutate(across(ends_with("_key"), as_factor)) |> 
  features(Trips, list(Media = mean,
                       SD = sd,
                       Max = max,
                       Min = min)) |> 
  DT::datatable(filter = "top")