Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Average Calculation Error? #315

Open
malevans opened this issue Feb 9, 2024 · 3 comments
Open

Average Calculation Error? #315

malevans opened this issue Feb 9, 2024 · 3 comments

Comments

@malevans
Copy link

malevans commented Feb 9, 2024

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?



@lazyguru
Copy link
Collaborator

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.

@malevans
Copy link
Author

malevans commented Feb 27, 2024 via email

@lazyguru
Copy link
Collaborator

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants