# Cargar librerías necesarias
library(readxl)
library(ggplot2)
library(forecast)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.1 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ lubridate 1.9.2 ✔ tibble 3.2.1
## ✔ purrr 1.0.1 ✔ tidyr 1.3.0
## ── 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
# Cargar datos
data <- read_excel("C:/Users/julie/OneDrive - PUJ Cali/Clase Predicción Econ/2025-1/Clases/Módulo III y IV/PACCIONES COL.xlsx")
## New names:
## • `` -> `...1`
# Crear la serie temporal de CHO
cho_ts <- ts(data$CHO, start = c(2010, 1), frequency = 12)
# Descomposición de la serie temporal
cho_decom <- decompose(cho_ts, type = "multiplicative")
# Extraer los componentes
trend <- cho_decom$trend
seasonal <- cho_decom$seasonal
random <- cho_decom$random
adj_seasonal <- cho_ts / seasonal
# Función para graficar con etiquetas de los últimos 12 meses
grafica_componentes <- function(serie, titulo) {
df <- data.frame(Fecha = seq(as.Date("2010-01-01"), by = "month", length.out = length(serie)),
Valor = serie)
df <- df %>% drop_na()
ultimos_12 <- tail(df, 12)
ggplot(df, aes(x = Fecha, y = Valor)) +
geom_line(color = "blue") +
geom_point(data = ultimos_12, aes(x = Fecha, y = Valor), color = "red") +
geom_text(data = ultimos_12, aes(label = round(Valor, 2)), vjust = -1, size = 3) +
labs(title = titulo, x = "Fecha", y = "Valor") +
theme_minimal()
}
Descomposición del precio mensual de cierre de la acción
NUTRESA(CHO)
# Graficar componentes individuales
grafica_componentes(trend, "Tendencia de CHO")

grafica_componentes(seasonal, "Estacionalidad de CHO")

grafica_componentes(random, "Componente Irregular de CHO")

grafica_componentes(adj_seasonal, "Serie Ajustada por Estacionalidad")

# Graficar serie original vs ajustada por estacionalidad desde 2019
ajustada_df <- data.frame(Fecha = seq(as.Date("2010-01-01"), by = "month", length.out = length(adj_seasonal)),
Original = cho_ts,
Ajustada = adj_seasonal) %>%
filter(Fecha >= as.Date("2019-01-01"))
ggplot(ajustada_df, aes(x = Fecha)) +
geom_line(aes(y = Original, color = "Original")) +
geom_line(aes(y = Ajustada, color = "Ajustada por Estacionalidad")) +
geom_text(aes(y = Original, label = round(Original, 2)), vjust = -1, size = 3, data = tail(ajustada_df, 12)) +
labs(title = "Serie Original CHO vs Ajustada por Estacionalidad", x = "Fecha", y = "Valor") +
theme_minimal()

# Calcular tasa de crecimiento anual de la tendencia
trend_growth <- 100 * (diff(trend, lag = 12) / stats::lag(trend, 12))
trend_growth_df <- data.frame(Fecha = seq(as.Date("2011-01-01"), by = "month", length.out = length(trend_growth)),
Crecimiento = trend_growth)
trend_growth_df <- trend_growth_df %>% drop_na() %>% filter(Fecha >= as.Date("2019-01-01"))
# Graficar serie original vs tendencia en tasa de crecimiento anual
ggplot(trend_growth_df, aes(x = Fecha, y = Crecimiento)) +
geom_line(color = "blue") +
geom_text(aes(label = round(Crecimiento, 2)), vjust = -1, size = 3, data = tail(trend_growth_df, 12)) +
labs(title = "Crecimiento Anual de la Tendencia de CHO", x = "Fecha", y = "Crecimiento (%)") +
theme_minimal()

# Calcular tasa de crecimiento anual de la serie original
original_growth <- 100 * (diff(cho_ts, lag = 12) / stats::lag(cho_ts, 12))
original_growth_df <- data.frame(Fecha = seq(as.Date("2011-01-01"), by = "month", length.out = length(original_growth)),
Crecimiento_Original = original_growth,
Crecimiento_Tendencia = trend_growth)
original_growth_df <- original_growth_df %>% drop_na() %>% filter(Fecha >= as.Date("2019-01-01"))
# Graficar serie original en tasa de crecimiento vs tendencia ciclo en tasa de crecimiento
ggplot(original_growth_df, aes(x = Fecha)) +
geom_line(aes(y = Crecimiento_Original, color = "Crecimiento Original")) +
geom_line(aes(y = Crecimiento_Tendencia, color = "Crecimiento Tendencia")) +
geom_text(aes(y = Crecimiento_Original, label = round(Crecimiento_Original, 2)), vjust = -1, size = 3, data = tail(original_growth_df, 12)) +
labs(title = "Crecimiento Anual de la Serie Original vs Tendencia", x = "Fecha", y = "Crecimiento (%)") +
theme_minimal()
