title: How do I calculate a weighted moving average with custom weights?
body: <p>I'm working with NHL player performance data, and have a data frame with the following variables (among others). war_82 is a measure of player value over a full 82 game season. The data spans 11 seasons, from 2007-2008 to 2017-2018.</p>
  
  <pre><code> first_name last_name season    war_82
     &lt;chr&gt;      &lt;chr&gt;     &lt;chr&gt;      &lt;dbl&gt;
   1 5EBASTIAN  AHO       2017-2018 -0.560
   2 AARON      DELL      2016-2017  7.50 
   3 AARON      DELL      2017-2018  1.61 
   4 AARON      DOWNEY    2007-2008 -0.560
   5 AARON      EKBLAD    2014-2015  0.350
   6 AARON      EKBLAD    2015-2016 -0.350
   7 AARON      EKBLAD    2016-2017 -1.39 
   8 AARON      EKBLAD    2017-2018 -0.320
   9 AARON      JOHNSON   2007-2008 -1.42 
  10 AARON      JOHNSON   2008-2009 -1.19 
  </code></pre>
  
  <p>I'd like to reduce the season-to-season variability of the war_82 metric, and create a new variable that's a weighted war_82. Ideally I'd look at 3 seasons of data, and have season n (the current season) be the most heavily weighted, and seasons n-1 and n-2 (the two preceding seasons) be less heavily weighted as recency decreases. Let's say weights of 0.5, 0.3, and 0.2 for argument's sake. </p>
  
  <p>UPDATE FOR CLARITY: I'm hoping to calculate a weighted moving average. For example; Sidney Crosby's 20172018_weighted_war would be be determined by 2017-2018, 2016-2017, and 2015-2016. His 20162017_weighted_war would be be determined by 2016-2017, 2015-2016, and 2014-2015. So on and so forth.</p>
  
  <p>I have two main questions:</p>
  
  <p>1) What method would you recommend for this? I've looked at weighted.mean(), but some players have played more than others, so I'm not sure how to specify the "w" (weights) argument. For example, Sidney Crosby played during all 11 seasons in my data-set, but many players only played during 1 or 2 seasons. I don't really want to throw out data for players who have played fewer than 3 seasons.</p>
  
  <p>2) How would you determine the weights for each season? The simplest method is the one I've mentioned above, which was sort of inspired by the Marcel method (<a href="https://www.beyondtheboxscore.com/2016/2/22/11079186/projections-marcel-pecota-zips-steamer-explained-guide-math-is-fun" rel="nofollow noreferrer">https://www.beyondtheboxscore.com/2016/2/22/11079186/projections-marcel-pecota-zips-steamer-explained-guide-math-is-fun</a>). I suppose you could also determine how well seasons n-1 and n-2 predict season n, and use those as your weights?</p>
  
  <p>How would you approach this problem? Any and all guidance is greatly appreciated!</p>