Missing value replacement by weighted moving average. Uses semi-adaptive window size to ensure all NAs are replaced.
na_ma(x, k = 4, weighting = "exponential", maxgap = Inf)
Numeric Vector (vector
) or Time Series (ts
)
object in which missing values shall be replaced
integer width of the moving average window. Expands to both sides of the center element e.g. k=2 means 4 observations (2 left, 2 right) are taken into account. If all observations in the current window are NA, the window size is automatically increased until there are at least 2 non-NA values present.
Weighting to be used. Accepts the following input:
"simple" - Simple Moving Average (SMA)
"linear" - Linear Weighted Moving Average (LWMA)
"exponential" - Exponential Weighted Moving Average (EWMA) (default choice)
Maximum number of successive NAs to still perform imputation on. Default setting is to replace all NAs without restrictions. With this option set, consecutive NAs runs, that are longer than 'maxgap' will be left NA. This option mostly makes sense if you want to treat long runs of NA afterwards separately.
In this function missing values get replaced by moving average values. Moving Averages are also sometimes referred to as "moving mean", "rolling mean", "rolling average" or "running average".
The mean in this implementation taken from an equal number of observations
on either side of a central value. This means for an NA value at position
i
of a time series, the observations i-1,i+1 and i+1, i+2 (assuming
a window size of k=2) are used to calculate the mean.
Since it can in case of long NA gaps also occur, that all values next to the central value are also NA, the algorithm has a semi-adaptive window size. Whenever there are less than 2 non-NA values in the complete window available, the window size is incrementally increased, till at least 2 non-NA values are there. In all other cases the algorithm sticks to the pre-set window size.
There are options for using Simple Moving Average (SMA), Linear Weighted Moving Average (LWMA) and Exponential Weighted Moving Average (EWMA).
SMA: all observations in the window are equally weighted for calculating the mean.
LWMA: weights decrease in arithmetical progression. The observations directly next to a central value i, have weight 1/2, the observations one further away (i-2,i+2) have weight 1/3, the next (i-3,i+3) have weight 1/4, ...
EWMA: uses weighting factors which decrease exponentially. The observations directly next to a central value i, have weight 1/2^1, the observations one further away (i-2,i+2) have weight 1/2^2, the next (i-3,i+3) have weight 1/2^3, ...
# Example 1: Perform imputation with simple moving average
na_ma(tsAirgap, weighting = "simple")
#> Jan Feb Mar Apr May Jun Jul Aug
#> 1949 112.0000 118.0000 132.0000 129.0000 131.7143 135.0000 148.0000 148.0000
#> 1950 115.0000 126.0000 141.0000 135.0000 125.0000 149.0000 170.0000 170.0000
#> 1951 145.0000 150.0000 178.0000 163.0000 172.0000 178.0000 199.0000 199.0000
#> 1952 171.0000 180.0000 193.0000 181.0000 183.0000 218.0000 230.0000 242.0000
#> 1953 196.0000 196.0000 236.0000 235.0000 229.0000 243.0000 264.0000 272.0000
#> 1954 204.0000 188.0000 235.0000 227.0000 234.0000 245.8750 302.0000 293.0000
#> 1955 242.0000 233.0000 267.0000 269.0000 270.0000 315.0000 364.0000 347.0000
#> 1956 284.0000 277.0000 310.5000 338.5000 351.3333 374.0000 413.0000 405.0000
#> 1957 315.0000 301.0000 356.0000 348.0000 355.0000 380.3750 465.0000 467.0000
#> 1958 340.0000 318.0000 375.8571 348.0000 363.0000 435.0000 491.0000 505.0000
#> 1959 360.0000 342.0000 406.0000 396.0000 420.0000 472.0000 548.0000 559.0000
#> 1960 417.0000 391.0000 419.0000 461.0000 494.8750 535.0000 622.0000 606.0000
#> Sep Oct Nov Dec
#> 1949 126.7143 119.0000 104.0000 118.0000
#> 1950 147.4286 133.0000 155.1429 140.0000
#> 1951 184.0000 162.0000 146.0000 166.0000
#> 1952 209.0000 191.0000 172.0000 194.0000
#> 1953 237.0000 211.0000 180.0000 201.0000
#> 1954 259.0000 229.0000 203.0000 229.0000
#> 1955 312.0000 274.0000 237.0000 278.0000
#> 1956 355.0000 306.0000 271.0000 306.0000
#> 1957 404.0000 347.0000 382.4286 336.0000
#> 1958 404.0000 359.0000 310.0000 337.0000
#> 1959 463.0000 407.0000 362.0000 434.8750
#> 1960 508.0000 461.0000 390.0000 432.0000
# Example 2: Perform imputation with exponential weighted moving average
na_ma(tsAirgap)
#> Jan Feb Mar Apr May Jun Jul Aug
#> 1949 112.0000 118.0000 132.0000 129.0000 133.6552 135.0000 148.0000 148.0000
#> 1950 115.0000 126.0000 141.0000 135.0000 125.0000 149.0000 170.0000 170.0000
#> 1951 145.0000 150.0000 178.0000 163.0000 172.0000 178.0000 199.0000 199.0000
#> 1952 171.0000 180.0000 193.0000 181.0000 183.0000 218.0000 230.0000 242.0000
#> 1953 196.0000 196.0000 236.0000 235.0000 229.0000 243.0000 264.0000 272.0000
#> 1954 204.0000 188.0000 235.0000 227.0000 234.0000 259.1000 302.0000 293.0000
#> 1955 242.0000 233.0000 267.0000 269.0000 270.0000 315.0000 364.0000 347.0000
#> 1956 284.0000 277.0000 294.7778 334.3571 369.2778 374.0000 413.0000 405.0000
#> 1957 315.0000 301.0000 356.0000 348.0000 355.0000 399.6000 465.0000 467.0000
#> 1958 340.0000 318.0000 350.7931 348.0000 363.0000 435.0000 491.0000 505.0000
#> 1959 360.0000 342.0000 406.0000 396.0000 420.0000 472.0000 548.0000 559.0000
#> 1960 417.0000 391.0000 419.0000 461.0000 501.7000 535.0000 622.0000 606.0000
#> Sep Oct Nov Dec
#> 1949 129.8276 119.0000 104.0000 118.0000
#> 1950 152.0000 133.0000 144.3077 140.0000
#> 1951 184.0000 162.0000 146.0000 166.0000
#> 1952 209.0000 191.0000 172.0000 194.0000
#> 1953 237.0000 211.0000 180.0000 201.0000
#> 1954 259.0000 229.0000 203.0000 229.0000
#> 1955 312.0000 274.0000 237.0000 278.0000
#> 1956 355.0000 306.0000 271.0000 306.0000
#> 1957 404.0000 347.0000 361.2069 336.0000
#> 1958 404.0000 359.0000 310.0000 337.0000
#> 1959 463.0000 407.0000 362.0000 406.9333
#> 1960 508.0000 461.0000 390.0000 432.0000
# Example 3: Perform imputation with exponential weighted moving average, window size 6
na_ma(tsAirgap, k = 6)
#> Jan Feb Mar Apr May Jun Jul Aug
#> 1949 112.0000 118.0000 132.0000 129.0000 133.1597 135.0000 148.0000 148.0000
#> 1950 115.0000 126.0000 141.0000 135.0000 125.0000 149.0000 170.0000 170.0000
#> 1951 145.0000 150.0000 178.0000 163.0000 172.0000 178.0000 199.0000 199.0000
#> 1952 171.0000 180.0000 193.0000 181.0000 183.0000 218.0000 230.0000 242.0000
#> 1953 196.0000 196.0000 236.0000 235.0000 229.0000 243.0000 264.0000 272.0000
#> 1954 204.0000 188.0000 235.0000 227.0000 234.0000 256.6349 302.0000 293.0000
#> 1955 242.0000 233.0000 267.0000 269.0000 270.0000 315.0000 364.0000 347.0000
#> 1956 284.0000 277.0000 298.0641 330.4516 362.3590 374.0000 413.0000 405.0000
#> 1957 315.0000 301.0000 356.0000 348.0000 355.0000 396.9677 465.0000 467.0000
#> 1958 340.0000 318.0000 354.1311 348.0000 363.0000 435.0000 491.0000 505.0000
#> 1959 360.0000 342.0000 406.0000 396.0000 420.0000 472.0000 548.0000 559.0000
#> 1960 417.0000 391.0000 419.0000 461.0000 499.0161 535.0000 622.0000 606.0000
#> Sep Oct Nov Dec
#> 1949 129.8607 119.0000 104.0000 118.0000
#> 1950 151.7909 133.0000 144.8091 140.0000
#> 1951 184.0000 162.0000 146.0000 166.0000
#> 1952 209.0000 191.0000 172.0000 194.0000
#> 1953 237.0000 211.0000 180.0000 201.0000
#> 1954 259.0000 229.0000 203.0000 229.0000
#> 1955 312.0000 274.0000 237.0000 278.0000
#> 1956 355.0000 306.0000 271.0000 306.0000
#> 1957 404.0000 347.0000 360.9500 336.0000
#> 1958 404.0000 359.0000 310.0000 337.0000
#> 1959 463.0000 407.0000 362.0000 410.7661
#> 1960 508.0000 461.0000 390.0000 432.0000
# Example 4: Same as example 1, just written with pipe operator
tsAirgap %>% na_ma(weighting = "simple")
#> Jan Feb Mar Apr May Jun Jul Aug
#> 1949 112.0000 118.0000 132.0000 129.0000 131.7143 135.0000 148.0000 148.0000
#> 1950 115.0000 126.0000 141.0000 135.0000 125.0000 149.0000 170.0000 170.0000
#> 1951 145.0000 150.0000 178.0000 163.0000 172.0000 178.0000 199.0000 199.0000
#> 1952 171.0000 180.0000 193.0000 181.0000 183.0000 218.0000 230.0000 242.0000
#> 1953 196.0000 196.0000 236.0000 235.0000 229.0000 243.0000 264.0000 272.0000
#> 1954 204.0000 188.0000 235.0000 227.0000 234.0000 245.8750 302.0000 293.0000
#> 1955 242.0000 233.0000 267.0000 269.0000 270.0000 315.0000 364.0000 347.0000
#> 1956 284.0000 277.0000 310.5000 338.5000 351.3333 374.0000 413.0000 405.0000
#> 1957 315.0000 301.0000 356.0000 348.0000 355.0000 380.3750 465.0000 467.0000
#> 1958 340.0000 318.0000 375.8571 348.0000 363.0000 435.0000 491.0000 505.0000
#> 1959 360.0000 342.0000 406.0000 396.0000 420.0000 472.0000 548.0000 559.0000
#> 1960 417.0000 391.0000 419.0000 461.0000 494.8750 535.0000 622.0000 606.0000
#> Sep Oct Nov Dec
#> 1949 126.7143 119.0000 104.0000 118.0000
#> 1950 147.4286 133.0000 155.1429 140.0000
#> 1951 184.0000 162.0000 146.0000 166.0000
#> 1952 209.0000 191.0000 172.0000 194.0000
#> 1953 237.0000 211.0000 180.0000 201.0000
#> 1954 259.0000 229.0000 203.0000 229.0000
#> 1955 312.0000 274.0000 237.0000 278.0000
#> 1956 355.0000 306.0000 271.0000 306.0000
#> 1957 404.0000 347.0000 382.4286 336.0000
#> 1958 404.0000 359.0000 310.0000 337.0000
#> 1959 463.0000 407.0000 362.0000 434.8750
#> 1960 508.0000 461.0000 390.0000 432.0000