SECCION 4: RESULTADOS ESTADISTICOS Y ECONOMICOS

Proyecto: Monitoreo Domestico de Agua - Medidor 12215105062 (EPMAPS)

1. Tablas completas de lecturas acumuladas y consumos diarios (m3 y L)

library(dplyr)
library(ggplot2)
library(DT)
library(gt)


fase1_lecturas <- data.frame(
  Dia = 0:7,
  Fecha = c("Lun 19 Oct", "Mar 20 Oct", "Mie 21 Oct", "Jue 22 Oct",
            "Vie 23 Oct", "Sab 24 Oct", "Dom 25 Oct", "Lun 26 Oct"),
  Lectura_m3 = c(471.19, 471.61, 471.95, 472.28, 473.18, 473.76, 474.15, 474.53)
) %>%
  mutate(
    Consumo_m3 = c(NA, round(diff(Lectura_m3), 2)),
    Consumo_L = ifelse(is.na(Consumo_m3), NA, round(Consumo_m3 * 1000, 0)),
    Fase = "Fase 1: Linea Base"
  )

fase3_lecturas <- data.frame(
  Dia = 0:7,
  Fecha = c("Lun 2 Nov", "Mar 3 Nov", "Mie 4 Nov", "Jue 5 Nov",
            "Vie 6 Nov", "Sab 7 Nov", "Dom 8 Nov", "Lun 9 Nov"),
  Lectura_m3 = c(477.91, 478.19, 478.50, 479.38, 479.68, 480.27, 480.48, 480.78)
) %>%
  mutate(
    Consumo_m3 = c(NA, round(diff(Lectura_m3), 2)),
    Consumo_L = ifelse(is.na(Consumo_m3), NA, round(Consumo_m3 * 1000, 0)),
    Fase = "Fase 3: Con Ahorro"
  )

registro_total <- bind_rows(fase1_lecturas, fase3_lecturas)

# =====================================================================
# 2. TABLA INTERACTIVA 1: REGISTRO DIARIO COMPLETO (LIBRERIA DT)
# Permite filtrar por fase, ordenar por consumo y buscar en el Viewer
# =====================================================================
tabla_interactiva_registros <- datatable(
  registro_total %>% select(Fase, Dia, Fecha, Lectura_m3, Consumo_m3, Consumo_L),
  colnames = c("Fase", "Dia", "Fecha", "Lectura Medidor (m3)", "Consumo Diario (m3)", "Consumo (L)"),
  filter = "top",
  rownames = FALSE,
  options = list(
    pageLength = 16,
    autoWidth = TRUE,
    dom = "Bfrtip"
  ),
  caption = "Tabla 1. Registro interactivo de lecturas y consumos diarios (Medidor 12215105062)"
) %>%
  formatRound(columns = c("Lectura_m3", "Consumo_m3"), digits = 2) %>%
  formatStyle(
    "Consumo_m3",
    background = styleColorBar(c(0, 0.95), "#AED6F1"),
    backgroundSize = "95% 75%",
    backgroundRepeat = "no-repeat",
    backgroundPosition = "center"
  )

# Mostrar en el panel Viewer de RStudio
tabla_interactiva_registros

2. TABLA INTERACTIVA 2: RESUMEN ESTADISTICO COMPARATIVO

datos_diarios <- registro_total %>% filter(!is.na(Consumo_m3))

resumen_stats <- datos_diarios %>%
  group_by(Fase) %>%
  summarise(
    Total_m3 = round(sum(Consumo_m3), 2),
    Total_L = round(sum(Consumo_L), 0),
    Media_m3 = round(mean(Consumo_m3), 4),
    Mediana_m3 = round(median(Consumo_m3), 2),
    Desv_Est_m3 = round(sd(Consumo_m3), 4),
    Min_m3 = min(Consumo_m3),
    Max_m3 = max(Consumo_m3),
    .groups = "drop"
  )

tabla_interactiva_stats <- datatable(
  resumen_stats,
  colnames = c("Fase", "Total (m3)", "Total (L)", "Media (m3/dia)", "Mediana (m3)", "DE (m3)", "Min (m3)", "Max (m3)"),
  
  rownames = FALSE,
  options = list(dom = "t", ordering = TRUE),
  caption = "Tabla 2. Estadisticos descriptivos por fase experimental"
)

tabla_interactiva_stats

3. TABLA INTERACTIVA 3: AHORRO HIDRICO Y ECONOMICO

# =====================================================================
# 4. TABLA INTERACTIVA 3: AHORRO HIDRICO Y ECONOMICO (LIBRERIA GT)
# Ideal para visualizar en RStudio y copiar/exportar limpia a Word
# =====================================================================
tarifa_variable <- (6.21 + 2.40) / 16 # 0.538125 USD/m3
ahorro_sem_m3 <- 3.34 - 2.87          # 0.47 m3/semana
ahorro_dia_m3 <- ahorro_sem_m3 / 7

df_economico <- data.frame(
  Categoria = c(
    rep("Ahorro Hidrico", 5),
    rep("Evaluacion Economica (EPMAPS)", 4)
  ),
  Indicador = c(
    "Ahorro semanal neto (7 dias)",
    "Ahorro diario promedio",
    "Reduccion porcentual exacta",
    "Proyeccion mensual (30 dias)",
    "Proyeccion anual (365 dias)",
    "Tarifa variable efectiva (Agua + Alcantarillado)",
    "Ahorro economico semanal",
    "Ahorro economico mensual (30 dias)",
    "Ahorro economico anual (365 dias)"
  ),
  Valor = c(
    "0.47 m3 (470 L)",
    "0.0671 m3/dia (67.1 L/dia)",
    "14.07 %",
    "2.01 m3/mes (2,014 L/mes)",
    "24.51 m3/ano (24,507 L/ano)",
    "0.5381 USD/m3",
    "0.25 USD/semana",
    "1.08 USD/mes",
    "13.19 USD/ano"
  )
)

tabla_interactiva_economica <- datatable(
  df_economico,
  colnames = c("Categoria", "Parametro Evaluado", "Resultado Obtenido"),
  rownames = FALSE,
  options = list(pageLength = 10, dom = "ft"),
  caption = "Tabla 3. Resumen de Impacto Hidrico y Economico (Medidor 12215105062)"
)

tabla_interactiva_economica

GRAFICAS del proyecto

FIGURA 1: Monitoreo Domestico de Agua - Medidor 12215105062 (Quito)

datos_diarios <- data.frame(
  Dia = rep(1:7, times = 2),
  Fase = rep(
    c("Fase 1: Linea Base (Total: 3.34 m3)", "Fase 3: Con Ahorro (Total: 2.87 m3)"),
    each = 7
  ),
  Consumo_m3 = c(
    0.42, 0.34, 0.33, 0.90, 0.58, 0.39, 0.38, # Fase 1 (Suma = 3.34 m3)
    0.28, 0.31, 0.88, 0.30, 0.59, 0.21, 0.28  # Fase 3 (Lecturas: 477.91 a 480.78 = 2.87 m3)
  )
)

media_f1 <- 3.34 / 7 # 0.4771 m3/dia (0.48)
media_f3 <- 2.87 / 7 # 0.4100 m3/dia (0.41)

fig1 <- ggplot(datos_diarios, aes(x = factor(Dia), y = Consumo_m3, fill = Fase)) +
  geom_col(position = position_dodge(width = 0.72), width = 0.64, alpha = 0.92) +
  geom_text(
    aes(label = sprintf("%.2f", Consumo_m3)),
    position = position_dodge(width = 0.72),
    vjust = -0.45,
    size = 3.1,
    color = "#1C2833",
    fontface = "bold"
  ) +
  geom_hline(yintercept = media_f1, color = "#922B21", linetype = "dashed", linewidth = 0.85) +
  geom_hline(yintercept = media_f3, color = "#145A32", linetype = "dotted", linewidth = 1.05) +
  # Etiqueta Media Fase 1 (arriba a la derecha, sobre su linea)
  geom_label(
    aes(x = 7.42, y = media_f1 + 0.012, label = sprintf("Media F1: %.2f m3/dia", media_f1)),
    inherit.aes = FALSE,
    hjust = 1,
    vjust = 0,
    size = 2.8,
    fontface = "bold",
    color = "#922B21",
    fill = "white",
    label.padding = unit(0.16, "lines"),
    label.r = unit(0.15, "lines"),
    linewidth = 0.25
  ) +
  # Etiqueta Media Fase 3 (justo encima de su linea punteada, en zona despejada)
  geom_label(
    aes(x = 4.42, y = media_f3 + 0.012, label = sprintf("Media F3: %.2f m3/dia", media_f3)),
    inherit.aes = FALSE,
    hjust = 0.5,
    vjust = 0,
    size = 2.8,
    fontface = "bold",
    color = "#145A32",
    fill = "white",
    label.padding = unit(0.16, "lines"),
    label.r = unit(0.15, "lines"),
    linewidth = 0.25
  ) +
  coord_cartesian(xlim = c(0.55, 7.55), ylim = c(0, 1.05), clip = "on") +
  scale_fill_manual(values = c("#C0392B", "#1E8449")) +
  scale_y_continuous(breaks = seq(0, 1.0, 0.2)) +
  labs(
    title = "Consumo Diario de Agua: Fase 1 vs. Fase 3",
    subtitle = "Medidor Itron Multimag+ (12215105062) - Quito",
    x = "Dia de monitoreo (1 a 7)",
    y = "Consumo diario (m3)",
    fill = "Etapa del estudio"
  ) +
  theme_minimal(base_size = 11) +
  theme(
    plot.title = element_text(face = "bold", hjust = 0.5),
    plot.subtitle = element_text(hjust = 0.5, color = "gray35"),
    legend.position = "top",
    panel.grid.minor = element_blank(),
    axis.line.x = element_line(color = "gray60", linewidth = 0.5),
    axis.line.y = element_line(color = "gray60", linewidth = 0.5)
  )

print(fig1)

ggsave("figura1_consumo_diario.png", fig1, width = 8.2, height = 4.8, dpi = 300)

FIGURA 2: DIAGRAMA DE CAJA (BOXPLOT) DE DISTRIBUCION Y DISPERSION

# =====================================================================
# FIGURA 2: DIAGRAMA DE CAJA (BOXPLOT) DE DISTRIBUCION Y DISPERSION
# =====================================================================
datos_box <- datos_diarios %>%
  mutate(
    Fase_Corta = ifelse(grepl("Fase 1", Fase), "Fase 1:\nLinea Base", "Fase 3:\nCon Ahorro")
  )

fig2 <- ggplot(datos_box, aes(x = Fase_Corta, y = Consumo_m3, fill = Fase_Corta)) +
  geom_boxplot(width = 0.45, alpha = 0.75, outlier.shape = 21, outlier.size = 2.8) +
  geom_jitter(width = 0.08, size = 2.5, alpha = 0.85, color = "#1C2833") +
  scale_fill_manual(values = c("#E6B0AA", "#A9DFBF")) +
  scale_y_continuous(limits = c(0.15, 1.0), breaks = seq(0.2, 1.0, 0.1)) +
  labs(
    title = "Distribucion y Dispersion del Consumo Diario",
    subtitle = "Comparacion de mediana, cuartiles y valores extremos (m3/dia)",
    x = "",
    y = "Consumo diario (m3)"
  ) +
  theme_minimal(base_size = 11) +
  theme(
    plot.title = element_text(face = "bold", hjust = 0.5),
    plot.subtitle = element_text(hjust = 0.5, color = "gray35"),
    legend.position = "none",
    panel.grid.minor = element_blank()
  )

print(fig2)

ggsave("figura2_boxplot_fases.png", fig2, width = 7, height = 4.5, dpi = 300)

FIGURA 3: PROYECCION DE AHORRO HIDRICO (m3) Y ECONOMICO (USD)

library(patchwork)
df_proy <- data.frame(
  Periodo = factor(
    c("Semanal\n(7 dias)", "Mensual\n(30 dias)", "Anual\n(365 dias)"),
    levels = c("Semanal\n(7 dias)", "Mensual\n(30 dias)", "Anual\n(365 dias)")
  ),
  Ahorro_m3 = c(0.47, 2.01, 24.51),
  Ahorro_USD = c(0.25, 1.08, 13.19)
)

g3_hidrico <- ggplot(df_proy, aes(x = Periodo, y = Ahorro_m3)) +
  geom_bar(stat = "identity", fill = "#2E86C1", width = 0.5) +
  geom_text(aes(label = sprintf("%.2f", Ahorro_m3)), vjust = -0.4, fontface = "bold", size = 3.5) +
  scale_y_continuous(limits = c(0, 29)) +
  labs(
    title = "Ahorro Hidrico Proyectado (m3)",
    x = "",
    y = "Volumen ahorrado (m3)"
  ) +
  theme_minimal(base_size = 10.5) +
  theme(plot.title = element_text(face = "bold", hjust = 0.5), panel.grid.minor = element_blank())

g3_economico <- ggplot(df_proy, aes(x = Periodo, y = Ahorro_USD)) +
  geom_bar(stat = "identity", fill = "#27AE60", width = 0.5) +
  geom_text(aes(label = sprintf("$%.2f", Ahorro_USD)), vjust = -0.4, fontface = "bold", size = 3.5) +
  scale_y_continuous(limits = c(0, 16)) +
  labs(
    title = "Ahorro Economico Proyectado (USD)",
    x = "",
    y = "Ahorro estimado (USD)"
  ) +
  theme_minimal(base_size = 10.5) +
  theme(plot.title = element_text(face = "bold", hjust = 0.5), panel.grid.minor = element_blank())

fig3 <- g3_hidrico + g3_economico +
  plot_annotation(
    title = "Proyeccion de Ahorro Hidrico y Economico (Tarifa EPMAPS: 0.5381 USD/m3)",
    theme = theme(plot.title = element_text(face = "bold", size = 12, hjust = 0.5))
  )

print(fig3)

ggsave("figura3_proyeccion_ahorro.png", fig3, width = 8.5, height = 4.3, dpi = 300)