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
Final adjustment ensures the average of 3 consecutive mensalized values equals the original rolling quarter value.
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: y = y0 + cum
Final adjustment for rolling quarter consistency
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...
#> [ 67%] Fetching popdesocup...
#> [100%] Fetching taxadesocup...
#> Successfully fetched 3/3 series
#> Rolling quarters: 201203 to 202603 (169 observations)
monthly <- mensalize_sidra_series(rq)
#> Using bundled starting points
#> Mensalizing 2 series...
#> Computing derived series...
#> Computed aggregate: m_popnaforca
#> Computed rate: m_taxadesocup
#> Mensalization complete: 169 months (201203 to 202603)
head(monthly)
#> anomesexato m_popocup m_popdesocup m_popnaforca m_taxadesocup
#> <int> <num> <num> <num> <num>
#> 1: 201203 88538.06 7375.089 95913.15 7.7
#> 2: 201204 88197.81 7696.149 95893.95 8.0
#> 3: 201205 89066.75 7342.311 96409.06 7.6
#> 4: 201206 90689.45 6918.540 97607.99 7.1
#> 5: 201207 88578.81 7474.149 96052.95 7.8
#> 6: 201208 89927.75 6955.311 96883.06 7.2
# }