13

Given a dataset of months, how do I calculate the "average" month, taking into account that months are circular?

months = c(1,1,1,2,3,5,7,9,11,12,12,12)
mean(months)
## [1] 6.333333

In this dummy example, the mean should be in January or December. I see that there are packages for circular statistics, but I'm not sure whether they suit my needs here.

David C.
  • 1,974
  • 2
  • 19
  • 29
generic_user
  • 3,430
  • 3
  • 32
  • 56

1 Answers1

17

I think

months <- c(1,1,1,2,3,5,7,9,11,12,12,12)
library("CircStats")

Now convert from months to radians, compute the circular mean, and convert back to months. I'm subtracting 1 here assuming that January is at "0 radians"/12 o'clock ...

conv <- 2*pi/12 ## months -> radians
(res1 <- circ.mean(conv*(months-1))/conv)

The result is -0.3457. You might want:

(res1 + 12) %% 12

which gives 11.65, i.e. partway through December (since we are still on the 0=January, 11=December scale)

I think this is right but haven't checked it too carefully.

For what it's worth, the CircStats::circ.mean function is very simple -- it might not be worth the overhead of loading the package if this is all you need:

function (x) 
{
    sinr <- sum(sin(x))
    cosr <- sum(cos(x))
    circmean <- atan2(sinr, cosr)
    circmean
}

Incorporating @A.Webb's clever alternative from the comments:

 m <- mean(exp(conv*(months-1)*1i))
 12+Arg(m)/conv%%12  ## 'direction', i.e. average month
 Mod(m)              ## 'intensity'
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • 2
    `12+Arg(mean(exp(conv*(months-1)*1i)))/conv%%12`, equivalently – A. Webb Sep 04 '15 at 19:15
  • That's a clever formula. I wonder if that equation can be used for determining means for bimodal data? – Chris Sep 04 '15 at 19:25
  • In the wiki article about this subject it is written " The resulting radius will be 1 if all angles are equal. If the angles are uniformly distributed on the circle, then the resulting radius will be 0, and there is no circular mean." How do we calculate the 'radius' to use that as an indicator of the strength of the mean? Source: https://en.wikipedia.org/wiki/Mean_of_circular_quantities – Chris Sep 04 '15 at 19:37
  • 2
    @Chris Just `Mod` instead of `Arg`: `Mod(mean(exp(conv*(months-1)*1i)))` – A. Webb Sep 04 '15 at 19:57
  • Http://www.inside-r.org/r-doc/base/Mod included some links – Chris Sep 04 '15 at 22:12