Convert Rolling Quarters to Exact Monthly Series
Source:R/mensalize-sidra-series.R
mensalize_sidra_series.RdTransforms SIDRA rolling quarterly averages into exact monthly values using the mathematical relationship between consecutive rolling quarters.
Usage
mensalize_sidra_series(
rolling_quarters,
starting_points = NULL,
series = "all",
compute_derived = TRUE,
verbose = TRUE
)Arguments
- rolling_quarters
data.table from
fetch_sidra_rolling_quarterscontaining rolling quarter series with columnsanomesfinaltrimmovel,mesnotrim, and series value columns.- starting_points
Optional data.table with precomputed starting points (y0 values). If NULL (default), uses bundled
pnadc_series_starting_points. See Details for format.- series
Character vector of series names to mensalize, or "all" (default) for all series in the input data (except price indices).
- compute_derived
Logical. Compute derived series (rates, aggregates)? Default TRUE.
- verbose
Logical. Print progress messages? Default TRUE.
Value
A data.table with columns:
- anomesexato
Integer. YYYYMM exact month
- m_*
Numeric. Mensalized value for each series (one column per series)
Details
The algorithm exploits the mathematical property of rolling quarterly averages:
$$RQ_t - RQ_{t-1} = (Month_t - Month_{t-3}) / 3$$
This means exact 3-month variations can be extracted from consecutive rolling quarters. By accumulating these variations separately for each month-position (1, 2, or 3), we build cumulative variation series. The only unknown is the starting level for Jan, Feb, and Mar 2012.
Starting points are estimated by:
Computing monthly estimates from calibrated microdata (z_ variables)
Calculating cumulative variations from SIDRA (cum_ variables)
Backprojecting: e0 = z_ - cum_ over calibration period (2013-2019)
Averaging e0 by month position to get y0_ for each position
Each of the three month-position sub-series accumulates strictly independently from its own starting point. As a consequence, adding a new rolling quarter never alters previously computed monthly values.
Starting Points Format
If providing custom starting points, the data.table must have columns:
series_name: Character. Series name matching rolling_quarters columnsmesnotrim: Integer (1, 2, or 3). Month position in quartery0: Numeric. Starting point value
Mathematical Foundation
The mensalization algorithm proceeds in steps:
Calculate d3 = 3 * (RQ_t - RQ_t-1)
Separate d3 by month position: d3m1, d3m2, d3m3
Cumulate separately: cum1, cum2, cum3
Apply starting points: m = y0 + cum (final monthly value)
The result satisfies the d3 identity \(m_t - m_{t-3} = 3 (rq_t - rq_{t-1})\) for every same-mesnotrim pair. Trailing positions where rq is missing are masked to NA so that incomplete trios are not published.
See also
fetch_sidra_rolling_quarters to obtain input data
compute_series_starting_points for custom calibration
Examples
# \donttest{
rq <- fetch_sidra_rolling_quarters(
series = c("taxadesocup", "popocup", "popdesocup")
)
#> Fetching 3 series from SIDRA API...
#> This may take a few minutes on first run.
#> [ 33%] Fetching popocup...
#> Failed to fetch popocup: Timeout was reached [servicodados.ibge.gov.br]:
#> Failed to connect to servicodados.ibge.gov.br port 443 after 20002 ms: Timeout was reached
#> [ 67%] Fetching popdesocup...
#> Failed to fetch popdesocup: Timeout was reached [servicodados.ibge.gov.br]:
#> Failed to connect to servicodados.ibge.gov.br port 443 after 20003 ms: Timeout was reached
#> [100%] Fetching taxadesocup...
#> Failed to fetch taxadesocup: Timeout was reached [servicodados.ibge.gov.br]:
#> Failed to connect to servicodados.ibge.gov.br port 443 after 20002 ms: Timeout was reached
#> fetch_sidra_rolling_quarters: no series could be fetched from the SIDRA API. Check internet connection or try again later.
monthly <- mensalize_sidra_series(rq)
#> Error in mensalize_sidra_series(rq): Missing required columns: anomesfinaltrimmovel, mesnotrim
head(monthly)
#> Error: object 'monthly' not found
# }