R For Sport - Calculating Rolling Averages in R

Hi everyone, in this weeks R For Sport post we are looking at how to calculate rolling averages using different packages that have been developed specifically for R. These packages make it super simple to create your own Rolling Averages in a matter of minutes!

If you want to skip to the video, you can find it at the bottom of this page!

So let’s get straight in to it.

\[\\[0.1in]\]

Read in Data

First of all, let’s read in the data we want to use and apply a few small steps. The first thing I will do is introduce you to MUTATE, a function in the Tidyverse that allows you to add new columns to a df. All you need to do is supply a new column name and an expression that will calculate the column for you. So below let’s add a High-speed distance column to the data I have read in.

data_select <- data %>% 
  select(c(1,2,4,8,9,10,11)) %>%  
  mutate("High-Speed-Distance" = 
           `Band 5 Distance` + `Band 6 Distance` + `Band 7 Distance`) %>% 
  mutate(Date = dmy(Date)) %>%   
  select(-c(5,6,7))

head(data_select) %>% 
  kbl() %>% 
  kable_material(full_width = F, c("striped", "hover"))
Date Athlete ID Athlete Total Distance High-Speed-Distance
2020-01-27 1 athlete 1 3597.65 169.1000
2020-01-28 1 athlete 1 5652.50 347.7000
2020-01-29 1 athlete 1 6305.53 385.6050
2020-01-30 1 athlete 1 4090.13 149.4350
2020-01-31 1 athlete 1 7258.00 423.9375
2020-02-01 1 athlete 1 0.00 0.0000

As you can see in the table above, we have added the new High-Speed-Distance column along with only selecting the data we want to keep. This has minimised our DF to be much smaller and easier to work with.

Using RcppRoll

The first package we are going to use is called RcppRoll. This package has a few functions within it that allow you to create rolling averages or sums. The formula is quite simple and you can see it in the below code chunk.

data_rcpp <- data_select %>% 
  group_by(`Athlete ID`) %>% 
  arrange(`Athlete ID`, Date) %>% 
  mutate(rolling_avg_rcpp_7 = 
           roll_mean(`Total Distance`, n=7, fill=0, align="right"),
         rolling_avg_rcpp_28 = 
           roll_mean(`Total Distance`, n=28, fill=0, align="right")) %>% 
  mutate(rcpp_acwr = rolling_avg_rcpp_7 / rolling_avg_rcpp_28)

data_rcpp %>% 
  filter(`Athlete ID` == 1) %>% 
  kbl() %>% 
  kable_material(full_width = F, c("striped", "hover")) %>% 
  scroll_box(height="400px")
Date Athlete ID Athlete Total Distance High-Speed-Distance rolling_avg_rcpp_7 rolling_avg_rcpp_28 rcpp_acwr
2020-01-27 1 athlete 1 3597.650 169.1000 0.000 0.000 NaN
2020-01-28 1 athlete 1 5652.500 347.7000 0.000 0.000 NaN
2020-01-29 1 athlete 1 6305.530 385.6050 0.000 0.000 NaN
2020-01-30 1 athlete 1 4090.130 149.4350 0.000 0.000 NaN
2020-01-31 1 athlete 1 7258.000 423.9375 0.000 0.000 NaN
2020-02-01 1 athlete 1 0.000 0.0000 0.000 0.000 NaN
2020-02-02 1 athlete 1 10669.450 703.9500 5367.609 0.000 Inf
2020-02-03 1 athlete 1 4550.500 227.0500 5503.730 0.000 Inf
2020-02-04 1 athlete 1 11104.550 666.9000 6282.594 0.000 Inf
2020-02-05 1 athlete 1 4701.550 168.1500 6053.454 0.000 Inf
2020-02-06 1 athlete 1 0.000 0.0000 5469.150 0.000 Inf
2020-02-07 1 athlete 1 12609.350 754.3000 6233.629 0.000 Inf
2020-02-08 1 athlete 1 5544.200 338.2000 7025.657 0.000 Inf
2020-02-09 1 athlete 1 14269.000 839.8000 7539.879 0.000 Inf
2020-02-10 1 athlete 1 0.000 0.0000 6889.807 0.000 Inf
2020-02-11 1 athlete 1 5075.850 178.6000 6028.564 0.000 Inf
2020-02-12 1 athlete 1 18186.562 1042.6250 7954.995 0.000 Inf
2020-02-13 1 athlete 1 4372.280 180.7850 8579.606 0.000 Inf
2020-02-14 1 athlete 1 10259.050 971.8500 8243.849 0.000 Inf
2020-02-15 1 athlete 1 0.000 0.0000 7451.820 0.000 Inf
2020-02-16 1 athlete 1 5733.250 265.0500 6232.427 0.000 Inf
2020-02-17 1 athlete 1 15202.850 979.4500 8404.263 0.000 Inf
2020-02-18 1 athlete 1 5501.450 349.6000 8465.063 0.000 Inf
2020-02-19 1 athlete 1 10047.200 924.3500 7302.297 0.000 Inf
2020-02-20 1 athlete 1 0.000 0.0000 6677.686 0.000 Inf
2020-02-21 1 athlete 1 6274.750 244.1500 6108.500 0.000 Inf
2020-02-22 1 athlete 1 8464.500 522.5000 7317.714 0.000 Inf
2020-02-23 1 athlete 1 5066.350 281.2000 7222.443 6590.589 1.0958721
2020-02-24 1 athlete 1 10243.850 947.1500 6514.014 6827.954 0.9540215
2020-02-25 1 athlete 1 3492.200 0.0000 6226.979 6750.800 0.9224060
2020-02-26 1 athlete 1 9236.850 558.6000 6111.214 6855.490 0.8914336
2020-02-27 1 athlete 1 5324.750 322.0500 6871.893 6899.584 0.9959866
2020-02-28 1 athlete 1 10811.950 994.6500 7520.064 7026.510 1.0702417
2020-02-29 1 athlete 1 0.000 0.0000 6310.850 7026.510 0.8981485
2020-03-01 1 athlete 1 5083.450 293.5500 6313.293 6827.010 0.9247522
2020-03-02 1 athlete 1 10745.450 959.5000 6384.950 7048.259 0.9058904
2020-03-03 1 athlete 1 2064.350 0.0000 6180.971 6725.394 0.9190497
2020-03-04 1 athlete 1 0.000 0.0000 4861.421 6557.482 0.7413549
2020-03-05 1 athlete 1 3514.050 163.4000 4602.750 6682.984 0.6887268
2020-03-06 1 athlete 1 5452.050 328.7000 3837.050 6427.366 0.5969864
2020-03-07 1 athlete 1 4803.200 205.2000 4523.221 6400.902 0.7066538
2020-03-08 1 athlete 1 3782.900 160.5500 4337.429 6026.398 0.7197382
2020-03-09 1 athlete 1 10594.400 951.9000 4315.850 6404.769 0.6738494
2020-03-10 1 athlete 1 2567.850 0.0000 4387.779 6315.198 0.6947967
2020-03-11 1 athlete 1 0.000 0.0000 4387.779 5665.678 0.7744490
2020-03-12 1 athlete 1 4727.200 205.2000 4561.086 5678.354 0.8032409
2020-03-13 1 athlete 1 7445.150 384.7500 4845.814 5577.857 0.8687591
2020-03-14 1 athlete 1 5273.450 243.2000 4912.993 5766.195 0.8520338
2020-03-15 1 athlete 1 4009.000 255.5500 4945.293 5704.614 0.8668935
2020-03-16 1 athlete 1 10168.800 922.4500 4884.493 5524.827 0.8840988
2020-03-17 1 athlete 1 2694.200 0.0000 4902.543 5424.568 0.9037665
2020-03-18 1 athlete 1 0.000 0.0000 4902.543 5065.739 0.9677843
2020-03-19 1 athlete 1 4530.550 213.7500 4874.450 5227.545 0.9324550
2020-03-20 1 athlete 1 6987.250 427.5000 4809.036 5252.991 0.9154852
2020-03-21 1 Athlete 1 6102.800 337.2500 4927.514 5168.645 0.9533475
2020-03-22 1 Athlete 1 3851.300 202.3500 4904.986 5125.250 0.9570237
2020-03-23 1 Athlete 1 10079.500 928.1500 4892.229 5119.380 0.9556290
2020-03-24 1 Athlete 1 2527.950 0.0000 4868.479 5084.943 0.9574303
2020-03-25 1 Athlete 1 0.000 0.0000 4868.479 4755.055 1.0238532
2020-03-26 1 Athlete 1 7429.380 251.9400 5282.597 4830.221 1.0936554
2020-03-27 1 Athlete 1 10022.595 572.6600 5716.218 4802.029 1.1903754
2020-03-28 1 Athlete 1 7058.642 203.2050 5852.767 5054.124 1.1580181
2020-03-29 1 Athlete 1 12429.420 1073.8800 7078.212 5316.480 1.3313720
2020-03-30 1 Athlete 1 0.000 0.0000 5638.284 4932.714 1.1430389
2020-03-31 1 Athlete 1 6757.350 402.8000 6242.484 5100.321 1.2239394
2020-04-01 1 Athlete 1 4693.950 226.1000 6913.048 5267.962 1.3122813
2020-04-02 1 Athlete 1 0.000 0.0000 5851.708 5142.460 1.1379200
2020-04-03 1 Athlete 1 6658.550 368.6000 5371.130 5185.550 1.0357881
2020-04-04 1 Athlete 1 5490.050 271.7000 5147.046 5210.080 0.9879015
2020-04-05 1 Athlete 1 4481.150 223.2500 4011.579 5235.017 0.7662971
2020-04-06 1 Athlete 1 10278.050 974.7000 5479.871 5223.719 1.0490364
2020-04-07 1 Athlete 1 2575.450 0.0000 4882.457 5223.991 0.9346221
2020-04-08 1 Athlete 1 0.000 0.0000 4211.893 5223.991 0.8062597
2020-04-09 1 Athlete 1 4773.750 252.7000 4893.857 5225.653 0.9365063
2020-04-10 1 Athlete 1 6763.050 424.6500 4908.786 5201.292 0.9437627
2020-04-11 1 Athlete 1 5911.850 270.7500 4969.043 5224.092 0.9511782
2020-04-12 1 Athlete 1 4549.550 281.2000 4978.814 5243.398 0.9495397
2020-04-13 1 Athlete 1 10771.100 957.6000 5049.250 5264.908 0.9590385
2020-04-14 1 Athlete 1 1576.050 0.0000 4906.479 5224.975 0.9390435
2020-04-15 1 Athlete 1 0.000 0.0000 4906.479 5224.975 0.9390435
2020-04-16 1 Athlete 1 3491.250 185.2500 4723.264 5187.857 0.9104462
2020-04-17 1 Athlete 1 5264.900 310.6500 4509.243 5126.344 0.8796216
2020-04-18 1 Athlete 1 3672.700 195.7000 4189.364 5039.555 0.8312965
2020-04-19 1 Athlete 1 3145.450 156.7500 3988.779 5014.346 0.7954733
2020-04-20 1 Athlete 1 10670.400 964.2500 3974.393 5035.450 0.7892826
2020-04-21 1 Athlete 1 3176.800 0.0000 4203.071 5058.623 0.8308727
2020-04-22 1 Athlete 1 3848.450 207.1000 4752.850 5196.067 0.9147014
2020-04-23 1 Athlete 1 10471.850 896.8000 5750.079 5304.727 1.0839537
2020-04-24 1 Athlete 1 2897.500 0.0000 5411.879 5050.259 1.0716041
2020-04-25 1 Athlete 1 7175.350 393.3000 5912.257 5054.427 1.1697185
2020-04-26 1 Athlete 1 3841.800 245.1000 6011.736 4747.727 1.2662346
2020-04-27 1 Athlete 1 10788.200 957.6000 6028.564 5133.020 1.1744674
2020-04-28 1 Athlete 1 2857.600 0.0000 5982.964 4993.743 1.1980922
2020-04-29 1 Athlete 1 0.000 0.0000 5433.186 4826.102 1.1257918
2020-04-30 1 Athlete 1 5170.850 185.2500 4675.900 5010.775 0.9331690
2020-05-01 1 Athlete 1 7566.750 450.3000 5342.936 5043.211 1.0594314
2020-05-02 1 Athlete 1 5900.450 296.4000 5160.807 5057.868 1.0203523
2020-05-03 1 Athlete 1 4314.900 270.7500 5228.393 5051.930 1.0349297
2020-05-04 1 Athlete 1 10654.250 1001.3000 5209.257 5065.366 1.0284068
2020-05-05 1 Athlete 1 3527.350 0.0000 5304.936 5099.363 1.0403135
2020-05-06 1 Athlete 1 0.000 0.0000 5304.936 5099.363 1.0403135
2020-05-07 1 Athlete 1 4993.200 223.2500 5279.557 5107.200 1.0337479
2020-05-08 1 Athlete 1 8216.550 502.5500 5372.386 5159.111 1.0413395
2020-05-09 1 Athlete 1 6255.750 304.0000 5423.143 5171.393 1.0486813
2020-05-10 1 Athlete 1 5333.300 235.6000 5568.629 5199.384 1.0710170
2020-05-11 1 Athlete 1 10613.400 937.6500 5562.793 5193.752 1.0710548
2020-05-12 1 Athlete 1 3826.600 0.0000 5605.543 5274.129 1.0628377
2020-05-13 1 Athlete 1 0.000 0.0000 5605.543 5274.129 1.0628377
2020-05-14 1 Athlete 1 6004.950 285.9500 5750.079 5363.904 1.0719951
2020-05-15 1 Athlete 1 9632.050 596.6000 5952.293 5519.873 1.0783387
2020-05-16 1 Athlete 1 7699.750 361.0000 6158.579 5663.696 1.0873779
2020-05-17 1 Athlete 1 4891.550 326.8000 6095.471 5726.057 1.0645146
2020-05-18 1 Athlete 1 10631.450 977.5500 6098.050 5724.666 1.0652237
2020-05-19 1 Athlete 1 2823.400 0.0000 5954.736 5712.045 1.0424876
2020-05-20 1 Athlete 1 0.000 0.0000 5954.736 5574.600 1.0681907
2020-05-21 1 Athlete 1 3694.550 79.8000 5624.679 5332.554 1.0547814
2020-05-22 1 Athlete 1 5278.200 332.5000 5002.700 5417.579 0.9234199
2020-05-23 1 Athlete 1 4657.850 240.3500 4568.143 5327.668 0.8574376
2020-05-24 1 Athlete 1 3552.050 133.0000 4376.786 5317.320 0.8231188
2020-05-25 1 Athlete 1 10444.300 976.6000 4350.050 5305.038 0.8199848
2020-05-26 1 Athlete 1 2713.200 0.0000 4334.307 5299.880 0.8178123
2020-05-27 1 Athlete 1 0.000 0.0000 4334.307 5299.880 0.8178123
2020-05-28 1 Athlete 1 5602.150 228.9500 4606.821 5315.284 0.8667122
2020-05-29 1 Athlete 1 8102.550 500.6500 5010.300 5334.420 0.9392399
2020-05-30 1 Athlete 1 6449.550 361.9500 5266.257 5354.030 0.9836061
2020-05-31 1 Athlete 1 4418.450 226.1000 5390.029 5357.729 1.0060287
2020-06-01 1 Athlete 1 10495.600 964.2500 5397.357 5352.062 1.0084630
2020-06-02 1 Athlete 1 3997.600 0.0000 5580.843 5368.857 1.0394843
2020-06-03 1 Athlete 1 0.000 0.0000 5580.843 5368.857 1.0394843
2020-06-04 1 Athlete 1 6181.650 291.6500 5663.629 5411.302 1.0466296
2020-06-05 1 Athlete 1 8937.600 501.6000 5782.921 5437.054 1.0636131
2020-06-06 1 Athlete 1 7485.050 311.6000 5930.850 5480.957 1.0820829
2020-06-07 1 Athlete 1 5257.300 283.1000 6050.686 5478.243 1.1044939
2020-06-08 1 Athlete 1 10914.550 994.6500 6110.536 5488.998 1.1132333
2020-06-09 1 Athlete 1 2808.200 0.0000 5940.621 5452.627 1.0894972
2020-06-10 1 Athlete 1 0.000 0.0000 5940.621 5452.627 1.0894972
2020-06-11 1 Athlete 1 4576.150 167.2000 5711.264 5401.598 1.0573286
2020-06-12 1 Athlete 1 6963.500 367.6500 5429.250 5306.293 1.0231719
2020-06-13 1 Athlete 1 5621.150 232.7500 5162.979 5232.057 0.9867971
2020-06-14 1 Athlete 1 4499.200 203.3000 5054.679 5218.045 0.9686921
2020-06-15 1 Athlete 1 10618.150 890.1500 5012.336 5217.570 0.9606648
2020-06-16 1 Athlete 1 0.000 0.0000 4611.164 5116.734 0.9011929
2020-06-17 1 Athlete 1 5355.150 334.4000 5376.186 5307.989 1.0128479
2020-06-18 1 Athlete 1 4417.500 249.8500 5353.521 5333.809 1.0036958
2020-06-19 1 Athlete 1 10569.700 920.5500 5868.693 5522.791 1.0626317
2020-06-20 1 Athlete 1 0.000 0.0000 5065.671 5356.439 0.9457162
2020-06-21 1 Athlete 1 4221.800 214.7000 5026.043 5380.359 0.9341464
2020-06-22 1 Athlete 1 10279.950 953.8000 4977.729 5374.489 0.9261770

There are a few things to notice in the function inputs. Firstly, you need to align your rolling values, with the default being center, this means that the data is equally distributed to each side of your value. I chose right, meaning that the value for our 7 or 28 day period will only start to appear once 7/28 days have passed. Because of this, our ACWR value takes 28 days to be calculated, resulting in NaN and inf values, which are blanks and infinite values. Both of these mean that the ACWR is unable to be calculated.

But now that we have some values, let’s plot them and see what they look like.

As we can see, our 7 day value appears quite early but it takes a little longer for the 28 day value to start appearing. This isn’t necessarily a bad thing, but it would be nicer to have your values from day 1. So let’s see what we have in another package!

Using the Zoo Package

The next package we are going to use is called Zoo. In much the same way as RcppRoll, Zoo provides a couple of functions for the calculation of rolling averages and sums. So let’s give it a go below.

data_zoo <- data_rcpp %>% 
  group_by(`Athlete ID`) %>% 
  arrange(`Athlete ID`, Date) %>%
  mutate(rolling_avg_zoo_7 = 
           zoo::rollmean(`Total Distance`, k = 7, fill=0,align="right"),
         rolling_avg_zoo_28 = 
           zoo::rollmean(`Total Distance`, k = 28, fill=0,align="right")) %>% 
  mutate(zoo_acwr = rolling_avg_zoo_7 / rolling_avg_zoo_28)

data_zoo %>% 
  filter(`Athlete ID` == 1) %>% 
  select(c(1:5,9:11)) %>% 
  kbl() %>% 
  kable_material(full_width = F, c("striped", "hover")) %>% 
  scroll_box(height="400px")
Date Athlete ID Athlete Total Distance High-Speed-Distance rolling_avg_zoo_7 rolling_avg_zoo_28 zoo_acwr
2020-01-27 1 athlete 1 3597.650 169.1000 0.000 0.000 NaN
2020-01-28 1 athlete 1 5652.500 347.7000 0.000 0.000 NaN
2020-01-29 1 athlete 1 6305.530 385.6050 0.000 0.000 NaN
2020-01-30 1 athlete 1 4090.130 149.4350 0.000 0.000 NaN
2020-01-31 1 athlete 1 7258.000 423.9375 0.000 0.000 NaN
2020-02-01 1 athlete 1 0.000 0.0000 0.000 0.000 NaN
2020-02-02 1 athlete 1 10669.450 703.9500 5367.609 0.000 Inf
2020-02-03 1 athlete 1 4550.500 227.0500 5503.730 0.000 Inf
2020-02-04 1 athlete 1 11104.550 666.9000 6282.594 0.000 Inf
2020-02-05 1 athlete 1 4701.550 168.1500 6053.454 0.000 Inf
2020-02-06 1 athlete 1 0.000 0.0000 5469.150 0.000 Inf
2020-02-07 1 athlete 1 12609.350 754.3000 6233.629 0.000 Inf
2020-02-08 1 athlete 1 5544.200 338.2000 7025.657 0.000 Inf
2020-02-09 1 athlete 1 14269.000 839.8000 7539.879 0.000 Inf
2020-02-10 1 athlete 1 0.000 0.0000 6889.807 0.000 Inf
2020-02-11 1 athlete 1 5075.850 178.6000 6028.564 0.000 Inf
2020-02-12 1 athlete 1 18186.562 1042.6250 7954.995 0.000 Inf
2020-02-13 1 athlete 1 4372.280 180.7850 8579.606 0.000 Inf
2020-02-14 1 athlete 1 10259.050 971.8500 8243.849 0.000 Inf
2020-02-15 1 athlete 1 0.000 0.0000 7451.820 0.000 Inf
2020-02-16 1 athlete 1 5733.250 265.0500 6232.427 0.000 Inf
2020-02-17 1 athlete 1 15202.850 979.4500 8404.263 0.000 Inf
2020-02-18 1 athlete 1 5501.450 349.6000 8465.063 0.000 Inf
2020-02-19 1 athlete 1 10047.200 924.3500 7302.297 0.000 Inf
2020-02-20 1 athlete 1 0.000 0.0000 6677.686 0.000 Inf
2020-02-21 1 athlete 1 6274.750 244.1500 6108.500 0.000 Inf
2020-02-22 1 athlete 1 8464.500 522.5000 7317.714 0.000 Inf
2020-02-23 1 athlete 1 5066.350 281.2000 7222.443 6590.589 1.0958721
2020-02-24 1 athlete 1 10243.850 947.1500 6514.014 6827.954 0.9540215
2020-02-25 1 athlete 1 3492.200 0.0000 6226.979 6750.800 0.9224060
2020-02-26 1 athlete 1 9236.850 558.6000 6111.214 6855.490 0.8914336
2020-02-27 1 athlete 1 5324.750 322.0500 6871.893 6899.584 0.9959866
2020-02-28 1 athlete 1 10811.950 994.6500 7520.064 7026.510 1.0702417
2020-02-29 1 athlete 1 0.000 0.0000 6310.850 7026.510 0.8981485
2020-03-01 1 athlete 1 5083.450 293.5500 6313.293 6827.010 0.9247522
2020-03-02 1 athlete 1 10745.450 959.5000 6384.950 7048.259 0.9058904
2020-03-03 1 athlete 1 2064.350 0.0000 6180.971 6725.394 0.9190497
2020-03-04 1 athlete 1 0.000 0.0000 4861.421 6557.482 0.7413549
2020-03-05 1 athlete 1 3514.050 163.4000 4602.750 6682.984 0.6887268
2020-03-06 1 athlete 1 5452.050 328.7000 3837.050 6427.366 0.5969864
2020-03-07 1 athlete 1 4803.200 205.2000 4523.221 6400.902 0.7066538
2020-03-08 1 athlete 1 3782.900 160.5500 4337.429 6026.398 0.7197382
2020-03-09 1 athlete 1 10594.400 951.9000 4315.850 6404.769 0.6738494
2020-03-10 1 athlete 1 2567.850 0.0000 4387.779 6315.198 0.6947967
2020-03-11 1 athlete 1 0.000 0.0000 4387.779 5665.678 0.7744490
2020-03-12 1 athlete 1 4727.200 205.2000 4561.086 5678.354 0.8032409
2020-03-13 1 athlete 1 7445.150 384.7500 4845.814 5577.857 0.8687591
2020-03-14 1 athlete 1 5273.450 243.2000 4912.993 5766.195 0.8520338
2020-03-15 1 athlete 1 4009.000 255.5500 4945.293 5704.614 0.8668935
2020-03-16 1 athlete 1 10168.800 922.4500 4884.493 5524.827 0.8840988
2020-03-17 1 athlete 1 2694.200 0.0000 4902.543 5424.568 0.9037665
2020-03-18 1 athlete 1 0.000 0.0000 4902.543 5065.739 0.9677843
2020-03-19 1 athlete 1 4530.550 213.7500 4874.450 5227.545 0.9324550
2020-03-20 1 athlete 1 6987.250 427.5000 4809.036 5252.991 0.9154852
2020-03-21 1 Athlete 1 6102.800 337.2500 4927.514 5168.645 0.9533475
2020-03-22 1 Athlete 1 3851.300 202.3500 4904.986 5125.250 0.9570237
2020-03-23 1 Athlete 1 10079.500 928.1500 4892.229 5119.380 0.9556290
2020-03-24 1 Athlete 1 2527.950 0.0000 4868.479 5084.943 0.9574303
2020-03-25 1 Athlete 1 0.000 0.0000 4868.479 4755.055 1.0238532
2020-03-26 1 Athlete 1 7429.380 251.9400 5282.597 4830.221 1.0936554
2020-03-27 1 Athlete 1 10022.595 572.6600 5716.218 4802.029 1.1903754
2020-03-28 1 Athlete 1 7058.642 203.2050 5852.767 5054.124 1.1580181
2020-03-29 1 Athlete 1 12429.420 1073.8800 7078.212 5316.480 1.3313720
2020-03-30 1 Athlete 1 0.000 0.0000 5638.284 4932.714 1.1430389
2020-03-31 1 Athlete 1 6757.350 402.8000 6242.484 5100.321 1.2239394
2020-04-01 1 Athlete 1 4693.950 226.1000 6913.048 5267.962 1.3122813
2020-04-02 1 Athlete 1 0.000 0.0000 5851.708 5142.460 1.1379200
2020-04-03 1 Athlete 1 6658.550 368.6000 5371.130 5185.550 1.0357881
2020-04-04 1 Athlete 1 5490.050 271.7000 5147.046 5210.080 0.9879015
2020-04-05 1 Athlete 1 4481.150 223.2500 4011.579 5235.017 0.7662971
2020-04-06 1 Athlete 1 10278.050 974.7000 5479.871 5223.719 1.0490364
2020-04-07 1 Athlete 1 2575.450 0.0000 4882.457 5223.991 0.9346221
2020-04-08 1 Athlete 1 0.000 0.0000 4211.893 5223.991 0.8062597
2020-04-09 1 Athlete 1 4773.750 252.7000 4893.857 5225.653 0.9365063
2020-04-10 1 Athlete 1 6763.050 424.6500 4908.786 5201.292 0.9437627
2020-04-11 1 Athlete 1 5911.850 270.7500 4969.043 5224.092 0.9511782
2020-04-12 1 Athlete 1 4549.550 281.2000 4978.814 5243.398 0.9495397
2020-04-13 1 Athlete 1 10771.100 957.6000 5049.250 5264.908 0.9590385
2020-04-14 1 Athlete 1 1576.050 0.0000 4906.479 5224.975 0.9390435
2020-04-15 1 Athlete 1 0.000 0.0000 4906.479 5224.975 0.9390435
2020-04-16 1 Athlete 1 3491.250 185.2500 4723.264 5187.857 0.9104462
2020-04-17 1 Athlete 1 5264.900 310.6500 4509.243 5126.344 0.8796216
2020-04-18 1 Athlete 1 3672.700 195.7000 4189.364 5039.555 0.8312965
2020-04-19 1 Athlete 1 3145.450 156.7500 3988.779 5014.346 0.7954733
2020-04-20 1 Athlete 1 10670.400 964.2500 3974.393 5035.450 0.7892826
2020-04-21 1 Athlete 1 3176.800 0.0000 4203.071 5058.623 0.8308727
2020-04-22 1 Athlete 1 3848.450 207.1000 4752.850 5196.067 0.9147014
2020-04-23 1 Athlete 1 10471.850 896.8000 5750.079 5304.727 1.0839537
2020-04-24 1 Athlete 1 2897.500 0.0000 5411.879 5050.259 1.0716041
2020-04-25 1 Athlete 1 7175.350 393.3000 5912.257 5054.427 1.1697185
2020-04-26 1 Athlete 1 3841.800 245.1000 6011.736 4747.727 1.2662346
2020-04-27 1 Athlete 1 10788.200 957.6000 6028.564 5133.020 1.1744674
2020-04-28 1 Athlete 1 2857.600 0.0000 5982.964 4993.743 1.1980922
2020-04-29 1 Athlete 1 0.000 0.0000 5433.186 4826.102 1.1257918
2020-04-30 1 Athlete 1 5170.850 185.2500 4675.900 5010.775 0.9331690
2020-05-01 1 Athlete 1 7566.750 450.3000 5342.936 5043.211 1.0594314
2020-05-02 1 Athlete 1 5900.450 296.4000 5160.807 5057.868 1.0203523
2020-05-03 1 Athlete 1 4314.900 270.7500 5228.393 5051.930 1.0349297
2020-05-04 1 Athlete 1 10654.250 1001.3000 5209.257 5065.366 1.0284068
2020-05-05 1 Athlete 1 3527.350 0.0000 5304.936 5099.363 1.0403135
2020-05-06 1 Athlete 1 0.000 0.0000 5304.936 5099.363 1.0403135
2020-05-07 1 Athlete 1 4993.200 223.2500 5279.557 5107.200 1.0337479
2020-05-08 1 Athlete 1 8216.550 502.5500 5372.386 5159.111 1.0413395
2020-05-09 1 Athlete 1 6255.750 304.0000 5423.143 5171.393 1.0486813
2020-05-10 1 Athlete 1 5333.300 235.6000 5568.629 5199.384 1.0710170
2020-05-11 1 Athlete 1 10613.400 937.6500 5562.793 5193.752 1.0710548
2020-05-12 1 Athlete 1 3826.600 0.0000 5605.543 5274.129 1.0628377
2020-05-13 1 Athlete 1 0.000 0.0000 5605.543 5274.129 1.0628377
2020-05-14 1 Athlete 1 6004.950 285.9500 5750.079 5363.904 1.0719951
2020-05-15 1 Athlete 1 9632.050 596.6000 5952.293 5519.873 1.0783387
2020-05-16 1 Athlete 1 7699.750 361.0000 6158.579 5663.696 1.0873779
2020-05-17 1 Athlete 1 4891.550 326.8000 6095.471 5726.057 1.0645146
2020-05-18 1 Athlete 1 10631.450 977.5500 6098.050 5724.666 1.0652237
2020-05-19 1 Athlete 1 2823.400 0.0000 5954.736 5712.045 1.0424876
2020-05-20 1 Athlete 1 0.000 0.0000 5954.736 5574.600 1.0681907
2020-05-21 1 Athlete 1 3694.550 79.8000 5624.679 5332.554 1.0547814
2020-05-22 1 Athlete 1 5278.200 332.5000 5002.700 5417.579 0.9234199
2020-05-23 1 Athlete 1 4657.850 240.3500 4568.143 5327.668 0.8574376
2020-05-24 1 Athlete 1 3552.050 133.0000 4376.786 5317.320 0.8231188
2020-05-25 1 Athlete 1 10444.300 976.6000 4350.050 5305.038 0.8199848
2020-05-26 1 Athlete 1 2713.200 0.0000 4334.307 5299.880 0.8178123
2020-05-27 1 Athlete 1 0.000 0.0000 4334.307 5299.880 0.8178123
2020-05-28 1 Athlete 1 5602.150 228.9500 4606.821 5315.284 0.8667122
2020-05-29 1 Athlete 1 8102.550 500.6500 5010.300 5334.420 0.9392399
2020-05-30 1 Athlete 1 6449.550 361.9500 5266.257 5354.030 0.9836061
2020-05-31 1 Athlete 1 4418.450 226.1000 5390.029 5357.729 1.0060287
2020-06-01 1 Athlete 1 10495.600 964.2500 5397.357 5352.062 1.0084630
2020-06-02 1 Athlete 1 3997.600 0.0000 5580.843 5368.857 1.0394843
2020-06-03 1 Athlete 1 0.000 0.0000 5580.843 5368.857 1.0394843
2020-06-04 1 Athlete 1 6181.650 291.6500 5663.629 5411.302 1.0466296
2020-06-05 1 Athlete 1 8937.600 501.6000 5782.921 5437.054 1.0636131
2020-06-06 1 Athlete 1 7485.050 311.6000 5930.850 5480.957 1.0820829
2020-06-07 1 Athlete 1 5257.300 283.1000 6050.686 5478.243 1.1044939
2020-06-08 1 Athlete 1 10914.550 994.6500 6110.536 5488.998 1.1132333
2020-06-09 1 Athlete 1 2808.200 0.0000 5940.621 5452.627 1.0894972
2020-06-10 1 Athlete 1 0.000 0.0000 5940.621 5452.627 1.0894972
2020-06-11 1 Athlete 1 4576.150 167.2000 5711.264 5401.598 1.0573286
2020-06-12 1 Athlete 1 6963.500 367.6500 5429.250 5306.293 1.0231719
2020-06-13 1 Athlete 1 5621.150 232.7500 5162.979 5232.057 0.9867971
2020-06-14 1 Athlete 1 4499.200 203.3000 5054.679 5218.045 0.9686921
2020-06-15 1 Athlete 1 10618.150 890.1500 5012.336 5217.570 0.9606648
2020-06-16 1 Athlete 1 0.000 0.0000 4611.164 5116.734 0.9011929
2020-06-17 1 Athlete 1 5355.150 334.4000 5376.186 5307.989 1.0128479
2020-06-18 1 Athlete 1 4417.500 249.8500 5353.521 5333.809 1.0036958
2020-06-19 1 Athlete 1 10569.700 920.5500 5868.693 5522.791 1.0626317
2020-06-20 1 Athlete 1 0.000 0.0000 5065.671 5356.439 0.9457162
2020-06-21 1 Athlete 1 4221.800 214.7000 5026.043 5380.359 0.9341464
2020-06-22 1 Athlete 1 10279.950 953.8000 4977.729 5374.489 0.9261770

The functions provided by Zoo are very similar to RcppRoll, requiring the alignment and also the delay in seeing values. So we can assume that our plot will look the same, but just to be sure, let’s plot the values below.

As we assumed, the figure looks identical to what we had previously. So let’s try our last package and see what we get.

Using the Pracma Package

The last package on the list as pracma. This package provide the basic rolling average functionality we have seen previously, with the ability to also calculate the exponentially weighted values as well. First, let’s try the simple average calc.

data_pracma <- data_zoo %>%
  group_by(`Athlete ID`) %>% 
  arrange(`Athlete ID`, Date) %>%  
  mutate(rolling_avg_pracma_7 = 
           movavg(`Total Distance` , n = 7, type="s" ),
         rolling_avg_pracma_28 = 
           movavg(`Total Distance` , n =28, type="s" )) %>% 
  mutate(pracma_acwr = rolling_avg_pracma_7 / rolling_avg_pracma_28)


data_pracma %>% 
  filter(`Athlete ID` == 1) %>% 
  select(c(1:5,12:14)) %>% 
  kbl() %>% 
  kable_material(full_width = F, c("striped", "hover")) %>% 
  scroll_box(height="400px")
Date Athlete ID Athlete Total Distance High-Speed-Distance rolling_avg_pracma_7 rolling_avg_pracma_28 pracma_acwr
2020-01-27 1 athlete 1 3597.650 169.1000 3597.650 3597.650 1.0000000
2020-01-28 1 athlete 1 5652.500 347.7000 4625.075 4625.075 1.0000000
2020-01-29 1 athlete 1 6305.530 385.6050 5185.227 5185.227 1.0000000
2020-01-30 1 athlete 1 4090.130 149.4350 4911.453 4911.453 1.0000000
2020-01-31 1 athlete 1 7258.000 423.9375 5380.762 5380.762 1.0000000
2020-02-01 1 athlete 1 0.000 0.0000 4483.968 4483.968 1.0000000
2020-02-02 1 athlete 1 10669.450 703.9500 5367.609 5367.609 1.0000000
2020-02-03 1 athlete 1 4550.500 227.0500 5503.730 5265.470 1.0452495
2020-02-04 1 athlete 1 11104.550 666.9000 6282.594 5914.257 1.0622796
2020-02-05 1 athlete 1 4701.550 168.1500 6053.454 5792.986 1.0449627
2020-02-06 1 athlete 1 0.000 0.0000 5469.150 5266.351 1.0385085
2020-02-07 1 athlete 1 12609.350 754.3000 6233.629 5878.267 1.0604534
2020-02-08 1 athlete 1 5544.200 338.2000 7025.657 5852.570 1.2004397
2020-02-09 1 athlete 1 14269.000 839.8000 7539.879 6453.744 1.1682953
2020-02-10 1 athlete 1 0.000 0.0000 6889.807 6023.494 1.1438224
2020-02-11 1 athlete 1 5075.850 178.6000 6028.564 5964.266 1.0107805
2020-02-12 1 athlete 1 18186.562 1042.6250 7954.995 6683.225 1.1902928
2020-02-13 1 athlete 1 4372.280 180.7850 8579.606 6554.839 1.3088965
2020-02-14 1 athlete 1 10259.050 971.8500 8243.849 6749.797 1.2213476
2020-02-15 1 athlete 1 0.000 0.0000 7451.820 6412.308 1.1621121
2020-02-16 1 athlete 1 5733.250 265.0500 6232.427 6379.972 0.9768739
2020-02-17 1 athlete 1 15202.850 979.4500 8404.263 6781.011 1.2393819
2020-02-18 1 athlete 1 5501.450 349.6000 8465.063 6725.378 1.2586746
2020-02-19 1 athlete 1 10047.200 924.3500 7302.297 6863.788 1.0638874
2020-02-20 1 athlete 1 0.000 0.0000 6677.686 6589.236 1.0134233
2020-02-21 1 athlete 1 6274.750 244.1500 6108.500 6577.140 0.9287471
2020-02-22 1 athlete 1 8464.500 522.5000 7317.714 6647.043 1.1008977
2020-02-23 1 athlete 1 5066.350 281.2000 7222.443 6590.589 1.0958721
2020-02-24 1 athlete 1 10243.850 947.1500 6514.014 6827.954 0.9540215
2020-02-25 1 athlete 1 3492.200 0.0000 6226.979 6750.800 0.9224060
2020-02-26 1 athlete 1 9236.850 558.6000 6111.214 6855.490 0.8914336
2020-02-27 1 athlete 1 5324.750 322.0500 6871.893 6899.584 0.9959866
2020-02-28 1 athlete 1 10811.950 994.6500 7520.064 7026.510 1.0702417
2020-02-29 1 athlete 1 0.000 0.0000 6310.850 7026.510 0.8981485
2020-03-01 1 athlete 1 5083.450 293.5500 6313.293 6827.010 0.9247522
2020-03-02 1 athlete 1 10745.450 959.5000 6384.950 7048.259 0.9058904
2020-03-03 1 athlete 1 2064.350 0.0000 6180.971 6725.394 0.9190497
2020-03-04 1 athlete 1 0.000 0.0000 4861.421 6557.482 0.7413549
2020-03-05 1 athlete 1 3514.050 163.4000 4602.750 6682.984 0.6887268
2020-03-06 1 athlete 1 5452.050 328.7000 3837.050 6427.366 0.5969864
2020-03-07 1 athlete 1 4803.200 205.2000 4523.221 6400.902 0.7066538
2020-03-08 1 athlete 1 3782.900 160.5500 4337.429 6026.398 0.7197382
2020-03-09 1 athlete 1 10594.400 951.9000 4315.850 6404.769 0.6738494
2020-03-10 1 athlete 1 2567.850 0.0000 4387.779 6315.198 0.6947967
2020-03-11 1 athlete 1 0.000 0.0000 4387.779 5665.678 0.7744490
2020-03-12 1 athlete 1 4727.200 205.2000 4561.086 5678.354 0.8032409
2020-03-13 1 athlete 1 7445.150 384.7500 4845.814 5577.857 0.8687591
2020-03-14 1 athlete 1 5273.450 243.2000 4912.993 5766.195 0.8520338
2020-03-15 1 athlete 1 4009.000 255.5500 4945.293 5704.614 0.8668935
2020-03-16 1 athlete 1 10168.800 922.4500 4884.493 5524.827 0.8840988
2020-03-17 1 athlete 1 2694.200 0.0000 4902.543 5424.568 0.9037665
2020-03-18 1 athlete 1 0.000 0.0000 4902.543 5065.739 0.9677843
2020-03-19 1 athlete 1 4530.550 213.7500 4874.450 5227.545 0.9324550
2020-03-20 1 athlete 1 6987.250 427.5000 4809.036 5252.991 0.9154852
2020-03-21 1 Athlete 1 6102.800 337.2500 4927.514 5168.645 0.9533475
2020-03-22 1 Athlete 1 3851.300 202.3500 4904.986 5125.250 0.9570237
2020-03-23 1 Athlete 1 10079.500 928.1500 4892.229 5119.380 0.9556290
2020-03-24 1 Athlete 1 2527.950 0.0000 4868.479 5084.943 0.9574303
2020-03-25 1 Athlete 1 0.000 0.0000 4868.479 4755.055 1.0238532
2020-03-26 1 Athlete 1 7429.380 251.9400 5282.597 4830.221 1.0936554
2020-03-27 1 Athlete 1 10022.595 572.6600 5716.218 4802.029 1.1903754
2020-03-28 1 Athlete 1 7058.642 203.2050 5852.767 5054.124 1.1580181
2020-03-29 1 Athlete 1 12429.420 1073.8800 7078.212 5316.480 1.3313720
2020-03-30 1 Athlete 1 0.000 0.0000 5638.284 4932.714 1.1430389
2020-03-31 1 Athlete 1 6757.350 402.8000 6242.484 5100.321 1.2239394
2020-04-01 1 Athlete 1 4693.950 226.1000 6913.048 5267.962 1.3122813
2020-04-02 1 Athlete 1 0.000 0.0000 5851.708 5142.460 1.1379200
2020-04-03 1 Athlete 1 6658.550 368.6000 5371.130 5185.550 1.0357881
2020-04-04 1 Athlete 1 5490.050 271.7000 5147.046 5210.080 0.9879015
2020-04-05 1 Athlete 1 4481.150 223.2500 4011.579 5235.017 0.7662971
2020-04-06 1 Athlete 1 10278.050 974.7000 5479.871 5223.719 1.0490364
2020-04-07 1 Athlete 1 2575.450 0.0000 4882.457 5223.991 0.9346221
2020-04-08 1 Athlete 1 0.000 0.0000 4211.893 5223.991 0.8062597
2020-04-09 1 Athlete 1 4773.750 252.7000 4893.857 5225.653 0.9365063
2020-04-10 1 Athlete 1 6763.050 424.6500 4908.786 5201.292 0.9437627
2020-04-11 1 Athlete 1 5911.850 270.7500 4969.043 5224.092 0.9511782
2020-04-12 1 Athlete 1 4549.550 281.2000 4978.814 5243.398 0.9495397
2020-04-13 1 Athlete 1 10771.100 957.6000 5049.250 5264.908 0.9590385
2020-04-14 1 Athlete 1 1576.050 0.0000 4906.479 5224.975 0.9390435
2020-04-15 1 Athlete 1 0.000 0.0000 4906.479 5224.975 0.9390435
2020-04-16 1 Athlete 1 3491.250 185.2500 4723.264 5187.857 0.9104462
2020-04-17 1 Athlete 1 5264.900 310.6500 4509.243 5126.344 0.8796216
2020-04-18 1 Athlete 1 3672.700 195.7000 4189.364 5039.555 0.8312965
2020-04-19 1 Athlete 1 3145.450 156.7500 3988.779 5014.346 0.7954733
2020-04-20 1 Athlete 1 10670.400 964.2500 3974.393 5035.450 0.7892826
2020-04-21 1 Athlete 1 3176.800 0.0000 4203.071 5058.623 0.8308727
2020-04-22 1 Athlete 1 3848.450 207.1000 4752.850 5196.067 0.9147014
2020-04-23 1 Athlete 1 10471.850 896.8000 5750.079 5304.727 1.0839537
2020-04-24 1 Athlete 1 2897.500 0.0000 5411.879 5050.259 1.0716041
2020-04-25 1 Athlete 1 7175.350 393.3000 5912.257 5054.427 1.1697185
2020-04-26 1 Athlete 1 3841.800 245.1000 6011.736 4747.727 1.2662346
2020-04-27 1 Athlete 1 10788.200 957.6000 6028.564 5133.020 1.1744674
2020-04-28 1 Athlete 1 2857.600 0.0000 5982.964 4993.743 1.1980922
2020-04-29 1 Athlete 1 0.000 0.0000 5433.186 4826.102 1.1257918
2020-04-30 1 Athlete 1 5170.850 185.2500 4675.900 5010.775 0.9331690
2020-05-01 1 Athlete 1 7566.750 450.3000 5342.936 5043.211 1.0594314
2020-05-02 1 Athlete 1 5900.450 296.4000 5160.807 5057.868 1.0203523
2020-05-03 1 Athlete 1 4314.900 270.7500 5228.393 5051.930 1.0349297
2020-05-04 1 Athlete 1 10654.250 1001.3000 5209.257 5065.366 1.0284068
2020-05-05 1 Athlete 1 3527.350 0.0000 5304.936 5099.363 1.0403135
2020-05-06 1 Athlete 1 0.000 0.0000 5304.936 5099.363 1.0403135
2020-05-07 1 Athlete 1 4993.200 223.2500 5279.557 5107.200 1.0337479
2020-05-08 1 Athlete 1 8216.550 502.5500 5372.386 5159.111 1.0413395
2020-05-09 1 Athlete 1 6255.750 304.0000 5423.143 5171.393 1.0486813
2020-05-10 1 Athlete 1 5333.300 235.6000 5568.629 5199.384 1.0710170
2020-05-11 1 Athlete 1 10613.400 937.6500 5562.793 5193.752 1.0710548
2020-05-12 1 Athlete 1 3826.600 0.0000 5605.543 5274.129 1.0628377
2020-05-13 1 Athlete 1 0.000 0.0000 5605.543 5274.129 1.0628377
2020-05-14 1 Athlete 1 6004.950 285.9500 5750.079 5363.904 1.0719951
2020-05-15 1 Athlete 1 9632.050 596.6000 5952.293 5519.873 1.0783387
2020-05-16 1 Athlete 1 7699.750 361.0000 6158.579 5663.696 1.0873779
2020-05-17 1 Athlete 1 4891.550 326.8000 6095.471 5726.057 1.0645146
2020-05-18 1 Athlete 1 10631.450 977.5500 6098.050 5724.666 1.0652237
2020-05-19 1 Athlete 1 2823.400 0.0000 5954.736 5712.045 1.0424876
2020-05-20 1 Athlete 1 0.000 0.0000 5954.736 5574.600 1.0681907
2020-05-21 1 Athlete 1 3694.550 79.8000 5624.679 5332.554 1.0547814
2020-05-22 1 Athlete 1 5278.200 332.5000 5002.700 5417.579 0.9234199
2020-05-23 1 Athlete 1 4657.850 240.3500 4568.143 5327.668 0.8574376
2020-05-24 1 Athlete 1 3552.050 133.0000 4376.786 5317.320 0.8231188
2020-05-25 1 Athlete 1 10444.300 976.6000 4350.050 5305.038 0.8199848
2020-05-26 1 Athlete 1 2713.200 0.0000 4334.307 5299.880 0.8178123
2020-05-27 1 Athlete 1 0.000 0.0000 4334.307 5299.880 0.8178123
2020-05-28 1 Athlete 1 5602.150 228.9500 4606.821 5315.284 0.8667122
2020-05-29 1 Athlete 1 8102.550 500.6500 5010.300 5334.420 0.9392399
2020-05-30 1 Athlete 1 6449.550 361.9500 5266.257 5354.030 0.9836061
2020-05-31 1 Athlete 1 4418.450 226.1000 5390.029 5357.729 1.0060287
2020-06-01 1 Athlete 1 10495.600 964.2500 5397.357 5352.062 1.0084630
2020-06-02 1 Athlete 1 3997.600 0.0000 5580.843 5368.857 1.0394843
2020-06-03 1 Athlete 1 0.000 0.0000 5580.843 5368.857 1.0394843
2020-06-04 1 Athlete 1 6181.650 291.6500 5663.629 5411.302 1.0466296
2020-06-05 1 Athlete 1 8937.600 501.6000 5782.921 5437.054 1.0636131
2020-06-06 1 Athlete 1 7485.050 311.6000 5930.850 5480.957 1.0820829
2020-06-07 1 Athlete 1 5257.300 283.1000 6050.686 5478.243 1.1044939
2020-06-08 1 Athlete 1 10914.550 994.6500 6110.536 5488.998 1.1132333
2020-06-09 1 Athlete 1 2808.200 0.0000 5940.621 5452.627 1.0894972
2020-06-10 1 Athlete 1 0.000 0.0000 5940.621 5452.627 1.0894972
2020-06-11 1 Athlete 1 4576.150 167.2000 5711.264 5401.598 1.0573286
2020-06-12 1 Athlete 1 6963.500 367.6500 5429.250 5306.293 1.0231719
2020-06-13 1 Athlete 1 5621.150 232.7500 5162.979 5232.057 0.9867971
2020-06-14 1 Athlete 1 4499.200 203.3000 5054.679 5218.045 0.9686921
2020-06-15 1 Athlete 1 10618.150 890.1500 5012.336 5217.570 0.9606648
2020-06-16 1 Athlete 1 0.000 0.0000 4611.164 5116.734 0.9011929
2020-06-17 1 Athlete 1 5355.150 334.4000 5376.186 5307.989 1.0128479
2020-06-18 1 Athlete 1 4417.500 249.8500 5353.521 5333.809 1.0036958
2020-06-19 1 Athlete 1 10569.700 920.5500 5868.693 5522.791 1.0626317
2020-06-20 1 Athlete 1 0.000 0.0000 5065.671 5356.439 0.9457162
2020-06-21 1 Athlete 1 4221.800 214.7000 5026.043 5380.359 0.9341464
2020-06-22 1 Athlete 1 10279.950 953.8000 4977.729 5374.489 0.9261770

The first difference we can see with the pracma calculation is the difference in our values. In the first 7 days, we are getting the average of the days preceeding that day, the then average of the full 7 days from day 7 onwards. This is the same for our 28 day value which makes this possibly a better way of calculating our values. Let’s see the plot!

As we can see, with our data starting straightaway, we get the gradual build up of our values until they then start to differ from day 7 onwards. Let’s try and calculate the EWMA version now!

data_pracma <- data_pracma %>%
  group_by(`Athlete ID`) %>% 
  arrange(`Athlete ID`, Date) %>% 
  mutate(rolling_avg_pracma_e_7 = 
           movavg(`Total Distance` , n = 7, type="e"),
         rolling_avg_pracma_e_28 = 
           movavg(`Total Distance` , n = 28, type="e")) %>% 
  mutate(pracma_e_acwr = rolling_avg_pracma_e_7 / rolling_avg_pracma_e_28)

data_pracma %>% 
  filter(`Athlete ID` == 1) %>% 
  select(c(1:5,15:17)) %>% 
  kbl() %>% 
  kable_material(full_width = F, c("striped", "hover")) %>% 
  scroll_box(height="400px")
Date Athlete ID Athlete Total Distance High-Speed-Distance rolling_avg_pracma_e_7 rolling_avg_pracma_e_28 pracma_e_acwr
2020-01-27 1 athlete 1 3597.650 169.1000 3597.650 3597.650 1.0000000
2020-01-28 1 athlete 1 5652.500 347.7000 4111.363 3739.364 1.0994818
2020-01-29 1 athlete 1 6305.530 385.6050 4659.904 3916.341 1.1898618
2020-01-30 1 athlete 1 4090.130 149.4350 4517.461 3928.326 1.1499709
2020-01-31 1 athlete 1 7258.000 423.9375 5202.596 4157.959 1.2512379
2020-02-01 1 athlete 1 0.000 0.0000 3901.947 3871.203 1.0079416
2020-02-02 1 athlete 1 10669.450 703.9500 5593.823 4340.048 1.2888850
2020-02-03 1 athlete 1 4550.500 227.0500 5332.992 4354.562 1.2246909
2020-02-04 1 athlete 1 11104.550 666.9000 6775.881 4820.078 1.4057617
2020-02-05 1 athlete 1 4701.550 168.1500 6257.299 4811.904 1.3003790
2020-02-06 1 athlete 1 0.000 0.0000 4692.974 4480.048 1.0475275
2020-02-07 1 athlete 1 12609.350 754.3000 6672.068 5040.690 1.3236418
2020-02-08 1 athlete 1 5544.200 338.2000 6390.101 5075.415 1.2590303
2020-02-09 1 athlete 1 14269.000 839.8000 8359.826 5709.455 1.4642073
2020-02-10 1 athlete 1 0.000 0.0000 6269.869 5315.700 1.1795003
2020-02-11 1 athlete 1 5075.850 178.6000 5971.364 5299.158 1.1268515
2020-02-12 1 athlete 1 18186.562 1042.6250 9025.164 6187.945 1.4585075
2020-02-13 1 athlete 1 4372.280 180.7850 7861.943 6062.726 1.2967669
2020-02-14 1 athlete 1 10259.050 971.8500 8461.220 6352.128 1.3320292
2020-02-15 1 athlete 1 0.000 0.0000 6345.915 5914.050 1.0730235
2020-02-16 1 athlete 1 5733.250 265.0500 6192.749 5901.581 1.0493372
2020-02-17 1 athlete 1 15202.850 979.4500 8445.274 6543.048 1.2907247
2020-02-18 1 athlete 1 5501.450 349.6000 7709.318 6471.214 1.1913249
2020-02-19 1 athlete 1 10047.200 924.3500 8293.788 6717.833 1.2345927
2020-02-20 1 athlete 1 0.000 0.0000 6220.341 6254.535 0.9945330
2020-02-21 1 athlete 1 6274.750 244.1500 6233.944 6255.929 0.9964857
2020-02-22 1 athlete 1 8464.500 522.5000 6791.583 6408.244 1.0598196
2020-02-23 1 athlete 1 5066.350 281.2000 6360.274 6315.700 1.0070578
2020-02-24 1 athlete 1 10243.850 947.1500 7331.168 6586.607 1.1130418
2020-02-25 1 athlete 1 3492.200 0.0000 6371.426 6373.199 0.9997218
2020-02-26 1 athlete 1 9236.850 558.6000 7087.782 6570.692 1.0786964
2020-02-27 1 athlete 1 5324.750 322.0500 6647.024 6484.765 1.0250215
2020-02-28 1 athlete 1 10811.950 994.6500 7688.256 6783.192 1.1334274
2020-02-29 1 athlete 1 0.000 0.0000 5766.192 6315.385 0.9130388
2020-03-01 1 athlete 1 5083.450 293.5500 5595.506 6230.424 0.8980939
2020-03-02 1 athlete 1 10745.450 959.5000 6882.992 6541.805 1.0521548
2020-03-03 1 athlete 1 2064.350 0.0000 5678.332 6233.015 0.9110088
2020-03-04 1 athlete 1 0.000 0.0000 4258.749 5803.152 0.7338682
2020-03-05 1 athlete 1 3514.050 163.4000 4072.574 5645.283 0.7214118
2020-03-06 1 athlete 1 5452.050 328.7000 4417.443 5631.957 0.7843532
2020-03-07 1 athlete 1 4803.200 205.2000 4513.882 5574.801 0.8096939
2020-03-08 1 athlete 1 3782.900 160.5500 4331.137 5451.222 0.7945259
2020-03-09 1 athlete 1 10594.400 951.9000 5896.953 5805.924 1.0156786
2020-03-10 1 athlete 1 2567.850 0.0000 5064.677 5582.608 0.9072241
2020-03-11 1 athlete 1 0.000 0.0000 3798.508 5197.601 0.7308194
2020-03-12 1 athlete 1 4727.200 205.2000 4030.681 5165.159 0.7803594
2020-03-13 1 athlete 1 7445.150 384.7500 4884.298 5322.400 0.9176871
2020-03-14 1 athlete 1 5273.450 243.2000 4981.586 5319.024 0.9365601
2020-03-15 1 athlete 1 4009.000 255.5500 4738.440 5228.678 0.9062405
2020-03-16 1 athlete 1 10168.800 922.4500 6096.030 5569.376 1.0945625
2020-03-17 1 athlete 1 2694.200 0.0000 5245.572 5371.088 0.9766313
2020-03-18 1 athlete 1 0.000 0.0000 3934.179 5000.668 0.7867307
2020-03-19 1 athlete 1 4530.550 213.7500 4083.272 4968.246 0.8218739
2020-03-20 1 athlete 1 6987.250 427.5000 4809.266 5107.488 0.9416110
2020-03-21 1 Athlete 1 6102.800 337.2500 5132.650 5176.130 0.9915999
2020-03-22 1 Athlete 1 3851.300 202.3500 4812.312 5084.762 0.9464183
2020-03-23 1 Athlete 1 10079.500 928.1500 6129.109 5429.227 1.1289101
2020-03-24 1 Athlete 1 2527.950 0.0000 5228.819 5229.139 0.9999389
2020-03-25 1 Athlete 1 0.000 0.0000 3921.615 4868.509 0.8055063
2020-03-26 1 Athlete 1 7429.380 251.9400 4798.556 5045.120 0.9511281
2020-03-27 1 Athlete 1 10022.595 572.6600 6104.566 5388.395 1.1329099
2020-03-28 1 Athlete 1 7058.642 203.2050 6343.085 5503.584 1.1525371
2020-03-29 1 Athlete 1 12429.420 1073.8800 7864.669 5981.228 1.3148920
2020-03-30 1 Athlete 1 0.000 0.0000 5898.502 5568.729 1.0592185
2020-03-31 1 Athlete 1 6757.350 402.8000 6113.214 5650.703 1.0818500
2020-04-01 1 Athlete 1 4693.950 226.1000 5758.398 5584.720 1.0310987
2020-04-02 1 Athlete 1 0.000 0.0000 4318.798 5199.567 0.8306073
2020-04-03 1 Athlete 1 6658.550 368.6000 4903.736 5300.187 0.9252007
2020-04-04 1 Athlete 1 5490.050 271.7000 5050.315 5313.281 0.9505078
2020-04-05 1 Athlete 1 4481.150 223.2500 4908.023 5255.892 0.9338135
2020-04-06 1 Athlete 1 10278.050 974.7000 6250.530 5602.248 1.1157182
2020-04-07 1 Athlete 1 2575.450 0.0000 5331.760 5393.503 0.9885523
2020-04-08 1 Athlete 1 0.000 0.0000 3998.820 5021.538 0.7963338
2020-04-09 1 Athlete 1 4773.750 252.7000 4192.553 5004.449 0.8377651
2020-04-10 1 Athlete 1 6763.050 424.6500 4835.177 5125.732 0.9433145
2020-04-11 1 Athlete 1 5911.850 270.7500 5104.345 5179.947 0.9854050
2020-04-12 1 Athlete 1 4549.550 281.2000 4965.646 5136.471 0.9667428
2020-04-13 1 Athlete 1 10771.100 957.6000 6417.010 5525.066 1.1614358
2020-04-14 1 Athlete 1 1576.050 0.0000 5206.770 5252.720 0.9912521
2020-04-15 1 Athlete 1 0.000 0.0000 3905.077 4890.464 0.7985086
2020-04-16 1 Athlete 1 3491.250 185.2500 3801.621 4793.966 0.7930011
2020-04-17 1 Athlete 1 5264.900 310.6500 4167.440 4826.444 0.8634597
2020-04-18 1 Athlete 1 3672.700 195.7000 4043.755 4746.876 0.8518772
2020-04-19 1 Athlete 1 3145.450 156.7500 3819.179 4636.433 0.8237322
2020-04-20 1 Athlete 1 10670.400 964.2500 5531.984 5052.568 1.0948856
2020-04-21 1 Athlete 1 3176.800 0.0000 4943.188 4923.205 1.0040590
2020-04-22 1 Athlete 1 3848.450 207.1000 4669.504 4849.084 0.9629661
2020-04-23 1 Athlete 1 10471.850 896.8000 6120.090 5236.861 1.1686562
2020-04-24 1 Athlete 1 2897.500 0.0000 5314.443 5075.526 1.0470724
2020-04-25 1 Athlete 1 7175.350 393.3000 5779.670 5220.341 1.1071440
2020-04-26 1 Athlete 1 3841.800 245.1000 5295.202 5125.269 1.0331559
2020-04-27 1 Athlete 1 10788.200 957.6000 6668.452 5515.816 1.2089691
2020-04-28 1 Athlete 1 2857.600 0.0000 5715.739 5332.491 1.0718703
2020-04-29 1 Athlete 1 0.000 0.0000 4286.804 4964.733 0.8634511
2020-04-30 1 Athlete 1 5170.850 185.2500 4507.816 4978.948 0.9053751
2020-05-01 1 Athlete 1 7566.750 450.3000 5272.549 5157.417 1.0223236
2020-05-02 1 Athlete 1 5900.450 296.4000 5429.524 5208.661 1.0424031
2020-05-03 1 Athlete 1 4314.900 270.7500 5150.868 5147.022 1.0007473
2020-05-04 1 Athlete 1 10654.250 1001.3000 6526.714 5526.831 1.1809143
2020-05-05 1 Athlete 1 3527.350 0.0000 5776.873 5388.936 1.0719877
2020-05-06 1 Athlete 1 0.000 0.0000 4332.655 5017.285 0.8635457
2020-05-07 1 Athlete 1 4993.200 223.2500 4497.791 5015.624 0.8967560
2020-05-08 1 Athlete 1 8216.550 502.5500 5427.481 5236.377 1.0364953
2020-05-09 1 Athlete 1 6255.750 304.0000 5634.548 5306.679 1.0617842
2020-05-10 1 Athlete 1 5333.300 235.6000 5559.236 5308.515 1.0472300
2020-05-11 1 Athlete 1 10613.400 937.6500 6822.777 5674.369 1.2023851
2020-05-12 1 Athlete 1 3826.600 0.0000 6073.733 5546.937 1.0949706
2020-05-13 1 Athlete 1 0.000 0.0000 4555.300 5164.389 0.8820597
2020-05-14 1 Athlete 1 6004.950 285.9500 4917.712 5222.359 0.9416649
2020-05-15 1 Athlete 1 9632.050 596.6000 6096.297 5526.476 1.1031075
2020-05-16 1 Athlete 1 7699.750 361.0000 6497.160 5676.357 1.1446004
2020-05-17 1 Athlete 1 4891.550 326.8000 6095.757 5622.232 1.0842237
2020-05-18 1 Athlete 1 10631.450 977.5500 7229.681 5967.695 1.2114694
2020-05-19 1 Athlete 1 2823.400 0.0000 6128.110 5750.847 1.0656013
2020-05-20 1 Athlete 1 0.000 0.0000 4596.083 5354.237 0.8584010
2020-05-21 1 Athlete 1 3694.550 79.8000 4370.700 5239.776 0.8341386
2020-05-22 1 Athlete 1 5278.200 332.5000 4597.575 5242.426 0.8769937
2020-05-23 1 Athlete 1 4657.850 240.3500 4612.644 5202.110 0.8866870
2020-05-24 1 Athlete 1 3552.050 133.0000 4347.495 5088.313 0.8544079
2020-05-25 1 Athlete 1 10444.300 976.6000 5871.696 5457.692 1.0758571
2020-05-26 1 Athlete 1 2713.200 0.0000 5082.072 5268.416 0.9646300
2020-05-27 1 Athlete 1 0.000 0.0000 3811.554 4905.077 0.7770630
2020-05-28 1 Athlete 1 5602.150 228.9500 4259.203 4953.151 0.8598977
2020-05-29 1 Athlete 1 8102.550 500.6500 5220.040 5170.351 1.0096103
2020-05-30 1 Athlete 1 6449.550 361.9500 5527.417 5258.572 1.0511252
2020-05-31 1 Athlete 1 4418.450 226.1000 5250.176 5200.632 1.0095264
2020-06-01 1 Athlete 1 10495.600 964.2500 6561.532 5565.802 1.1789013
2020-06-02 1 Athlete 1 3997.600 0.0000 5920.549 5457.651 1.0848164
2020-06-03 1 Athlete 1 0.000 0.0000 4440.412 5081.261 0.8738799
2020-06-04 1 Athlete 1 6181.650 291.6500 4875.721 5157.150 0.9454294
2020-06-05 1 Athlete 1 8937.600 501.6000 5891.191 5417.870 1.0873628
2020-06-06 1 Athlete 1 7485.050 311.6000 6289.656 5560.435 1.1311446
2020-06-07 1 Athlete 1 5257.300 283.1000 6031.567 5539.529 1.0888231
2020-06-08 1 Athlete 1 10914.550 994.6500 7252.313 5910.220 1.2270800
2020-06-09 1 Athlete 1 2808.200 0.0000 6141.284 5696.287 1.0781205
2020-06-10 1 Athlete 1 0.000 0.0000 4605.963 5303.440 0.8684860
2020-06-11 1 Athlete 1 4576.150 167.2000 4598.510 5253.282 0.8753594
2020-06-12 1 Athlete 1 6963.500 367.6500 5189.757 5371.228 0.9662143
2020-06-13 1 Athlete 1 5621.150 232.7500 5297.606 5388.464 0.9831383
2020-06-14 1 Athlete 1 4499.200 203.3000 5098.004 5327.136 0.9569879
2020-06-15 1 Athlete 1 10618.150 890.1500 6478.041 5692.033 1.1380891
2020-06-16 1 Athlete 1 0.000 0.0000 4858.530 5299.479 0.9167940
2020-06-17 1 Athlete 1 5355.150 334.4000 4982.685 5303.319 0.9395410
2020-06-18 1 Athlete 1 4417.500 249.8500 4841.389 5242.228 0.9235366
2020-06-19 1 Athlete 1 10569.700 920.5500 6273.467 5609.639 1.1183369
2020-06-20 1 Athlete 1 0.000 0.0000 4705.100 5222.768 0.9008825
2020-06-21 1 Athlete 1 4221.800 214.7000 4584.275 5153.736 0.8895053
2020-06-22 1 Athlete 1 10279.950 953.8000 6008.194 5507.268 1.0909573

The easiest of change, with type being changed to e, the function now knows to use a different moving average type. We can see that these values differ from day 2 already, which is really interesting. Let’s plot these as well!

From day 2, we can see the difference very quickly which is interesting. There is a lot more variation between these values. The more interesting thing will be the output of the ACWR values.

So let’s plot those.

Our EWMA ACWR version is different and far more varied compared to our normal rolling average version. My data isn’t perfect, so the look of this in reality might be much different, so give it a go with your data and see how it appears!

If you enjoyed this post, please leave a comment below with how your images appeared if you tried it on your data, I would love to see them.

\[\\[0.1in]\]

Video

You can find the full video showing the information discussed above, in a little more detail, below.

As always, hit like on the video and subscribe here for more videos to help you Power Performance Through Data.

Until next time,

Josh

comments powered by Disqus

Related