Am I correctly removing trend and seasonality using X-13 in R?
Posted by HairRadiant2114@reddit | learnprogramming | View on Reddit | 2 comments
Hi, I’m working with quarterly macroeconomic data and trying to remove both trend and seasonality using X-13 in R.
Here is my code:
rm(list = ls())
library(seasonal)
library(dplyr)
library(zoo)
dados <- read.table(
file.choose(),
header = TRUE,
sep = "\t",
dec = "."
)
dados <- dados %>% arrange(ano, tri)
n <- nrow(dados)
vars <- c(
"ibc", "ipca", "inpc", "selic", "desocupacao",
"remuneracao_real", "cambio_real",
"commodities_real", "rndb",
"otimo_bom", "regular", "ruim_pessimo"
)
x13_ciclo_safe <- function(x, nome) {
if (any(is.na(x))) {
x <- na.approx(x, na.rm = FALSE)
}
ts_data <- ts(
x,
start = c(dados$ano[1], dados$tri[1]),
frequency = 4
)
ajuste <- try(
seas(ts_data),
silent = TRUE
)
if (inherits(ajuste, "try-error")) {
return(rep(NA, n))
}
dessaz <- final(ajuste)
trend <- trend(ajuste)
ciclo <- dessaz - trend
return(as.numeric(ciclo))
}
base_final <- dados[, c("ano", "tri")]
for (v in vars) {
base_final[[paste(v, "ciclo", sep = "_")]] <-
x13_ciclo_safe(dados[[v]], v)
}
write.csv2(base_final, "base_ciclo_x13.csv", row.names = FALSE)
My understanding is:
- final() removes seasonality
- trend() extracts the trend
so ciclo = final - trend should give me the irregular (cycle) component
My question:
Is this the correct way to remove both trend and seasonality using X-13?
Any feedback on methodology would be really appreciated.
Temporary_Gur8335@reddit
dude your approch looks solid but you might be missing something in the decomposition. X-13 actually breaks things down into trend-cycle, seasonal, and irregular components - not just trend and seasonal seperately
when you do `final()` you're getting the seasonally adjusted series (original minus seasonal), and `trend()` gives you the trend-cycle component. so when you subtract trend from final, you're getting the irregular component, not the full cycle
if you want the actual cyclical component, you'd need to extract it differently since X-13 bundles trend and cycle together by default. might want to look into using the `series()` function to pull specific components or adjust your decomposition method
what exactly are you trying to analyze with the cycle data? that might help figure out if this approach fits what you need
HairRadiant2114@reddit (OP)
Thanks, that makes sense - I see your point about the decomposition.
In my case, the goal is not to extract a “pure” business cycle, but to remove both trend and seasonality before estimating a VAR with multiple macro variables.
Given the instability I was getting (autocorrelation, non-normality), I was instructed to work with series that only capture short-run fluctuations.
So even if final() - trend() gives the irregular component rather than a textbook cycle, that’s actually aligned with what I need - i.e., a series without trend and seasonal effects.
Does that sound reasonable in this context? Is there a better/safer way to do it?