#Cargar librerías necesarias
library(readxl)  # Para leer archivos Excel
## Warning: package 'readxl' was built under R version 4.4.3
library(tseries)  # Para pruebas de estacionariedad
## Warning: package 'tseries' was built under R version 4.4.3
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(forecast)  # Para modelado ARIMA y pronósticos
## Warning: package 'forecast' was built under R version 4.4.3
library(ggplot2)  # Para visualización de datos
## Warning: package 'ggplot2' was built under R version 4.4.3
library(plotly)  # Para gráficos interactivos
## Warning: package 'plotly' was built under R version 4.4.3
## 
## Adjuntando el paquete: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(timetk)   
## Warning: package 'timetk' was built under R version 4.4.3
library(readxl)
data_col <- read_excel("C:/Users/luisi/Downloads/data_col.xlsx",  col_types = c("date", "numeric", "numeric"))
## Warning: Expecting numeric in B2 / R2C2: got '1.396,35'
## Warning: Coercing text to numeric in C2 / R2C3: '1396.35'
## Warning: Expecting numeric in B3 / R3C2: got '1.306,62'
## Warning: Coercing text to numeric in C3 / R3C3: '1306.62'
## Warning: Expecting numeric in B4 / R4C2: got '1.331,35'
## Warning: Coercing text to numeric in C4 / R4C3: '1331.35'
## Warning: Expecting numeric in B5 / R5C2: got '1.317,24'
## Warning: Coercing text to numeric in C5 / R5C3: '1317.24'
## Warning: Expecting numeric in B6 / R6C2: got '1.246,59'
## Warning: Coercing text to numeric in C6 / R6C3: '1246.59'
## Warning: Expecting numeric in B7 / R7C2: got '1.218,82'
## Warning: Coercing text to numeric in C7 / R7C3: '1218.82'
## Warning: Expecting numeric in B8 / R8C2: got '1.218,13'
## Warning: Coercing text to numeric in C8 / R8C3: '1218.13'
## Warning: Expecting numeric in B9 / R9C2: got '1.114,36'
## Warning: Coercing text to numeric in C9 / R9C3: '1114.36'
## Warning: Expecting numeric in B10 / R10C2: got '1.153,71'
## Warning: Coercing text to numeric in C10 / R10C3: '1153.71'
## Warning: Expecting numeric in B11 / R11C2: got '1.175,09'
## Warning: Coercing text to numeric in C11 / R11C3: '1175.09'
## Warning: Expecting numeric in B12 / R12C2: got '1.244,17'
## Warning: Coercing text to numeric in C12 / R12C3: '1244.17'
## Warning: Expecting numeric in B13 / R13C2: got '1.336,27'
## Warning: Coercing text to numeric in C13 / R13C3: '1336.27'
## Warning: Expecting numeric in B14 / R14C2: got '1.342,42'
## Warning: Coercing text to numeric in C14 / R14C3: '1342.42'
## Warning: Expecting numeric in B15 / R15C2: got '1.292,51'
## Warning: Coercing text to numeric in C15 / R15C3: '1292.51'
## Warning: Expecting numeric in B16 / R16C2: got '1.313,18'
## Warning: Coercing text to numeric in C16 / R16C3: '1313.18'
## Warning: Expecting numeric in B17 / R17C2: got '1.308,22'
## Warning: Coercing text to numeric in C17 / R17C3: '1308.22'
## Warning: Expecting numeric in B18 / R18C2: got '1.380,18'
## Warning: Coercing text to numeric in C18 / R18C3: '1380.18'
## Warning: Expecting numeric in B19 / R19C2: got '1.338,83'
## Warning: Coercing text to numeric in C19 / R19C3: '1338.83'
## Warning: Expecting numeric in B20 / R20C2: got '1.365,53'
## Warning: Coercing text to numeric in C20 / R20C3: '1365.53'
## Warning: Expecting numeric in B21 / R21C2: got '1.286,07'
## Warning: Coercing text to numeric in C21 / R21C3: '1286.07'
## Warning: Expecting numeric in B22 / R22C2: got '1.351,68'
## Warning: Coercing text to numeric in C22 / R22C3: '1351.68'
## Warning: Expecting numeric in B23 / R23C2: got '1.357,47'
## Warning: Coercing text to numeric in C23 / R23C3: '1357.47'
## Warning: Expecting numeric in B24 / R24C2: got '1.326,31'
## Warning: Coercing text to numeric in C24 / R24C3: '1326.31'
## Warning: Expecting numeric in B25 / R25C2: got '1.365,61'
## Warning: Coercing text to numeric in C25 / R25C3: '1365.61'
## Warning: Expecting numeric in B26 / R26C2: got '1.371,54'
## Warning: Coercing text to numeric in C26 / R26C3: '1371.54'
## Warning: Expecting numeric in B27 / R27C2: got '1.439,48'
## Warning: Coercing text to numeric in C27 / R27C3: '1439.48'
## Warning: Expecting numeric in B28 / R28C2: got '1.462,90'
## Warning: Coercing text to numeric in C28 / R28C3: '1462.90'
## Warning: Expecting numeric in B29 / R29C2: got '1.481,37'
## Warning: Coercing text to numeric in C29 / R29C3: '1481.37'
## Warning: Expecting numeric in B30 / R30C2: got '1.482,27'
## Warning: Coercing text to numeric in C30 / R30C3: '1482.27'
## Warning: Expecting numeric in B31 / R31C2: got '1.487,52'
## Warning: Coercing text to numeric in C31 / R31C3: '1487.52'
## Warning: Expecting numeric in B32 / R32C2: got '1.424,58'
## Warning: Coercing text to numeric in C32 / R32C3: '1424.58'
## Warning: Expecting numeric in B33 / R33C2: got '1.445,23'
## Warning: Coercing text to numeric in C33 / R33C3: '1445.23'
## Warning: Expecting numeric in B34 / R34C2: got '1.513,65'
## Warning: Coercing text to numeric in C34 / R34C3: '1513.65'
## Warning: Expecting numeric in B35 / R35C2: got '1.558,18'
## Warning: Coercing text to numeric in C35 / R35C3: '1558.18'
## Warning: Expecting numeric in B36 / R36C2: got '1.478,33'
## Warning: Coercing text to numeric in C36 / R36C3: '1478.33'
## Warning: Expecting numeric in B37 / R37C2: got '1.455,52'
## Warning: Coercing text to numeric in C37 / R37C3: '1455.52'
## Warning: Expecting numeric in B38 / R38C2: got '1.565,56'
## Warning: Coercing text to numeric in C38 / R38C3: '1565.56'
## Warning: Expecting numeric in B39 / R39C2: got '1.546,71'
## Warning: Coercing text to numeric in C39 / R39C3: '1546.71'
## Warning: Expecting numeric in B40 / R40C2: got '1.577,01'
## Warning: Coercing text to numeric in C40 / R40C3: '1577.01'
## Warning: Expecting numeric in B41 / R41C2: got '1.526,60'
## Warning: Coercing text to numeric in C41 / R41C3: '1526.60'
## Warning: Expecting numeric in B42 / R42C2: got '1.542,77'
## Warning: Coercing text to numeric in C42 / R42C3: '1542.77'
## Warning: Expecting numeric in B43 / R43C2: got '1.506,07'
## Warning: Coercing text to numeric in C43 / R43C3: '1506.07'
## Warning: Expecting numeric in B44 / R44C2: got '1.392,18'
## Warning: Coercing text to numeric in C44 / R44C3: '1392.18'
## Warning: Expecting numeric in B45 / R45C2: got '1.379,24'
## Warning: Coercing text to numeric in C45 / R45C3: '1379.24'
## Warning: Expecting numeric in B46 / R46C2: got '1.325,93'
## Warning: Coercing text to numeric in C46 / R46C3: '1325.93'
## Warning: Expecting numeric in B47 / R47C2: got '1.447,01'
## Warning: Coercing text to numeric in C47 / R47C3: '1447.01'
## Warning: Expecting numeric in B48 / R48C2: got '1.508,27'
## Warning: Coercing text to numeric in C48 / R48C3: '1508.27'
## Warning: Expecting numeric in B49 / R49C2: got '1.587,74'
## Warning: Coercing text to numeric in C49 / R49C3: '1587.74'
## Warning: Expecting numeric in B50 / R50C2: got '1.573,64'
## Warning: Coercing text to numeric in C50 / R50C3: '1573.64'
## Warning: Expecting numeric in B51 / R51C2: got '1.487,00'
## Warning: Coercing text to numeric in C51 / R51C3: '1487.00'
## Warning: Expecting numeric in B52 / R52C2: got '1.548,98'
## Warning: Coercing text to numeric in C52 / R52C3: '1548.98'
## Warning: Expecting numeric in B53 / R53C2: got '1.562,13'
## Warning: Coercing text to numeric in C53 / R53C3: '1562.13'
## Warning: Expecting numeric in B54 / R54C2: got '1.559,52'
## Warning: Coercing text to numeric in C54 / R54C3: '1559.52'
## Warning: Expecting numeric in B55 / R55C2: got '1.577,96'
## Warning: Coercing text to numeric in C55 / R55C3: '1577.96'
## Warning: Expecting numeric in B56 / R56C2: got '1.633,15'
## Warning: Coercing text to numeric in C56 / R56C3: '1633.15'
## Warning: Expecting numeric in B57 / R57C2: got '1.611,92'
## Warning: Coercing text to numeric in C57 / R57C3: '1611.92'
## Warning: Expecting numeric in B58 / R58C2: got '1.662,42'
## Warning: Coercing text to numeric in C58 / R58C3: '1662.42'
## Warning: Expecting numeric in B59 / R59C2: got '1.623,83'
## Warning: Coercing text to numeric in C59 / R59C3: '1623.83'
## Warning: Expecting numeric in B60 / R60C2: got '1.549,61'
## Warning: Coercing text to numeric in C60 / R60C3: '1549.61'
## Warning: Expecting numeric in B61 / R61C2: got '1.123,85'
## Warning: Coercing text to numeric in C61 / R61C3: '1123.85'
## Warning: Expecting numeric in B62 / R62C2: got '1.142,04'
## Warning: Coercing text to numeric in C62 / R62C3: '1142.04'
## Warning: Expecting numeric in B63 / R63C2: got '1.095,84'
## Warning: Coercing text to numeric in C63 / R63C3: '1095.84'
## Warning: Expecting numeric in B64 / R64C2: got '1.111,80'
## Warning: Coercing text to numeric in C64 / R64C3: '1111.80'
## Warning: Expecting numeric in B65 / R65C2: got '1.134,34'
## Warning: Coercing text to numeric in C65 / R65C3: '1134.34'
## Warning: Expecting numeric in B66 / R66C2: got '1.216,03'
## Warning: Coercing text to numeric in C66 / R66C3: '1216.03'
## Warning: Expecting numeric in B67 / R67C2: got '1.171,92'
## Warning: Coercing text to numeric in C67 / R67C3: '1171.92'
## Warning: Expecting numeric in B68 / R68C2: got '1.136,75'
## Warning: Coercing text to numeric in C68 / R68C3: '1136.75'
## Warning: Expecting numeric in B69 / R69C2: got '1.258,00'
## Warning: Coercing text to numeric in C69 / R69C3: '1258.00'
## Warning: Expecting numeric in B70 / R70C2: got '1.437,89'
## Warning: Coercing text to numeric in C70 / R70C3: '1437.89'
## Warning: Expecting numeric in B71 / R71C2: got '1.348,12'
## Warning: Coercing text to numeric in C71 / R71C3: '1348.12'
## Warning: Expecting numeric in B72 / R72C2: got '1.359,48'
## Warning: Coercing text to numeric in C72 / R72C3: '1359.48'
## Warning: Expecting numeric in B73 / R73C2: got '1.316,81'
## Warning: Coercing text to numeric in C73 / R73C3: '1316.81'
## Warning: Expecting numeric in B74 / R74C2: got '1.250,65'
## Warning: Coercing text to numeric in C74 / R74C3: '1250.65'
## Warning: Expecting numeric in B75 / R75C2: got '1.200,90'
## Warning: Coercing text to numeric in C75 / R75C3: '1200.90'
## Warning: Expecting numeric in B76 / R76C2: got '1.248,83'
## Warning: Coercing text to numeric in C76 / R76C3: '1248.83'
## Warning: Expecting numeric in B77 / R77C2: got '1.236,73'
## Warning: Coercing text to numeric in C77 / R77C3: '1236.73'
## Warning: Expecting numeric in B78 / R78C2: got '1.319,96'
## Warning: Coercing text to numeric in C78 / R78C3: '1319.96'
## Warning: Expecting numeric in B79 / R79C2: got '1.361,95'
## Warning: Coercing text to numeric in C79 / R79C3: '1361.95'
## Warning: Expecting numeric in B80 / R80C2: got '1.394,04'
## Warning: Coercing text to numeric in C80 / R80C3: '1394.04'
## Warning: Expecting numeric in B81 / R81C2: got '1.373,84'
## Warning: Coercing text to numeric in C81 / R81C3: '1373.84'
## Warning: Expecting numeric in B82 / R82C2: got '1.410,97'
## Warning: Coercing text to numeric in C82 / R82C3: '1410.97'
## Warning: Expecting numeric in B83 / R83C2: got '1.536,92'
## Warning: Coercing text to numeric in C83 / R83C3: '1536.92'
## Warning: Expecting numeric in B84 / R84C2: got '1.527,75'
## Warning: Coercing text to numeric in C84 / R84C3: '1527.75'
## Warning: Expecting numeric in B85 / R85C2: got '1.616,08'
## Warning: Coercing text to numeric in C85 / R85C3: '1616.08'
## Warning: Expecting numeric in B86 / R86C2: got '1.562,46'
## Warning: Coercing text to numeric in C86 / R86C3: '1562.46'
## Warning: Expecting numeric in B87 / R87C2: got '1.603,23'
## Warning: Coercing text to numeric in C87 / R87C3: '1603.23'
## Warning: Expecting numeric in B88 / R88C2: got '1.322,88'
## Warning: Coercing text to numeric in C88 / R88C3: '1322.88'
## Warning: Expecting numeric in B89 / R89C2: got '1.295,96'
## Warning: Coercing text to numeric in C89 / R89C3: '1295.96'
## Warning: Expecting numeric in B90 / R90C2: got '1.228,33'
## Warning: Coercing text to numeric in C90 / R90C3: '1228.33'
## Warning: Expecting numeric in B91 / R91C2: got '1.128,24'
## Warning: Coercing text to numeric in C91 / R91C3: '1128.24'
## Warning: Expecting numeric in B92 / R92C2: got '1.231,52'
## Warning: Coercing text to numeric in C92 / R92C3: '1231.52'
## Warning: Expecting numeric in B93 / R93C2: got '1.242,63'
## Warning: Coercing text to numeric in C93 / R93C3: '1242.63'
## Warning: Expecting numeric in B94 / R94C2: got '1.286,07'
## Warning: Coercing text to numeric in C94 / R94C3: '1286.07'
## Warning: Expecting numeric in B95 / R95C2: got '1.290,11'
## Warning: Coercing text to numeric in C95 / R95C3: '1290.11'
## Warning: Expecting numeric in B96 / R96C2: got '1.187,56'
## Warning: Coercing text to numeric in C96 / R96C3: '1187.56'
## Warning: Expecting numeric in B97 / R97C2: got '1.157,59'
## Warning: Coercing text to numeric in C97 / R97C3: '1157.59'
## Warning: Expecting numeric in B98 / R98C2: got '1.198,81'
## Warning: Coercing text to numeric in C98 / R98C3: '1198.81'
## Warning: Expecting numeric in B99 / R99C2: got '1.099,03'
## Warning: Coercing text to numeric in C99 / R99C3: '1099.03'
## Warning: Expecting numeric in B100 / R100C2: got '1.133,60'
## Warning: Coercing text to numeric in C100 / R100C3: '1133.60'
## Warning: Expecting numeric in B101 / R101C2: got '1.174,70'
## Warning: Coercing text to numeric in C101 / R101C3: '1174.70'
## Warning: Expecting numeric in B102 / R102C2: got '1.076,12'
## Warning: Coercing text to numeric in C102 / R102C3: '1076.12'
## Warning: Expecting numeric in B103 / R103C2: got '1.121,64'
## Warning: Coercing text to numeric in C103 / R103C3: '1121.64'
## Warning: Expecting numeric in B104 / R104C2: got '1.088,15'
## Warning: Coercing text to numeric in C104 / R104C3: '1088.15'
## Warning: Expecting numeric in B105 / R105C2: got '1.146,87'
## Warning: Coercing text to numeric in C105 / R105C3: '1146.87'
## Warning: Expecting numeric in B106 / R106C2: got '1.195,20'
## Warning: Coercing text to numeric in C106 / R106C3: '1195.20'
## Warning: Expecting numeric in B107 / R107C2: got '1.276,55'
## Warning: Coercing text to numeric in C107 / R107C3: '1276.55'
## Warning: Expecting numeric in B108 / R108C2: got '1.274,08'
## Warning: Coercing text to numeric in C108 / R108C3: '1274.08'
## Warning: Expecting numeric in B109 / R109C2: got '1.332,76'
## Warning: Coercing text to numeric in C109 / R109C3: '1332.76'
## Warning: Expecting numeric in B110 / R110C2: got '1.368,46'
## Warning: Coercing text to numeric in C110 / R110C3: '1368.46'
## Warning: Expecting numeric in B111 / R111C2: got '1.399,72'
## Warning: Coercing text to numeric in C111 / R111C3: '1399.72'
## Warning: Expecting numeric in B112 / R112C2: got '1.382,29'
## Warning: Coercing text to numeric in C112 / R112C3: '1382.29'
## Warning: Expecting numeric in B113 / R113C2: got '1.348,69'
## Warning: Coercing text to numeric in C113 / R113C3: '1348.69'
## Warning: Expecting numeric in B114 / R114C2: got '1.339,79'
## Warning: Coercing text to numeric in C114 / R114C3: '1339.79'
## Warning: Expecting numeric in B115 / R115C2: got '1.307,59'
## Warning: Coercing text to numeric in C115 / R115C3: '1307.59'
## Warning: Expecting numeric in B116 / R116C2: got '1.358,46'
## Warning: Coercing text to numeric in C116 / R116C3: '1358.46'
## Warning: Expecting numeric in B117 / R117C2: got '1.392,13'
## Warning: Coercing text to numeric in C117 / R117C3: '1392.13'
## Warning: Expecting numeric in B118 / R118C2: got '1.379,58'
## Warning: Coercing text to numeric in C118 / R118C3: '1379.58'
## Warning: Expecting numeric in B119 / R119C2: got '1.521,74'
## Warning: Coercing text to numeric in C119 / R119C3: '1521.74'
## Warning: Expecting numeric in B120 / R120C2: got '1.607,57'
## Warning: Coercing text to numeric in C120 / R120C3: '1607.57'
## Warning: Expecting numeric in B121 / R121C2: got '1.619,38'
## Warning: Coercing text to numeric in C121 / R121C3: '1619.38'
View(data_col)
# Convertir/declarar variable 1=ENER en serie de tiempo mensual
variable1_ts <- ts(data_col$COLCAP, start = c(2015, 1), frequency = 12)
variable1_ts
##          Jan     Feb     Mar     Apr     May     Jun     Jul     Aug     Sep
## 2015 1396.35 1306.62 1331.35 1317.24 1246.59 1218.82 1218.13 1114.36 1153.71
## 2016 1342.42 1292.51 1313.18 1308.22 1380.18 1338.83 1365.53 1286.07 1351.68
## 2017 1371.54 1439.48 1462.90 1481.37 1482.27 1487.52 1424.58 1445.23 1513.65
## 2018 1565.56 1546.71 1577.01 1526.60 1542.77 1506.07 1392.18 1379.24 1325.93
## 2019 1573.64 1487.00 1548.98 1562.13 1559.52 1577.96 1633.15 1611.92 1662.42
## 2020 1142.04 1095.84 1111.80 1134.34 1216.03 1171.92 1136.75 1258.00 1437.89
## 2021 1250.65 1200.90 1248.83 1236.73 1319.96 1361.95 1394.04 1373.84 1410.97
## 2022 1562.46 1603.23 1322.88 1295.96 1228.33 1128.24 1231.52 1242.63 1286.07
## 2023 1198.81 1099.03 1133.60 1174.70 1076.12 1121.64 1088.15 1146.87 1195.20
## 2024 1368.46 1399.72 1382.29 1348.69 1339.79 1307.59 1358.46 1392.13 1379.58
##          Oct     Nov     Dec
## 2015 1175.09 1244.17 1336.27
## 2016 1357.47 1326.31 1365.61
## 2017 1558.18 1478.33 1455.52
## 2018 1447.01 1508.27 1587.74
## 2019 1623.83 1549.61 1123.85
## 2020 1348.12 1359.48 1316.81
## 2021 1536.92 1527.75 1616.08
## 2022 1290.11 1187.56 1157.59
## 2023 1276.55 1274.08 1332.76
## 2024 1521.74 1607.57 1619.38
library(ggplot2)
library(plotly)

# Convertir la serie temporal a un vector numérico para lograr graficar con ggplot2
data_col$variable1 <- as.numeric(variable1_ts)

# Crear el gráfico
grafico_serie <- ggplot(data_col, aes(x = seq.Date(from = as.Date("2015-01-01"), by = "month", length.out = nrow(data_col)), 
                                      y = variable1)) +
  geom_line(color = "grey", linewidth = 0.4) +  # Cambiado 'size' por 'linewidth'
  geom_point(color = "black", size = 0.1) +
  ggtitle("Variable 1: Serie original") +
  xlab("Tiempo") +
  ylab("Unidad Variable 1") +
  theme_minimal()

ggplotly(grafico_serie)
# Cargar librerías necesarias
library(ggplot2)
library(plotly)

# Descomposición de la serie temporal
stl_decomp_var1 <- stl(variable1_ts, s.window = "periodic")

# Convertir la descomposición a un data frame para graficar con ggplot2
stl_df_var1 <- data.frame(
  Time = rep(time(variable1_ts), 4),  # Tiempo repetido para cada componente (son 4 componentes)
  Value = c(stl_decomp_var1$time.series[, "seasonal"], 
            stl_decomp_var1$time.series[, "trend"], 
            stl_decomp_var1$time.series[, "remainder"], 
            variable1_ts),
  Component = rep(c("Estacional", "Tendencia", "Residuo", "Serie Original"), each = length(variable1_ts))
)

# Crear gráfico con ggplot2
p <- ggplot(stl_df_var1, aes(x = Time, y = Value, color = Component)) +
  geom_line() +
  facet_wrap(~Component, scales = "free_y", ncol = 1) + 
  theme_minimal() +
  labs(title = "Descomposición temporal de la variable 1",
       x = "Tiempo",
       y = "Valor")

# Convertir a gráfico interactivo con plotly
ggplotly(p)
# Extraer los componentes de la descomposición
variable1_sa <- variable1_ts - stl_decomp_var1$time.series[, "seasonal"]
# Crear vector de fechas correctamente alineado con la serie
fechas_var1 <- seq.Date(from = as.Date("2015-01-01"), by = "month", length.out = length(variable1_ts))

# Gráfico mejorado con fechas en el eje X
grafico_ajustada_var1 <- ggplot() +
  geom_line(aes(x = fechas_var1, y = variable1_ts), color = "grey", size = 0.5, linetype = "solid", name = "Serie Original") +
  geom_line(aes(x = fechas_var1, y = variable1_sa), color = "black", size = 0.6, linetype = "solid", name = "Serie Ajustada") +
  ggtitle("Variable 1:Serie Original vs Serie Ajustada por Estacionalidad") +
  xlab("Tiempo") +
  ylab("Unidad de medida variable 1") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) # Rotar etiquetas para mejor visualización
## 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.
## Warning in geom_line(aes(x = fechas_var1, y = variable1_ts), color = "grey", :
## Ignoring unknown parameters: `name`
## Warning in geom_line(aes(x = fechas_var1, y = variable1_sa), color = "black", :
## Ignoring unknown parameters: `name`
# Convertir a gráfico interactivo
ggplotly(grafico_ajustada_var1)
## Don't know how to automatically pick scale for object of type <ts>. Defaulting
## to continuous.
library(ggplot2)
library(plotly)

# Convertir la serie a un vector numérico
variable1_vec <- as.numeric(variable1_ts)
tendencia_var1 <- as.numeric(stl_decomp_var1$time.series[, "trend"])

# Asegurar que 'fechas' tenga la misma longitud
fechas <- seq.Date(from = as.Date("2015-01-01"), by = "month", length.out = length(variable1_ts))

# Gráfico interactivo de la serie original vs tendencia
grafico_tendencia_var1 <- ggplot() +
  geom_line(aes(x = fechas, y = variable1_vec, color = "Serie Original"), size = 0.7, linetype = "solid") +
  geom_line(aes(x = fechas, y = tendencia_var1, color = "Tendencia"), size = 0.8, linetype = "solid") +
  scale_color_manual(values = c("Serie Original" = "grey", "Tendencia" = "black")) +
  ggtitle("Variable 1: Serie Original vs Tendencia") +
  xlab("Tiempo") +
  ylab("Unidad de medida Variable 1") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) # Rotar etiquetas del eje X

# Convertir a gráfico interactivo con plotly
ggplotly(grafico_tendencia_var1)
#Cálculo de la tasa de crecimiento anual correctamente alineada
tasa_crecimiento_var1 <- (variable1_ts[(13:length(variable1_ts))] / variable1_ts[1:(length(variable1_ts) - 12)] - 1) * 100
tasa_tendencia_var1 <- (tendencia_var1[(13:length(tendencia_var1))] / tendencia_var1[1:(length(tendencia_var1) - 12)] - 1) * 100

# Crear vector de fechas corregido, es decir que inicie desde enero 2013
fechas_corregidas_var1 <- seq(from = as.Date("2016-01-01"), by = "month", length.out = length(tasa_crecimiento_var1))

# Verificar longitudes
print(length(fechas_corregidas_var1))
## [1] 108
print(length(tasa_crecimiento_var1))
## [1] 108
print(length(tasa_tendencia_var1))
## [1] 108
library(ggplot2)
library(plotly)

# Gráfico de la tasa de crecimiento anual variable 1
grafico_crecimiento_var1 <- ggplot() +
  geom_line(aes(x = fechas_corregidas_var1, y = tasa_crecimiento_var1), color = "grey", size = 0.7) +
  geom_line(aes(x = fechas_corregidas_var1, y = tasa_tendencia_var1), color = "black", size = 0.8, linetype = "dashed") +
  ggtitle("Variable1: Tasa de crecimiento anual % de la serie Original y la tendencia") +
  xlab("Tiempo") +
  ylab("% de Crecimiento Anual") +
  theme_minimal()

# Convertir a gráfico interactivo
ggplotly(grafico_crecimiento_var1)