In this chapter, we present the sources and data formatting of the macroeconomic variables.
We use data from the Central Reserve Bank of Peru. The Consumer Price Index (CPI) data are obtained using the token PN01270PM, the Food Consumer Price Index (CPIA) data with token PN01336PM, the Real Exchange Rate (RER) data with token PN01259PM, Exports data with token PN01461BM, the GDP data with token PN01773AM, the agricultural GDP data with token PN01755AM, the interbank interest rate data with token PN07819NM, and PN02079AM for the manufacturing production.
In order to address any underlying trends in the GDP data, we express this variable in percentage deviations from the Hodrick-Prescott trend. To that end, we define a helper that returns the estimated trend:
#' Applies HP filter for monthly data and returns the trend component#' #' @param x vector of monthly observations#' @param freq smoothing parameter (\lambda)hp_filter_trend <-function(x, freq =14400) { res_hp_filter <- mFilter::hpfilter( x,freq =14400, type ="lambda", drift =FALSE )as.vector(res_hp_filter$trend)}
Furthermore, to eliminate any seasonal components present in the data, we employ the X13 method developed by the Census Bureau. This method enables us to remove seasonal variations and isolate the underlying patterns and dynamics of the variables
#' Removes the seasonality of a vector of monthly values#'#' @param x vector of numerical values#' @param start_date start date of the values#' @param remove_trend should the estimated trend be removed? Default to ``FALSEadj_season_X13 <-function(x, start_date,remove_trend =FALSE) { x_ts <-ts(x, start =c(year(start_date), month(start_date)), freq =12)# Seasonal Adjustment with X-13ARIMA-SEATS x_ts_season <- seasonal::seas( x_ts, estimate.maxiter=10000,arima.model ="(0 1 1)(0 1 1)",x11 ="" ) x_ts_season_df <- timetk::tk_tbl(x_ts_season$data) |>mutate(date = lubridate::my(index)) df_resul <-tibble(date =seq(start_date, last(x_ts_season_df$date), by ="month") ) |>left_join(x_ts_season_df, by ="date") |>mutate(val = seasonaladj)if (remove_trend) { df_resul <- df_resul |>mutate(val = seasonaladj - trend) } df_resul |>pull(val)}
Note
The hp_filter_trend() and the adj_season_X13() functions are defined in the ../weatherperu/R/detrending.R script.
For the international commodity prices, we will not include those in the final macro data, as they are given depending on the crop. We will produce a separate file. We have downloaded these series from the IMF Primary Commodity Price System website.