3

I want to compute a weighted moving average across multiple columns, using the same weights for each column. The weighted moving average shall be computed per group (in contrast to using `dplyr::across` with functions with more than one argument).

In the example below, the grouping should make the weighted moving average "reset" every year, yielding missing values for the first two observations of each year.

How do I make this work?

library(tidyverse)

weighted.filter <- function(x, wt, filter, ...) {
  filter <- filter / sum(filter)
  stats::filter(x * wt, filter, ...) / stats::filter(wt, filter, ...)
}

economics %>%
  group_by(year = lubridate::year(date)) %>%
  arrange(date) %>%
  mutate(across(
    c(pce, psavert, uempmed),
    list("moving_average_weighted" = weighted.filter),
    wt = pop, filter = rep(1, 3), sides = 1
  ))
#> Error: Problem with `mutate()` input `..1`.
#> x Input `..1` can't be recycled to size 12.
#> ℹ Input `..1` is `(function (.cols = everything(), .fns = NULL, ..., .names = NULL) ...`.
#> ℹ Input `..1` must be size 12 or 1, not 6.
#> ℹ The error occurred in group 2: year = 1968.

Created on 2021-03-31 by the reprex package (v1.0.0)

robust
  • 594
  • 5
  • 17

1 Answers1

3

Try

economics %>%
  group_by(year = lubridate::year(date)) %>%
  arrange(date) %>%
  mutate(across(
    c(pce, psavert, uempmed),
    list("moving_average_weighted" =
      ~ weighted.filter(., wt = pop, filter = rep(1, 3), sides = 1))
  ))
# # A tibble: 574 x 10
# # Groups:   year [49]
#    date         pce    pop psavert uempmed unemploy  year pce_moving_average_w~ psavert_moving_avera~ uempmed_moving_avera~
#    <date>     <dbl>  <dbl>   <dbl>   <dbl>    <dbl> <dbl>                 <dbl>                 <dbl>                 <dbl>
#  1 1967-07-01  507. 198712    12.6     4.5     2944  1967                   NA                   NA                   NA   
#  2 1967-08-01  510. 198911    12.6     4.7     2945  1967                   NA                   NA                   NA   
#  3 1967-09-01  516. 199113    11.9     4.6     2958  1967                  511.                  12.4                  4.60
#  4 1967-10-01  512. 199311    12.9     4.9     3143  1967                  513.                  12.5                  4.73
#  5 1967-11-01  517. 199498    12.8     4.7     3066  1967                  515.                  12.5                  4.73
#  6 1967-12-01  525. 199657    11.8     4.8     3018  1967                  518.                  12.5                  4.80
#  7 1968-01-01  531. 199808    11.7     5.1     2878  1968                   NA                   NA                   NA   
#  8 1968-02-01  534. 199920    12.3     4.5     3001  1968                   NA                   NA                   NA   
#  9 1968-03-01  544. 200056    11.7     4.1     2877  1968                  536.                  11.9                  4.57
# 10 1968-04-01  544  200208    12.3     4.6     2709  1968                  541.                  12.1                  4.40
# # ... with 564 more rows
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • Thank you, that works! But why does my attempt fail and yours work? – robust Apr 01 '21 at 01:06
  • Not sure exactly why, but if you `debug` your function and run it: first group `length(x)` is 6, `length(wt)` is 6, good; second group, `length(x)` is 12, `length(wt)` is 6, the same 6 as the first group ... bad. I would have assumed that `wt=pop` would have brought in each group's values of `pop, but apparently that isn't happening here with your call ... but with my call, it does (6 and 6, then 12 and 12, etc) – r2evans Apr 01 '21 at 01:14