# Measure of central tendency for periodic variable (hour of day)

I have a dataset that shows, for each group, the number of times a certain action was completed during each hour of the day.

GROUP   0   1   2   3   4   ... 21  22  23
A       0   0   1   3   1   ... 2   0   0
B       0   0   2   2   4   ... 0   0   0
C       3   1   0   0   0   ... 0   0   4
D       4   5   0   0   0   ... 0   0   9
...


I'm trying to find a median (or some other measure of central tendency) for each group, but I'm not sure how to do this with a periodic variable. When many actions occur during hours 0 and 23 (as in groups C and D), for example, it's because the actions are occurring at night. These actions are clustered, but based on the variable coding, it appears as though there's no cluster of activity for these groups.

What methods exist for dealing with this type of data? I'm having trouble finding anything when using the "periodic variable" search term.

• Look under the name in the tag: "circular statistics". There's an R package for that, for example. Mar 26 '16 at 0:51

As @Wayne commented, this is a problem in circular statistics. Consider a toy example with times at 23, 24, 1, 2, 3 hours. An intuitive mean here is 1 hour, and it should be clear that a conventional mean of 53/5 = 10.6 hour is absurd.

The vector mean is the mean in circular statistics. It is the arctangent of the sum of sines over the sum of cosines, where each sine and cosine corresponds to an angle on the circle. 24 hours $=$ 360 degrees, but most software expects angles to be in radians (2$\pi$ radians = 360 degrees), so you might as well go straight there.

This token calculation is cavalier about the arctangent function, because the answer is constructed to emerge simply and directly. For real work, find a circular statistics package in your favourite software. (Or abandon your favourite software if there isn't one.)

Here I use Mata in Stata. :* is elementwise multiplication. pi() yields $\pi$.

: times = (23,  24, 1, 2, 3)

: angles = times :* pi()/12

: s = sum(sin(angles)); c = sum(cos(angles))

: (12/pi()) * atan(s/c)
1


In your case repeated times need to be repeated, or some software supports counts as weights.

Medians can be defined in circular statistics, but a little awkwardly.

• Thank you both, Nick and @Wayne. This is incredibly helpful. For the weighted mean, is it correct to do : s = sum(weights * sin(angles)); c = sum(weights *cos(angles)), where weights is a vector of the count of times? It also looks like the circular R package will be useful here. Mar 28 '16 at 13:59
• Yes, that's correct. Mar 28 '16 at 17:20
• Thanks again. For posterity, the R code for this is: times <- c(21, 22, 23, 0) ; output_angle <- mean.circular(circular(x = times * (2*pi/24), modulo="2pi", units="radians")) ; as.numeric(output_angle) * 24/(2*pi) where mean.circular is a function from the circular R package. Mar 28 '16 at 21:03