# What are ways to deal with circular covariates (e.g. with a GAM)?

I'm building a model in which several of my covariates live on a "circle", in the sense that they take values in the interval [0,1), and 0=1. I'm wondering about techniques for dealing with this situation. One idea is to represent a circular variable theta as a pair of variables ( sin(theta), cos(theta) ). Any thoughts on this approach or better approaches?

I'm specifically using the mgcv package GAMs. Is there a way to tell the model that certain additive pieces should have the same values at the endpoints? Another package?

Thanks!

• I've only ever seen sine and cosine as you have mentioned, here is another question on the site that asks a similar question, Logistic regression with directional data as IV. In this question over on SO Iterator has a comment pointing to a circular package that may possibly be of interest. Hopefully you get better responses though. Oct 4 '11 at 17:22
• @Andy sines and cosines are not the only basis, nor are they even the only orthogonal basis, for the (square-integrable) periodic functions. One of their merits is that often physical theory implicates them in relationships with other variables. This suggests we should be acutely interested in the hypothesized or expected relationship between the DV and these covariates. DavidR, what can you tell us about this?
– whuber
Oct 4 '11 at 18:17
• I'm trying to model a periodic temporal process. My original data are the date and time of events, and I'd like to model the rate of these events over time. I expect there to be periodicity with respect to hour-of-day, day-of-week, and time-of-year, for starters. These are the obvious circular variables. I am starting with a Poisson-GAM. I am interested in examining the effects of each of these separately, as well as making rate predictions for future times. Oct 4 '11 at 20:48

There are two ways of dealing with circular variables, one hacky method would be to manually duplicate your data set on either side of the boundary conditions but the more elegant solution I think would be to use the built-in spline basis functions with periodic boundary conditions !

For example:

bs="cc" specifies a cyclic cubic regression splines (see cyclic.cubic.spline). i.e. a penalized cubic regression splines whose ends match, up to second derivative.

Splines on the sphere

bs="sos". These are two dimensional splines on a sphere. Arguments are latitude and longitude, and they are the analogue of thin plate splines for the sphere. Useful for data sampled over a large portion of the globe, when isotropy is appropriate. See Spherical.Spline for details.

bs="cp" gives a cyclic version of a P-spline

• That sounds perfect! I'll try this. I had thought about the hacky method, but since I have multiple circular variables, I was thinking I'd have to make a huge number of duplicate data points to show all the different symmetries in the problem. Oct 5 '11 at 12:59
• @DavidR I have had good success using circular cubic splines for low-frequency phenomena. You need enough to represent the highest frequency you wish to model. That's going to rule out the hourly and daily periods, but might work well for the seasonality. For the former two, parsimony suggests starting with a small basis such as a sine and cosine for each expected frequency.
– whuber
Oct 5 '11 at 15:41
• @whuber, I think you misunderstood DavidR s modelling approach. David is using separate variables for hour, day of week etc. So he would apply a separate spline for each. (Would welcome your thoughts as am doing something similar) Jan 7 '17 at 1:12
• Whuber, I agree with your point that eg modelling heavier shopping in December would require a high order spline on months variable. Jan 7 '17 at 1:21
• If I have a model with a tensor spline smooth te(longitude, latitude, time), is there then any way to specify that longitude and latitude but not time should then be circular? Would that be with argument bs=c("cp","cp","ps")? Oct 19 '21 at 18:49

You might want to look into Gill and Hangartner (2010). Circular Data in Political Science and How to Handle It. They talk about various models for circular/clock/seasonal data, and Jeff Gill provides R code for the paper which you can look into for inspiration. There should be a presentation version of this material which would weave the methodology and R code together.

• Thanks for this pointer into the literature on circular data. It seems like a good starting point. I quickly skimmed this particular article, and it seems to deal with circular responses, rather than circular covariates. Oct 4 '11 at 21:07