mav: Calculate the moving average (mav) over 3 or 5 years.

View source: R/MAV.R

mavR Documentation

Calculate the moving average (mav) over 3 or 5 years.

Description

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

Usage

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

Arguments

Value

numeric. A vector of demographic counts in single age groups.

Age

integer. A vector of ages corresponding to the lower integer bound of the counts.

n

integer. A single number, (often 3 or 5), indicating the number of years taken to smooth the population distribution by single ages.

OAG

logical. Whether or not the top age group is open. Default TRUE.

tails

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.

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.

Value

Vector with the smoothed demographic counts.

References

\insertRef

GDA1981IREDADemoTools

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)

## Not run: 
 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")
matplot(Age,nwindows,type='l',col=cols, lwd=lwds, add=TRUE,lty=1)
legend("topright",
		lty=1,
		col = cols,
		lwd = lwds,
		legend = paste0("n=",seq(3,11,by=2)))

## End(Not run)

# For cascading smoothing on the tails:
mav(Pop, Age, tails = TRUE)

## Not run: 
# 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.
head(nwindows)
head(nwindows_tails)

tail(nwindows)
tail(nwindows_tails)

## End(Not run)

timriffe/DemoTools documentation built on Jan. 28, 2024, 5:13 a.m.