No, not the Star Trek episode, sorry.

Apparently there is a Star Trek episode called “Time Squared” which I only know because I’ve been looking to see how other people explain why time series analysts sometimes use both “Time” and “Time Squared” to fit seasonal effects in their models. And most of the initial Google hits are for Star Trek.

(I’m not above placing a little Star Trek graphic here though, just to scratch that itch.)

Derek, Steve and I put a lot of effort into thinking about how to account for season in time series modeling, on the Baikal Dimensions project and many others.

Adding the variables Time (e.g., months 1, 2, 3 …) and Time Squared (e.g. months 1, 4, 9 …) to a linear model is one of the common ways that a seasonal pattern can be accounted for when you are trying to understand the drivers of some phenomenon that varies in at least a semi-predictable way annually. e.g., more bugs in the summer, more leaves falling in the autumn, or more crocuses blooming in the springtime. (Not the band Krokus, they are year-round.)

Let’s say you don’t care about the nature of the seasonality itself in some particular study, but what you really want to know is the effect of predator abundance and temperature on prey. There are lots of ways you could deal with this. One of which is to add Time and Time Squared to the model with Predator Abundance and Temperature to account for variation in Prey Abundance that is only associated with the seasonal trend. And then you really just spend most of your energy thinking about the part of the variation that was explained by Predator Abundance and Temperature.

There are many ways to model this seasonal pattern and remove it, if that’s what you want to do. What I want to talk about here is why it’s so common to throw Time and Time Squared into the model, because I think it seems non-intuitive until you play around with it yourself.

First, why not just Time?

If prey are gradually getting more abundant in the summer (months 6, 7, 8, 9), and gradually less abundant in the winter (months 1, 2, 3, 4, 5, 10, 11,12) then it can’t be accounted for very well by just a linear trend, because months 1 and 12 are very similar.

When you put Time Squared together with Time, and give your model those two variables to fit with whatever 2 coefficients best describe the trend together, you suddenly have a lot of flexible ways to describe a variable’s quadratic relationship with time. (e.g. accounting for that summer or fall hump in prey abundance)

I’m pasting some R code below, so that you can check it out yourself!

Again, there are a lot of ways to account for season, and there are a lot of ways to study the characteristics of season if that is your focus. And I’m not even sure that Time and Time Squared are the best ways.

But if you are staring at Time Squared in a published paper and asking yourself why, I think checking out the R code will help you see why it’s been a popular and simple way to deal with season.

##time and time squared visualization##

#Hampton 21 Aug 2012

time<-seq(1,12)

time2<-time^2

ct <- 9 ##change if you want – coefficient for effect of time

ct2 <- -1.2 ##change if you want – coefficient for effect of timesquared

ctV<-time*ct

ct2V<-time2*ct2

ctV

ct2V

pop <- rep(0,12)

pop

pop[1] <- 1800 ##change if you want – initial starting population

for (i in 2:12) {

pop[i] <- pop[i-1] + ctV[i-1]+ct2V[i-1]

}

pop

par(mfrow=c(2,1))

plot(pop~time)

very interesting – same problems exist in the analysis of circadian cycles – 11pm is very similar to 0 am.

Another common way to do this is to use a sine function, but again that is symmetric within the year. I’ve found that a nice, simple way to model seasonal effects in a time series regression is to use a cyclic spline (cyclic cubic regression spline) for the sub-annual variable (month or day of year of observation) via the

`gam()`

function from the mgcv package (which comes with R). As it is a cyclic spline the end points at the start and end of the year are forced to have continuous first and second derivatives (i.e. they join smoothly). It is then relatively simple to allow the cyclic spline to “interact” with the trend to model a varying seasonal effect through time.The main benefit is that the form of the seasonal signal is identified from the data.

Very nicely explained – thank you for weighing in! 🙂

Pingback: The Next Generation | Lake Baikal Dimensions of Biodiversity