Chart user defined moving average of a metric

249 views
Skip to first unread message

wooliethai

unread,
Apr 21, 2017, 6:40:26 AM4/21/17
to golden-cheetah-users
I want to plot trailing 30 day, 90 day, and 365 day average of a number of metrics. What's the easiest way to do this?

Ale Martinez

unread,
Apr 21, 2017, 10:59:41 AM4/21/17
to golden-cheetah-users
El viernes, 21 de abril de 2017, 7:40:26 (UTC-3), wooliethai escribió:
I want to plot trailing 30 day, 90 day, and 365 day average of a number of metrics. What's the easiest way to do this?  
On v3.3 you can add simple formula s.t. mean(Metric[Date-30:Date]) to a Metrics Trends chart, but this is broken in v3.4 so the alternative would be an R chart which is much more flexible but not that easy, something in the lines of http://www.cookbook-r.com/Manipulating_data/Calculating_a_moving_average/ with y <- GC.season.metrics()$Metric should do the trick on v3.4

Mark Liversedge

unread,
Apr 21, 2017, 11:08:21 AM4/21/17
to golden-cheetah-users
Its only broken because we wanted to add array subscripts, we could re-introduce with another operator, e.g: '[[' and ']]' ?

Ale Martinez

unread,
Apr 25, 2017, 5:12:56 PM4/25/17
to golden-cheetah-users
The change proposed by Mark will be on v3.5, starting from the next development build, the new formula to average a metric for the last 30 days will be: mean(Metric_Name[[Date-30:Date]])

wooliethai

unread,
May 7, 2017, 11:59:33 AM5/7/17
to golden-cheetah-users
Would this be the correct formula to chart 30-day trailing weekly mileage: mean(Distance[[Date-30:Date]])*0.621371*7?

The general trend appears to be correct but the actual mileage being graphed is overstated significantly (Strava says my average weekly mileage over the last 4 weeks was 29 miles, the formula says its 45.70 miles)

Robert Chung

unread,
May 7, 2017, 12:48:53 PM5/7/17
to golden-cheetah-users


On Tuesday, April 25, 2017 at 2:12:56 PM UTC-7, Ale Martinez wrote:
The change proposed by Mark will be on v3.5, starting from the next development build, the new formula to average a metric for the last 30 days will be: mean(Metric_Name[[Date-30:Date]])

Just a comment: in R, operator precedence means the ":" gets evaluated first. If you wanted something like a 30-day running mean, you'd have to add parentheses like (Date-30):Date

Mark Liversedge

unread,
May 7, 2017, 12:51:36 PM5/7/17
to golden-cheetah-users
Thanks, this is a GC "formula" rather than R, but your point is still noted. 

wooliethai

unread,
May 7, 2017, 1:16:16 PM5/7/17
to golden-cheetah-users
On further thought, my suspicion is that days with no activity are being excluded from the average rather than being counted a 0 miles. How can I include days without activity as being 0?

Ale Martinez

unread,
May 7, 2017, 1:54:29 PM5/7/17
to golden-cheetah-users
El domingo, 7 de mayo de 2017, 14:16:16 (UTC-3), wooliethai escribió:
On further thought, my suspicion is that days with no activity are being excluded from the average rather than being counted a 0 miles. How can I include days without activity as being 0?
You can try sum(Distance[[Date-30:Date]])*0.621371*7/30

Ale Martinez

unread,
May 7, 2017, 2:20:37 PM5/7/17
to golden-cheetah-users
Just for clarity sake: mean on date range is an average of the activities in that range not a daily mean. 

nev...@gmail.com

unread,
May 10, 2017, 2:43:48 PM5/10/17
to golden-cheetah-users
I also hit this problem. The R tools all assume a contigious timeseries and don't give a correct result. I use this script to graph rolling cumulative and moving average distances using the zoo package to create a contigious series:zm. Should be pretty easy to adapt to the metrics you want.

require(zoo)

win <- 7  # no. of days in window

z <- zoo(GC.season.metrics()$Distance,GC.season.metrics()$time)

z0 <- zoo(, seq(start(z), end(z), "day")) # empty vector of all the days

zm <- merge(z,z0, fill=0) 

index(zm) <- as.Date(index(zm)) # round to whole days

zm <- aggregate(zm,time(zm), sum) # no dup indexes allowed in zoo

ms7 <- rollapply(zm,win, sum, na.rm=TRUE,fill=NA,align = "right",partial=TRUE)

ma7 <- rollapply(zm,win, mean, na.rm=TRUE,fill=NA,align = "right",partial=TRUE)

zresult <- merge(zm,ma7,ms7)

plot(zresult,plot.type = "single", col=c("yellow","blue","red"),xlab="Date",ylab="Dist/km",main="Sliding Window Distances")


I havent worked out if it is possible to feed these vectors back into the PMC or Diary views yet.
Reply all
Reply to author
Forward
0 new messages