Solved

How to calculate a 7-days moving average

  • 16 April 2019
  • 8 replies
  • 26 views

I'm trying to calculate a 7-days moving average in FME 2017.

Using "DateTimeConverter" and the "Cumulative" option in "StatisticCalculator" I've managed to calculate a moving average on a weekly basis. However, the calcul of the average re-intializes at the start of each week with the value of the first day of the week.

Is there a way to implemente a moving average with a known frequency (7 days in this case, 365 in another I'm working on)?

Thanks

icon

Best answer by takashi 17 April 2019, 01:00

View original

8 replies

Badge +9

Just a comment and no answer, what about looking at Julian Days? https://en.wikipedia.org/wiki/Julian_day

Userlevel 2
Badge +17

Assuming that the input features have been sorted by date already and N represents the number of days to calculate average (e.g. N = 7 in this case, N = 365 in another case).

  1. Counter: Add 0-based sequential number attribute (e.g. "_count") to the features.
  2. Cloner: Create N copies for every feature. Save the copy number in an attribute (e.g. "_copynum").
  3. ExpressionEvaluator: Calculate _group = _count - _copynum
  4. Aggregator: Aggregate the features grouping by "_group", save the number of features aggregated (e.g. "_num"), and calculate the average of your interest attribute(s) (e.g. "value").
  5. Tester: Filter out the features whose "_num" is NOT equal to N.

Assuming that the input features have been sorted by date already and N represents the number of days to calculate average (e.g. N = 7 in this case, N = 365 in another case).

  1. Counter: Add 0-based sequential number attribute (e.g. "_count") to the features.
  2. Cloner: Create N copies for every feature. Save the copy number in an attribute (e.g. "_copynum").
  3. ExpressionEvaluator: Calculate _group = _count - _copynum
  4. Aggregator: Aggregate the features grouping by "_group", save the number of features aggregated (e.g. "_num"), and calculate the average of your interest attribute(s) (e.g. "value").
  5. Tester: Filter out the features whose "_num" is NOT equal to N.

Thank you, it was very helpful.

Is there a way to combine different types of calcul in a same column?

Now that I have a 7-days moving average, it means that I don't have an individual average for the 7 first values.

I'm not sure if what I mean is clear... I've to calculate an average with an increasing N for the 7 first values, then a 7-days moving average from the 7th.

day 1 average (day 1)

day 2 average (day 1, day 2)

day 3 average (day 1, day 2, day 3)

day 4 average (day 1, day 2, day 3, day 4)

...

day 7 average (day 1, day 2, day 3, day 4, day 5, day 6, day 7)

day 8 average (day 2, day 3, day 4, day 5, day 6, day 7, day 8)

...

And then combine everything in one column grouped by date.

Is it possible?

Userlevel 1
Badge +21

Thank you, it was very helpful.

Is there a way to combine different types of calcul in a same column?

Now that I have a 7-days moving average, it means that I don't have an individual average for the 7 first values.

I'm not sure if what I mean is clear... I've to calculate an average with an increasing N for the 7 first values, then a 7-days moving average from the 7th.

day 1 average (day 1)

day 2 average (day 1, day 2)

day 3 average (day 1, day 2, day 3)

day 4 average (day 1, day 2, day 3, day 4)

...

day 7 average (day 1, day 2, day 3, day 4, day 5, day 6, day 7)

day 8 average (day 2, day 3, day 4, day 5, day 6, day 7, day 8)

...

And then combine everything in one column grouped by date.

Is it possible?

If you want to keep the first 7 values, just drop the 'Filter out the features whose "_num" is NOT equal to N' part

If you want to keep the first 7 values, just drop the 'Filter out the features whose "_num" is NOT equal to N' part

thank you... it's the right values, now I just have to find a way to order them and connect them to the right date (by default they are all attributed to the same date).

Userlevel 1
Badge +21

thank you... it's the right values, now I just have to find a way to order them and connect them to the right date (by default they are all attributed to the same date).

Sorting by group will give you the correct order at the end, and if you sort by group in descending order prior to the aggregator you should also retain the correct date

Sorting by group will give you the correct order at the end, and if you sort by group in descending order prior to the aggregator you should also retain the correct date

it kind of works... I have the values in the right order, but the dates are wrong

The solution is good, but not perfect. The average value will be attributed to the first instance of the group, whilst it would be more correct to attibute it to the center of the group. The solution is to use the statisticscalculator instead and also calculate the median of the date (julian day). Then use the summary output and replace your date by _julianday._median or whatever it is called. Otherwise you'll observe a shift of N-days/2 towards the past.

Reply