1

I have a dc.js example going where I'd like to calculate moving averages over two different windows of the dataset and group them. Ultimately, I'd like to also group the ratio between the two moving averages with them so I can access them using a key.

I've got a handle on how to do single moving avg using reductio, but I'm not sure how to do two of them concurrently, make a ratio, and show all three (MA1,MA2,Ratio) in the graph.

Here is how I'm doing each independent MA:

    var date_array = [];
    var mapped_date_array = [];

    var activities_infinity = activityDistanceByDayGroup.top(Infinity);
    var i = 0;
    for (i=0; i < activities_infinity.length; i++) {
        date_array.push(activities_infinity[i].key);
    }
    date_array.sort(function (date1, date2) {
        if (date1 > date2) return 1;
        if (date1 < date2) return -1;
    })
    mapped_date_array = date_array.map(function(e) { return e.toDateString(); 
    });

    //      For Chronic Load
    var cLoadMovingAvg = activityByDay.groupAll();
    cReducer = reductio().groupAll(function(record) {
        var idx = mapped_date_array.indexOf(record.dtg.toDateString());
        if (record.dtg < date_array[9]) {
            return [date_array[idx]];
        } else {
            var i = 0;
            var return_array = [];
            for (i = 9; i >= 0; i--) {
                return_array.push(date_array[idx - i]);
            }
            return return_array;
        }
    }).count(true).sum(dc.pluck('Distance')).avg(true)(cLoadMovingAvg); 

    //      For Acute Load
    var aLoadMovingAvg = activityByDay.groupAll();
    aReducer = reductio().groupAll(function(record) {
        var idx = mapped_date_array.indexOf(record.dtg.toDateString());
        if (record.dtg < date_array[3]) {
            return [date_array[idx]];
        } else {
            var i = 0;
            var return_array = [];
            for (i = 3; i >= 0; i--) {
                return_array.push(date_array[idx - i]);
            }
            return return_array;
        }
    }).count(true).sum(dc.pluck('Distance')).avg(true)(aLoadMovingAvg);

jsFiddle is here: http://jsfiddle.net/gasteps/hLh5frc8/2/

Thanks so much for any help on this!

G A S
  • 23
  • 5
  • This is what Reductio's `value` is for. Take a look at the example in the documentation here: https://github.com/crossfilter/reductio#aggregations-standard-aggregations-reductio-b-value-b-i-propertyname-i- Of note as well: You should look at using array dimensions (introduced in Crossfilter 1.4 in the community fork) as they will accomplish something quite similar to Reductio's `groupAll` but should be much more efficient - https://github.com/crossfilter/crossfilter/wiki/API-Reference#dimension_with_arrays – Ethan Jewett Nov 17 '17 at 21:24
  • Thanks Ethan. I can now see how we would use value to store multiple aggs but I continue to struggle with how we compute the two MA aggs from separate array dimensions. I can produce an array dimension that lets me compute one or the other, but not both. Here's my [updated jsfiddle](http://jsfiddle.net/gasteps/kykse3ym/3/). The [original fiddle](http://jsfiddle.net/gasteps/hLh5frc8/2/) may give more context. In short, my data has date and distance. From that, I'd like to make a group that has date (key), distance, 3day MA of distance, 9day MA of distance, and ratio of 9day/3day. Possible? – G A S Nov 29 '17 at 19:15
  • Oh, I see. I don't see a good way to compute a single group for moving averages over 2 different periods. I would suggest that you'll have to just compute this on 2 different groups and combine them based on the date key. Look up dc.js fake groups (it's in the FAQ). You would have to use a fake group to combine them. – Ethan Jewett Nov 30 '17 at 17:18

0 Answers0