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.4     
## ── 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
dique = read_table("Dique.txt", col_names = c("año", "mes", "dia", "altura"))
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   año = col_double(),
##   mes = col_character(),
##   dia = col_character(),
##   altura = col_double()
## )
## Warning: 9224 parsing failures.
## row col  expected    actual        file
##   1  -- 4 columns 5 columns 'Dique.txt'
##   2  -- 4 columns 5 columns 'Dique.txt'
##   3  -- 4 columns 5 columns 'Dique.txt'
##   4  -- 4 columns 5 columns 'Dique.txt'
##   5  -- 4 columns 5 columns 'Dique.txt'
## ... ... ......... ......... ...........
## See problems(...) for more details.
guajaro = read_table("Guajaro.txt", col_names = c("año", "mes", "dia", "altura"))
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   año = col_double(),
##   mes = col_character(),
##   dia = col_character(),
##   altura = col_double()
## )
## Warning: 6597 parsing failures.
## row col  expected    actual          file
##   1  -- 4 columns 5 columns 'Guajaro.txt'
##   2  -- 4 columns 5 columns 'Guajaro.txt'
##   3  -- 4 columns 5 columns 'Guajaro.txt'
##   4  -- 4 columns 5 columns 'Guajaro.txt'
##   5  -- 4 columns 5 columns 'Guajaro.txt'
## ... ... ......... ......... .............
## See problems(...) for more details.
inkora = read_table("Inkora.txt", col_names = c("año", "mes", "dia", "altura"))
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   año = col_double(),
##   mes = col_character(),
##   dia = col_character(),
##   altura = col_double()
## )
Bomba = read_table("Casa_Bomba.txt", col_names = c("año", "mes", "dia", "altura"))
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   año = col_double(),
##   mes = col_character(),
##   dia = col_character(),
##   altura = col_double()
## )
## Warning: 6224 parsing failures.
## row col  expected    actual             file
##   1  -- 4 columns 5 columns 'Casa_Bomba.txt'
##   2  -- 4 columns 5 columns 'Casa_Bomba.txt'
##   3  -- 4 columns 5 columns 'Casa_Bomba.txt'
##   4  -- 4 columns 5 columns 'Casa_Bomba.txt'
##   5  -- 4 columns 5 columns 'Casa_Bomba.txt'
## ... ... ......... ......... ................
## See problems(...) for more details.

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)
### Inkora

ts_bomba = Bomba %>% 
  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()

## CRA

ts_bomba = ts_bomba %>% 
  fill_gaps()


## Inkora

ts_inkora = ts_inkora %>% 
  fill_gaps()

#Bomba

Estadísticas generales

skim(ts_bomba$altura) 
Data summary
Name ts_bomba$altura
Number of rows 8128
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 1903 0.77 375.6 64.88 218 328 371 425 547 ▂▇▇▆▁

##Grafica

ggplot(ts_bomba, aes(x = fecha, y = altura)) +
  geom_line(color = "black", size = 0.6) +
  geom_hline(yintercept = 375.6039, color = "red", linetype = "solid", linewidth = 0.8) +
  geom_hline(yintercept = 375.6039 + 64.88179, color = "blue", linetype = "dashed", linewidth = 0.8) +
  labs(
    title = "Altura del Agua - Estación Casa Bomba",
    x = "Fecha",
    y = "Altura (cm)"
  ) +
  theme_bw(base_size = 12)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

#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 ▁▂▇▇▁

Grafica

ggplot(ts_dique, aes(x = fecha, y = altura)) +
  geom_line(color = "black", size = 0.6) +
  geom_hline(yintercept = 398.4978, color = "red", linetype = "solid", linewidth = 0.8) +
  geom_hline(yintercept = 398.4978 + 132.5882, color = "blue", linetype = "dashed", linewidth = 0.8) +
  labs(
    title = "Altura del Agua - Estación Dique",
    x = "Fecha",
    y = "Altura (cm)"
  ) +
  theme_bw(base_size = 12)

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

Grafica

ggplot(ts_guajaro, aes(x = fecha, y = altura)) +
  geom_line(color = "black", size = 0.6) +
  geom_hline(yintercept = 397.6359, color = "red", linetype = "solid", linewidth = 0.8) +
  geom_hline(yintercept = 397.6359 + 69.51968, color = "blue", linetype = "dashed", linewidth = 0.8) +
  labs(
    title = "Altura del Agua - Estación Guájaro",
    x = "Fecha",
    y = "Altura (cm)"
  ) +
  theme_bw(base_size = 12)

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 ▁▃▇▇▂
head(ts_inkora)
## # A tsibble: 6 x 2 [1D]
##   fecha      altura
##   <date>      <dbl>
## 1 1980-01-01    652
## 2 1980-01-02    643
## 3 1980-01-03    634
## 4 1980-01-04    625
## 5 1980-01-05    614
## 6 1980-01-06    607
ggplot(ts_inkora, aes(x = fecha, y = altura)) +
  geom_line(color = "black", size = 0.6) +
  geom_hline(yintercept = 527.9994, color = "red", linetype = "solid", linewidth = 0.8) +
  geom_hline(yintercept = 527.9994 + 158.3073, color = "blue", linetype = "dashed", linewidth = 0.8) +
  labs(
    title = "Altura del Agua - Estación Inkora",
    x = "Fecha",
    y = "Altura (cm)"
  ) +
  theme_bw(base_size = 12)

Dique, Guajaro, Inkora

Serie de tiempo

# Combinar todas las series temporales
ts_estaciones_completas <- ts_dique %>%
  rename(dique = altura) %>%
  full_join(ts_guajaro %>% rename(guajaro = altura), by = "fecha") %>%
  full_join(ts_inkora %>% rename(inkora = altura), by = "fecha") %>%
  full_join(ts_bomba %>% rename(bomba = altura), by = "fecha") %>%
  pivot_longer(cols = c(dique, guajaro, inkora, bomba),
               names_to = "estacion", values_to = "altura")

# Graficar en formato científico
ggplot(ts_estaciones_completas, aes(x = fecha, y = altura, color = estacion)) +
  geom_line(size = 0.7, na.rm = TRUE) +
  scale_color_manual(
    values = c("dique" = "#1f77b4", "guajaro" = "#ff7f0e", "inkora" = "#2ca02c", "bomba" = "#d62728"),
    labels = c("Dique", "Guájaro", "Inkora", "Casa Bomba")
  ) +
  labs(
    title = "Altura del agua en estaciones del Embalse del Guájaro",
    x = "Fecha",
    y = "Altura (cm)",
    color = "Estación"
  ) +
  theme_bw(base_size = 12) +
  theme(
    plot.title = element_text(hjust = 0.5, face = "bold"),
    legend.title = element_text(face = "bold"),
    legend.position = "bottom"
  )

# Combinar todas las series temporales en un solo tibble largo
ts_estaciones_completas <- ts_dique %>%
  rename(dique = altura) %>%
  full_join(ts_guajaro %>% rename(guajaro = altura), by = "fecha") %>%
  full_join(ts_inkora %>% rename(inkora = altura), by = "fecha") %>%
  full_join(ts_bomba %>% rename(bomba = altura), by = "fecha") %>%
  pivot_longer(
    cols = c(dique, guajaro, inkora, bomba),
    names_to = "estacion",
    values_to = "altura"
  )

# Renombrar estaciones para mostrar nombres más legibles
ts_estaciones_completas$estacion <- recode(
  ts_estaciones_completas$estacion,
  dique = "Dique",
  guajaro = "Guájaro",
  inkora = "Inkora",
  bomba = "Casa Bomba"
)

# Crear gráfico con facetas
ggplot(ts_estaciones_completas, aes(x = fecha, y = altura)) +
  geom_line(color = "black", size = 0.6, na.rm = TRUE) +
  facet_wrap(~ estacion, scales = "free_y", ncol = 2) +
  labs(
    title = "Altura del agua por estación - Embalse del Guájaro",
    x = "Fecha",
    y = "Altura (cm)"
  ) +
  theme_bw(base_size = 12) +
  theme(
    strip.text = element_text(face = "bold"),
    plot.title = element_text(hjust = 0.5, face = "bold")
  )