Filtro HP

Datos

library(mFilter)
library(dplyr)
library(tidyr)
library(stats)

library(readxl)
datos_PIB_trim_CA <- read_excel("C:/Users/Fatima/Downloads/datos_PIB_trim_CA.xlsx", 
    skip = 4)
datos_PIB  <- datos_PIB_trim_CA  %>% 
  filter(!is.na(Fechas)) %>% 
  mutate(across(-Fechas, ~na_if(.x, "--"))) %>% 
  mutate(across(-Fechas, as.numeric))

Aplicación a cada país de centroamerica

Costa Rica

Implementación personalizada

# Serie Temporal (trimestral)
PIB_CR <- datos_PIB[[2]][5:138] %>% 
  as.numeric() %>% 
  ts(start = c(1991,1),frequency = 4) 

# Aplicar el filtro Hodrick-Prescott
hp_cr<- hpfilter(PIB_CR, freq = 1600)

# Extrae la tendencia y el componente cíclico
trend_cr <- hp_cr$trend
cycle_cr <- hp_cr$cycle

Resultados Costa Rica

options(scipen = 9999)
plot(PIB_CR, type = "l", main = "Filtro de Hodrick-Prescott - Costa Rica (1991-I - 2024-II)", ylab = "PIB", xlab = "Tiempo")
lines(trend_cr, col = "blue", lwd = 2)
legend("topleft", legend = c("PIB Original", "Tendencia (Filtro HP)"), col = c("black", "yellow"), lty = 1, lwd = 2)

plot(cycle_cr, type = "l", main = "Componente Ciclico - Costa Rica", ylab = "Ciclo", xlab = "Tiempo")

Implementación rápida

plot(hp_cr,ask = FALSE)

El Salvador

Implementación personalizada

# Serie Temporal (trimestral)
PIB_SV <- datos_PIB[[3]]%>% 
  as.numeric() %>% 
  ts(start = c(1990,1),frequency = 4) 

# Aplicar el filtro Hodrick-Prescott
hp_sv<- hpfilter(PIB_SV, freq = 1600)

# Extrae la tendencia y el componente cíclico
trend_sv <- hp_sv$trend
cycle_sv <- hp_sv$cycle

Resultados El Salvador

# Visualiza los resultados
options(scipen = 9999)
plot(PIB_SV, type = "l", main = "Filtro de Hodrick-Prescott - El Salvador (1990-I - 2024-II)", ylab = "PIB", xlab = "Tiempo")
lines(trend_sv, col = "blue", lwd = 2)
legend("topleft", legend = c("PIB Original", "Tendencia (Filtro HP)"), col = c("black", "yellow"), lty = 1, lwd = 2)

plot(cycle_sv, type = "l", main = "Componente Ciclico - El Salvador", ylab = "Ciclo", xlab = "Tiempo")

Implementación rápida

plot(hp_sv,ask = FALSE)

Guatemala

Implementación personalizada

# Serie Temporal (trimestral)
PIB_GM <- datos_PIB[[4]][45:138] %>% 
  as.numeric() %>% 
  ts(start = c(2001,1),frequency = 4) 

# Aplicar el filtro Hodrick-Prescott
hp_gm<- hpfilter(PIB_GM, freq = 1600)

# Extrae la tendencia y el componente cíclico
trend_gm <- hp_gm$trend
cycle_gm <- hp_gm$cycle

Resultados Guatemala

options(scipen = 9999)
plot(PIB_GM, type = "l", main = "Filtro de Hodrick-Prescott - Guatemala (2001-I - 2024-II)", ylab = "PIB", xlab = "Tiempo")
lines(trend_gm, col = "blue", lwd = 2)
legend("topleft", legend = c("PIB Original", "Tendencia (Filtro HP)"), col = c("black", "yellow"), lty = 1, lwd = 2)

plot(cycle_gm, type = "l", main = "Componente Ciclico - Guatemala", ylab = "Ciclo", xlab = "Tiempo")

Implementación rápida

plot(hp_gm,ask = FALSE)

Honduras

Implementación personalizada

# Serie Temporal (trimestral)
PIB_HD <- datos_PIB[[5]][41:138] %>% 
  as.numeric() %>% 
  ts(start = c(2000,1),frequency = 4) 

# Aplicar el filtro 
hp_hd<- hpfilter(PIB_HD, freq = 1600)

# Extrae la tendencia y el componente cíclico
trend_hd <- hp_hd$trend
cycle_hd <- hp_hd$cycle

Resultados Honduras

options(scipen = 9999)
plot(PIB_HD, type = "l", main = "Filtro de Hodrick-Prescott -Honduras (2000-I - 2024-II)", ylab = "PIB", xlab = "Tiempo")
lines(trend_hd, col = "blue", lwd = 2)
legend("topleft", legend = c("PIB Original", "Tendencia (Filtro HP)"), col = c("black", "yellow"), lty = 1, lwd = 2)

plot(cycle_hd, type = "l", main = "Componente Ciclico - Honduras", ylab = "Ciclo", xlab = "Tiempo")

Implementación rápida

plot(hp_hd,ask = FALSE)

Nicaragua

Implementación personalizada

# Serie Temporal (trimestral)
PIB_NC <- datos_PIB[[6]][65:138] %>% 
  as.numeric() %>% 
  ts(start = c(2006,1),frequency = 4) 

# Aplicar el filtro Hodrick-Prescott
hp_nc<- hpfilter(PIB_NC, freq = 1600)

# Extrae la tendencia y el componente cíclico
trend_nic <- hp_nc$trend
cycle_nic <- hp_nc$cycle

Resultados Nicaragua

options(scipen = 9999)
plot(PIB_NC, type = "l", main = "Filtro de Hodrick-Prescott - Nicaragua (2006-I - 2024-II)", ylab = "PIB", xlab = "Tiempo")
lines(trend_nic, col = "blue", lwd = 2)
legend("topleft", legend = c("PIB Original", "Tendencia (Filtro HP)"), col = c("black", "orange"), lty = 1, lwd = 2)

plot(cycle_nic, type = "l", main = "Componente Ciclico - Nicaragua", ylab = "Ciclo", xlab = "Tiempo")

Implementación rápida

plot(hp_nc,ask = FALSE)

Panamá

Implementación personalizada

# Serie Temporal (trimestral)
PIB_PAN <- datos_PIB[[8]][25:131] %>% 
  as.numeric() %>% 
  ts(start = c(1996,1), end =c(2022,3),frequency = 4) 

# Aplicar el filtro Hodrick-Prescott
hp_pan<- hpfilter(PIB_PAN, freq = 1600)

# Extrae la tendencia y el componente cíclico
trend_pan<- hp_pan$trend
cycle_pan <- hp_pan$cycle

Resultados Panamá

options(scipen = 9999)
plot(PIB_PAN, type = "l", main = "Filtro de Hodrick-Prescott - Panamá (1996-I - 2022-III)", ylab = "PIB", xlab = "Tiempo")
lines(trend_pan, col = "blue", lwd = 2)
legend("topleft", legend = c("PIB Original", "Tendencia (Filtro HP)"), col = c("black", "yellow"), lty = 1, lwd = 2)

plot(cycle_pan, type = "l", main = "Componente Ciclico - Panamá", ylab = "Ciclo", xlab = "Tiempo")

Implementación rápida

plot(hp_pan,ask = FALSE)