Análisis exploratorio de los datos

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(skimr)
library(fpp3)
## Registered S3 method overwritten by 'tsibble':
##   method               from 
##   as_tibble.grouped_df dplyr
## ── Attaching packages ──────────────────────────────────────────── fpp3 1.0.1 ──
## ✔ tsibble     1.1.6     ✔ feasts      0.4.1
## ✔ tsibbledata 0.4.1     ✔ fable       0.4.1
## ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
## ✖ lubridate::date()    masks base::date()
## ✖ dplyr::filter()      masks stats::filter()
## ✖ tsibble::intersect() masks base::intersect()
## ✖ tsibble::interval()  masks lubridate::interval()
## ✖ dplyr::lag()         masks stats::lag()
## ✖ tsibble::setdiff()   masks base::setdiff()
## ✖ tsibble::union()     masks base::union()

Cargue de datos

Se cargan los datos correspondientes cambiando el nombre de las columnas según la variable correspondiente.

Conversión de data.frame a time serie

### Dique 

ts_dique = dique %>% 
  mutate(fecha = as.Date(paste(año, mes, dia, sep = "-"), format = "%Y-%m-%d")) %>% 
  select(fecha, altura) %>% 
  as_tsibble(index = fecha)

### Guajaro

ts_guajaro = guajaro %>% 
  mutate(fecha = as.Date(paste(año, mes, dia, sep = "-"), format = "%Y-%m-%d")) %>% 
  select(fecha, altura) %>% 
  as_tsibble(index = fecha)

### Inkora

ts_inkora = inkora %>% 
  mutate(fecha = as.Date(paste(año, mes, dia, sep = "-"), format = "%Y-%m-%d")) %>% 
  select(fecha, altura) %>% 
  as_tsibble(index = fecha)

Incompletitud en la secuencia temporal

Los datos presentan incosistencias temporales en la fecha y su contituidad. Es decir, la serie temporal no es continua ya que salta periodos de tiempo. Para solucionar esto se emplea un código que asigna la fecha que sigue la secuecuencia siguiendo el orden cronológico correcto y asina a la columna de altura un NA.

## Secuencia adecuada 

## Dique 

ts_dique = ts_dique %>% 
  fill_gaps()

## Guajaro

ts_guajaro = ts_guajaro %>% 
  fill_gaps()


## Inkora

ts_inkora = ts_inkora %>% 
  fill_gaps()

Dique

Estadísticas generales

skim(ts_dique$altura) 
Data summary
Name ts_dique$altura
Number of rows 13516
Number of columns 1
_______________________
Column type frequency:
numeric 1
________________________
Group variables None

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
data 4292 0.68 398.5 132.59 0 318 414 500 704 ▁▂▇▇▁

Series de tiempo

Serie de tiempo diaría

ts_dique %>% 
  autoplot(altura) 

Doble de las desviaciones estándar

ts_dique_sd <- ts_dique %>%
    mutate(
    sd_altura = sd(altura, na.rm = TRUE)
  ) %>%
  as_tsibble(index = fecha)
ggplot(ts_dique_sd, aes(x = fecha, y = altura)) +
  geom_line(color = "blue") +  # Línea de la media
  geom_ribbon(aes(ymin = altura, ymax = altura + 2 * sd_altura),
              fill = "blue", alpha = 0.2) +  # Banda de 2 SD por arriba
  labs(x = "fecha [1D]", y = "Altura ± 2 SD") +
  theme_grey()

Serie de tiempo mensual

ts_dique %>% 
  mutate(Mes = yearmonth(fecha)) %>%  
  index_by(Mes) %>%  
  summarise(altura = mean(altura)) %>%  
  as_tsibble(index = Mes) %>% 
  autoplot(altura)

Serie de tiempo anual

ts_dique %>% 
  mutate(año = year(fecha)) %>%  
  index_by(año) %>%  
  summarise(altura = mean(altura, na.rm = TRUE)) %>%  
  as_tsibble(index = año) %>% 
  autoplot(altura)+
  geom_smooth(method = "lm", se = FALSE, formula = 'y ~ x')
## Warning: Removed 5 rows containing non-finite outside the scale range
## (`stat_smooth()`).

Box plot mensual

ts_dique %>% 
  mutate(mes = month(fecha, label = TRUE)) %>% 
  index_by(mes) %>% 
  ggplot(aes(x = mes, y = altura)) +
  geom_boxplot()
## Warning: Removed 4292 rows containing non-finite outside the scale range
## (`stat_boxplot()`).

Distribución de la altura del canal

ts_dique%>% 
  ggplot(aes(x = altura,)) +
  geom_histogram(fill = "lightblue", colour = "grey60", bins = 15) +
  labs(
    x = "Altura (m)", 
    y = "Frecuencia"
  )
## Warning: Removed 4292 rows containing non-finite outside the scale range
## (`stat_bin()`).

Guajaro

Estadísticas generales

skim(ts_guajaro$altura)
Data summary
Name ts_guajaro$altura
Number of rows 7794
Number of columns 1
_______________________
Column type frequency:
numeric 1
________________________
Group variables None

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
data 1197 0.85 397.64 69.52 200 353 401 448 587 ▁▅▇▆▁

Series de tiempo

Serie de tiempo diaría

ts_guajaro %>% 
  autoplot(altura)

Doble de las desviaciones estándar

ts_guajaro_sd <- ts_guajaro %>%
    mutate(
    sd_altura = sd(altura, na.rm = TRUE)
  ) %>%
  as_tsibble(index = fecha)
ggplot(ts_guajaro_sd, aes(x = fecha, y = altura)) +
  geom_line(color = "blue") +  # Línea de la media
  geom_ribbon(aes(ymin = altura, ymax = altura + 2 * sd_altura),
              fill = "blue", alpha = 0.2) +  # Banda de 2 SD por arriba
  labs(x = "fecha [1D]", y = "Altura ± 2 SD") +
  theme_grey()

Serie de tiempo mensual

ts_guajaro %>% 
  mutate(Mes = yearmonth(fecha)) %>%  
  index_by(Mes) %>%  
  summarise(altura = mean(altura)) %>%  
  as_tsibble(index = Mes) %>% 
  autoplot(altura)

Serie de tiempo anual

ts_guajaro %>% 
  mutate(año = year(fecha)) %>%  
  index_by(año) %>%  
  summarise(altura = mean(altura, na.rm = TRUE)) %>%  
  as_tsibble(index = año) %>% 
  autoplot(altura)+
  geom_smooth(method = "lm", se = FALSE, formula = 'y ~ x')
## Warning: Removed 1 row containing non-finite outside the scale range
## (`stat_smooth()`).

Box plot mensual

ts_guajaro %>% 
  mutate(mes = month(fecha, label = TRUE)) %>% 
  index_by(mes) %>% 
  ggplot(aes(x = mes, y = altura)) +
  geom_boxplot()
## Warning: Removed 1197 rows containing non-finite outside the scale range
## (`stat_boxplot()`).

Distribución de la altura del canal

ts_guajaro%>% 
  ggplot(aes(x = altura,)) +
  geom_histogram(fill = "lightblue", colour = "grey60", bins = 15) +
  labs(
    x = "Altura (m)", 
    y = "Frecuencia"
  )
## Warning: Removed 1197 rows containing non-finite outside the scale range
## (`stat_bin()`).

Inkora

Estadísticas generales

skim(ts_inkora$altura)
Data summary
Name ts_inkora$altura
Number of rows 11689
Number of columns 1
_______________________
Column type frequency:
numeric 1
________________________
Group variables None

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
data 1180 0.9 528 158.31 73 420 533 648 896 ▁▃▇▇▂

Series de tiempo

Serie de tiempo diaría

ts_inkora %>% 
  autoplot(altura)

Doble de las desviaciones estándar

ts_ikara_sd <- ts_guajaro %>%
    mutate(
    sd_altura = sd(altura, na.rm = TRUE)
  ) %>%
  as_tsibble(index = fecha)
ggplot(ts_ikara_sd, aes(x = fecha, y = altura)) +
  geom_line(color = "blue") +  # Línea de la media
  geom_ribbon(aes(ymin = altura, ymax = altura + 2 * sd_altura),
              fill = "blue", alpha = 0.2) +  # Banda de 2 SD por arriba
  labs(x = "fecha [1D]", y = "Altura ± 2 SD") +
  theme_grey()

Serie de tiempo mensual

ts_inkora %>% 
  mutate(Mes = yearmonth(fecha)) %>%  
  index_by(Mes) %>%  
  summarise(altura = mean(altura)) %>%  
  as_tsibble(index = Mes) %>% 
  autoplot(altura)

Serie de tiempo anual

ts_inkora %>% 
  mutate(año = year(fecha)) %>%  
  index_by(año) %>%  
  summarise(altura = mean(altura, na.rm = TRUE)) %>%  
  as_tsibble(index = año) %>% 
  autoplot(altura)+
  geom_smooth(method = "lm", se = FALSE, formula = 'y ~ x')

Box plot mensual

ts_inkora %>% 
  mutate(mes = month(fecha, label = TRUE)) %>% 
  index_by(mes) %>% 
  ggplot(aes(x = mes, y = altura)) +
  geom_boxplot()
## Warning: Removed 1180 rows containing non-finite outside the scale range
## (`stat_boxplot()`).

Distribución de la altura del canal

ts_inkora%>% 
  ggplot(aes(x = altura,)) +
  geom_histogram(fill = "lightblue", colour = "grey60", bins = 15) +
  labs(
    x = "Altura (m)", 
    y = "Frecuencia"
  )
## Warning: Removed 1180 rows containing non-finite outside the scale range
## (`stat_bin()`).

Dique, Guajaro, Inkora

Serie de tiempo

ts_estaciones = ts_dique %>%
  left_join(ts_guajaro, by = "fecha") %>%
  left_join(ts_inkora, by = "fecha") %>%
  as_tsibble(index = fecha) %>% 
  rename(
    dique = altura.x, 
    guajaro = altura.y,
    inkora = altura
  ) %>% 
  pivot_longer(cols = c(dique, guajaro, inkora), names_to = "estacion", values_to = "altura")

ts_estaciones %>% 
  filter(fecha < as.Date("2010-01-01")) %>% 
  autoplot(na.rm = FALSE)
## Plot variable not specified, automatically selected `.vars = altura`
## Warning: Removed 2841 rows containing missing values or values outside the scale range
## (`geom_line()`).