Skip to content

Instantly share code, notes, and snippets.

@sientifiko
Created November 5, 2024 23:57
Show Gist options
  • Select an option

  • Save sientifiko/85e62bb843f2bda43e7aa6b6f6d922d0 to your computer and use it in GitHub Desktop.

Select an option

Save sientifiko/85e62bb843f2bda43e7aa6b6f6d922d0 to your computer and use it in GitHub Desktop.
Script para obtener la proyección de crecimiento tendencial (asumiendo linealidad) a partir del PIB y el IMACEC
# "pib trim.xlsx" es el PIB trimestral a precios encadenados de este enlace:
#https://si3.bcentral.cl/Siete/ES/Siete/Cuadro/CAP_CCNN/MN_CCNN76/CCNN2018_P0_V2/637801082315858005?cbFechaInicio=2000&cbFechaTermino=2024&cbFrecuencia=QUARTERLY&cbCalculo=NONE&cbFechaBase=
pibtrim <- readxl::read_excel("pib trim.xlsx",skip = 1)
colnames(pibtrim) <- c("time", "pib")
pibtrim <- pibtrim %>%
mutate(time2 = ymd(str_sub(time, 1, 10))) %>%
mutate(rate = log(pib/lag(pib)),
anio = year(time2))
avg2018 <- pibtrim %>%
filter(anio == 2018) %>%
pull(pib) %>%
mean()
pibtrim$pib18 <- pibtrim$pib/avg2018
# "imacec.xlsx" IMACEC es el mensual desestacionalizado de este enlace:
#https://si3.bcentral.cl/Siete/ES/Siete/Cuadro/CAP_CCNN/MN_CCNN76/CCNN2018_IMACEC_03_A/638131831615238879?cbFechaInicio=2000&cbFechaTermino=2024&cbFrecuencia=MONTHLY&cbCalculo=NONE&cbFechaBase=
imacec <- readxl::read_excel("imacec.xlsx", skip = 1)
colnames(imacec) <- c("time", "imacec_index")
imacec <- imacec %>%
mutate(time2 = ymd(str_sub(time, 1, 10))) %>%
mutate(trim = quarter(time2),
anio = year(time2))
imactrim <- imacec %>%
group_by(anio, trim) %>%
summarise(time = max(time2),
avgimacec = mean(imacec_index)) %>%
as.data.frame()
# PIB POTENCIAL
y1pib <- first(pibtrim$pib18[pibtrim$time2>= as.Date("2015-01-01")])
temp <- pibtrim %>%
filter(time2 >= as.Date("2015-01-01"),
time2 <= as.Date("2019-01-01")) %>%
mutate(corre = row_number())
m_pib <- as.numeric(coef(lm(pib18~corre, data = temp))[2])
serie1 <- pibtrim %>%
mutate(corte = ifelse(time2 < as.Date("2015-01-01"),
0, 1)) %>%
mutate(periodo = ifelse(
time2 < as.Date("2015-01-01"),
corte,
cumsum(corte)
)) %>%
mutate(pibproyectado = ifelse(
time2 < as.Date("2015-01-01"),
pib18,
(m_pib*periodo)+y1pib
))
serie1 %>%
ggplot() +
aes(x=time2) +
geom_line(aes(y = pibproyectado*100, color = "Proyectado"),
linewidth = 1, linetype = "dashed") +
geom_line(aes(y = pib18*100, color = "PIB"),
linewidth = 1.2) +
scale_x_date(breaks = "1 year",
expand = c(0,0)) +
theme(axis.text.x = element_text(angle = 90, vjust = .5),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
legend.title = element_blank(),
legend.position.inside = c(.2, .8)) +
labs(title = "PIB trimestral",
subtitle = "2018 = 100")
# IMACEC POTENCIAL
temp2 <- imacec %>%
filter(time2 >= as.Date("2015-01-01"),
time2 <= as.Date("2019-01-01")) %>%
mutate(corre = row_number())
y1ima <- first(imacec$imacec_index[imacec$time2>= as.Date("2015-01-01")])
m_ima <- as.numeric(coef(lm(imacec_index~corre, data = temp2))[2])
serie2 <- imacec %>%
mutate(corte = ifelse(time2 < as.Date("2015-01-01"),
0, 1)) %>%
mutate(periodo = ifelse(
time2 < as.Date("2015-01-01"),
corte,
cumsum(corte)
)) %>%
mutate(imacecproyectado = ifelse(
time2 < as.Date("2015-01-01"),
imacec_index,
(m_ima*periodo)+y1ima
))
serie2 %>%
as.data.frame() %>%
ggplot() +
aes(x=time2) +
geom_line(aes(y = imacecproyectado, color = "Proyectado"),
linewidth = 1, linetype = "dashed") +
geom_line(aes(y = imacec_index, color = "IMACEC"),
linewidth = 1.2) +
scale_x_date(breaks = "1 year",
expand = c(0,0)) +
theme(axis.text.x = element_text(angle = 90, vjust = .5),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
legend.title = element_blank(),
legend.position = c(.2, .8)) +
labs(title = "IMACEC mensual",
subtitle = "Serie desestacionalizada")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment