This arithmetic smoothing technique aims to eliminate irregularities of the population pyramid by averaging values in a moving window of user-defined width.

mav(Value, Age, n = 3, OAG = TRUE, tails = FALSE)

## Arguments

Value numeric. A vector of demographic counts in single age groups. integer. A vector of ages corresponding to the lower integer bound of the counts. integer. A single number, (often 3 or 5), indicating the number of years taken to smooth the population distribution by single ages. logical. Whether or not the top age group is open. Default TRUE. logical. If set to TRUE, smaller-n moving averages are applied on both tails such that all values are non-NA. If FALSE (default), tails are set to NA due to the lag of moving averages.

## Value

Vector with the smoothed demographic counts.

## Details

The moving window is applied symmetrically. By default (tails = FALSE) data endpoints are imputed with NAs in output: the is nothing under 0 or over the highest closed age group to average with. The open age group is not used in averaging, and it is returned as-is. Age intervals are assumed uniform. This function could be used with either single or 5-year age groups.

If tails is set to TRUE, then tails have been imputed using moving averages with successively smaller values of n, the cascade method.

## References

Roger G, Waltisperger D, Corbille-Guitton C (1981). Les structures par sexe et âge en Afrique. GDA, Paris, France.

## Examples

Pop  <-c(303583,390782,523903,458546,517996,400630,485606,325423,471481,189710,
385442,143205,270890,145105,138078,157444,153035,91566,247160,73115,
384222,83551,198555,111347,129851,477510,149272,100814,178465,50684,
577167,51878,97788,55544,58011,393200,85048,51131,80336,31246,
454698,34864,51810,31146,26618,228718,38504,23616,40836,15589,
339158,21349,26997,17590,17513,119763,22704,12336,17635,8485,
323263,9535,13906,9063,8294,90459,9817,6376,8884,3773,160609)
Age  <- 0:70
# final age group assumed open
mav(Pop, n = 3, Age = Age)
#>          0          1          2          3          4          5          6
#>         NA 406089.333 457743.667 500148.333 459057.333 468077.333 403886.333
#>          7          8          9         10         11         12         13
#> 427503.333 328871.333 348877.667 239452.333 266512.333 186400.000 184691.000
#>         14         15         16         17         18         19         20
#> 146875.667 149519.000 134015.000 163920.333 137280.333 234832.333 180296.000
#>         21         22         23         24         25         26         27
#> 222109.333 131151.000 146584.333 239569.333 252211.000 242532.000 142850.333
#>         28         29         30         31         32         33         34
#> 109987.667 268772.000 226576.333 242277.667  68403.333  70447.667 168918.333
#>         35         36         37         38         39         40         41
#> 178753.000 176459.667  72171.667  54237.667 188760.000 173602.667 180457.333
#>         42         43         44         45         46         47         48
#>  39273.333  36524.667  95494.000  97946.667  96946.000  34318.667  26680.333
#>         49         50         51         52         53         54         55
#> 131861.000 125365.333 129168.000  21978.667  20700.000  51622.000  53326.667
#>         56         57         58         59         60         61         62
#>  51601.000  17558.333  12818.667 116461.000 113761.000 115568.000  10834.667
#>         63         64         65         66         67         68         69
#>  10421.000  35938.667  36190.000  35550.667   8359.000   6344.333         NA
#>         70
#> 160609.000
if (FALSE) {
odds <- seq(3, 11, by = 2)
nwindows <- sapply(odds,
mav,
Value = Pop,
Age = Age,
OAG = TRUE,
tails = FALSE)
cols     <- gray(seq(.8, 0, length = 5))
lwds     <- seq(3, 1, length = 5)
plot(Age,Pop, col = "red", xlab = "Age", ylab = "The counts", pch=16,
main = "Moving average windows and extreme heaping")
legend("topright",
lty=1,
col = cols,
lwd = lwds,
legend = paste0("n=",seq(3,11,by=2)))
}

# For cascading smoothing on the tails:
mav(Pop, Age, tails = TRUE)
#>         0         1         2         3         4         5         6         7
#> 303583.00 402262.50 457743.67 500148.33 459057.33 468077.33 403886.33 427503.33
#>         8         9        10        11        12        13        14        15
#> 328871.33 348877.67 239452.33 266512.33 186400.00 184691.00 146875.67 149519.00
#>        16        17        18        19        20        21        22        23
#> 134015.00 163920.33 137280.33 234832.33 180296.00 222109.33 131151.00 146584.33
#>        24        25        26        27        28        29        30        31
#> 239569.33 252211.00 242532.00 142850.33 109987.67 268772.00 226576.33 242277.67
#>        32        33        34        35        36        37        38        39
#>  68403.33  70447.67 168918.33 178753.00 176459.67  72171.67  54237.67 188760.00
#>        40        41        42        43        44        45        46        47
#> 173602.67 180457.33  39273.33  36524.67  95494.00  97946.67  96946.00  34318.67
#>        48        49        50        51        52        53        54        55
#>  26680.33 131861.00 125365.33 129168.00  21978.67  20700.00  51622.00  53326.67
#>        56        57        58        59        60        61        62        63
#>  51601.00  17558.33  12818.67 116461.00 113761.00 115568.00  10834.67  10421.00
#>        64        65        66        67        68        69        70
#>  35938.67  36190.00  35550.67   8359.00   6979.25   8884.00 160609.00
if (FALSE) {
# Compare
nwindows_tails <- sapply(odds,
mav,
Value = Pop,
Age = Age,
OAG = TRUE,
tails = TRUE)

colnames(nwindows)        <- odds
colnamaes(nwindows_tails) <- odds

# NA triangles are completed with
# successively smaller ns.