Calculate mean of calendarDuration? (i.e. mean of age)
11 visualizzazioni (ultimi 30 giorni)
I have an array of calendarDuration data, formatted as (for example) 18y 5mo 21d.
The data is the ages of participants when I tested them. I subtracted their birth date from the testing date, using caldiff.
I'd like to calculate the mean age of my participants. The obvious step would be to do something like:
Where ages is a calendarDuration array (e.g. a 1 x 33 calendarDuration array with the data I'm using).
When I try this, I get the error
Error using sum
Invalid data type. First argument must be numeric or logical.
Error in mean (line 127)
y = sum(x, dim, flag) ./ mysize(x,dim);
What am I missing? For example, is there some different mean function I'm supposed to use for calendarDuration arrays? I couldn't find anything about this when I searched. The nearest I found was a description of timeseries means:
These use the built-in matlab 'mean' function. Which is about what I'd expect - adapting the built-in function so that it detects homogenous data type and operates accordingly would seem so useful that it would have been added long ago. But perhaps not for calendarDuration data, as per my example?
P.S. If anyone can tell me how to get the mean of calendarDuration data, could you tell me how to get standard deviation as well?
Eric Sofen il 8 Feb 2019
You probably want to calculate the ages of the participants as durations, and mean() is defined for duration. calendarDuration is useful for representing elapsed time in calendar units so that vagaries like the number of days in a month get handled automatically. However, a lot of math on calendarDurations is ill-defined, because the length of a calendar duration isn't determined in terms of absolute time until the calendarDuration is "anchored" to a datetime (by adding it to that datetime). A calmonth could be anything from 28 to 31 days and with daylight savings time, a calday may be 23, 24, or 25 hours. Thus, what is mean([calmonths(3), caldays(5)])? calmonths and caldays are separate calendar units and you cannot do arithmetic combining them, which is why they're represented separately in your example 18y 5mo 21d.
Durations, in contrast, represent absolute elapsed time. Subtracting two datetimes (not using caldiff) gives a duration, and you can take the mean of durations.
>> births = datetime(1970,1,1)+10*years(randn(5,1))
5×1 datetime array
>> testDate = datetime+days(randn(5,1))
5×1 datetime array
>> meanAge = mean(testDate-births)
>> meanAge = years(mean(testDate-births))