Average Calculation Error?
malevans opened this issue · 3 comments
I'm tracking my steps and have the following code for the sum and average values over the last 30 days
tracker
searchType: frontmatter
searchTarget: Steps
datasetName: Steps
folder: • Calendar/Journal/Daily
startDate: -30D
endDate: +0D
summary:
template: "Average: {{average(dataset(0))::i}}"
tracker
searchType: frontmatter
searchTarget: Steps
datasetName: Steps
folder: • Calendar/Journal/Daily
startDate: -30D
endDate: +0D
summary:
template: "Last 30 Days: {{sum(dataset(0))::i}}"
That all works well except that the calculated values differ :(
The above code results in:
Average: 6213
Last 30 Days: 142906
One (or both) of those values is wrong (unless I missed the memo about a complete change in basic maths.. LOL).
If the average value is correct the corresponding Last 30 Days should be 186,390 not 142906.
Or if the sum value is correct, the corresponding Average should be 4763, not 6213.
Am I missing something here?
Do you have data for every day? I think it might have issues if you miss a day (so if there are only 29 files it will calculate the average differently than if you had 30). I'll have to double check the code, but it might also be counting "today" differently than expected.
NOTE: Your image didn't come through in your email so I am not able to see what your dataset looks like. In which case, it's possible the below isn't helpful.
I double checked and the average function works as I said. Here's the function:
average: function (dataset, renderInfo) {
// return number
let countNotNull = dataset.getLengthNotNull();
if (!checkDivisor(countNotNull)) {
return "Error: divide by zero in expression";
}
let sum = d3.sum(dataset.getValues());
return sum / countNotNull;
},
So the way you have it configured it says:
- Give me all values in the last 30 days
- Sum the values
- Divide that by the NUMBER OF ENTRIES (not 30 days, but the number of times a non-null value appeared)
So, if in the last 30 days you had 5 entries:
- 123
- 45
- 67
- 89
- 0
Having an entry of 0 is still having an entry. If you have a day where there is no entry at all (or you don't have a file for that day), then it won't count in the average. If you have 30 days worth of files, please make sure every file has a non-blank entry
For the data above, the sum would be 123+45+67+89 = 324
And the average would be 324 / 5 = 64.8 (it's possible to not have this value match exactly as JavaScript/TypeScript treats float value very badly)