obsidian-tracker icon indicating copy to clipboard operation
obsidian-tracker copied to clipboard

Average Calculation Error?

Open malevans opened this issue 1 year ago • 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?



malevans avatar Feb 09 '24 23:02 malevans

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.

lazyguru avatar Feb 27 '24 01:02 lazyguru

Can confirm I have a complete dataset as you can see from this: [image: image.png]

I will need to go back and add the individual values together but it appears that the 7 day total is about right but the 30 day total is much lower than it should be.

Here is my code for the above rendering. I can't see a problem but if I've got something in error please let me know.

searchType: frontmatter
searchTarget: Steps
datasetName: Steps
folder: • Calendar/Journal/Daily
startDate: -30D
endDate: +0d
fixedScale: 1.0
line:
    fillGap: true
    yAxisLabel: Steps
    yMin: 0
    yMax: 12000
    xAxisLabel: ""
    lineColor: "#db5c97"
    showPoint: false
    xAxisColor: "#C4B1E6"
    yAxisColor: "#C4B1E6"
    yAxisLabelColor: "#C4B1E6"
searchType: frontmatter
searchTarget: Steps
datasetName: Steps
folder: • Calendar/Journal/Daily
startDate: 2024-01-01
endDate: 2024-12-31
summary:
    template: "Max.: {{max(dataset(0))::i}}"
searchType: frontmatter
searchTarget: Steps
datasetName: Steps
folder: • Calendar/Journal/Daily
startDate: 2024-01-01
endDate: 2024-12-31
summary:
    template: "Min.: {{min(dataset(0))::i}}"
searchType: frontmatter
searchTarget: Steps
datasetName: Steps
folder: • Calendar/Journal/Daily
startDate: -30D
endDate: +0D
summary:
    template: "Average: {{average(dataset(0))::i}}"
searchType: frontmatter
searchTarget: Steps
datasetName: Steps
folder: • Calendar/Journal/Daily
startDate: -7D
endDate: +0D
summary:
    template: "Last 7 Days: {{sum(dataset(0))::i}}"
searchType: frontmatter
searchTarget: Steps
datasetName: Steps
folder: • Calendar/Journal/Daily
startDate: -30D
endDate: +0D
summary:
    template: "Last 30 Days: {{sum(dataset(0))::i}}"

On Tue, Feb 27, 2024 at 11:34 AM Joe Constant @.***> wrote:

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.

— Reply to this email directly, view it on GitHub https://github.com/pyrochlore/obsidian-tracker/issues/315#issuecomment-1965632341, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACOXZNPIRFDWGY4NW3NNLK3YVUZ3FAVCNFSM6AAAAABDCGZEPCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNRVGYZTEMZUGE . You are receiving this because you authored the thread.Message ID: @.***>

-- Cheers Mal Evans

malevans avatar Feb 27 '24 10:02 malevans

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)

lazyguru avatar Mar 22 '24 05:03 lazyguru