I would like to use a generalized additive model to investigate time-series data in R. My data are monthly and I would like to estimate a seasonal effect and a longer run trend effect. I have followed some helpful posts by Gavin Simpson here and here:
My data look like this:
I have the full data set available on my github page:
I have attempted to specify a generalized additive model with smooth seasonal and trend terms as follows:
df <- read.csv('trips.csv')
head(df)
# A tibble: 276 × 2
date ntrips
<date> <int>
1 1994-01-01 157
2 1994-02-01 169
3 1994-03-01 195
4 1994-04-01 124
5 1994-05-01 169
#add a time column
trips <- tbl_df(trips) %>% mutate(time=as.numeric(date))
mod1 <- gamm(ntrips~s(month,bs="cc",k=12) + s(time),data=trips)
I extracted the estimate of the seasonal effect as follows:
pred <- predict(mod1$gam,newdata=trips,type="terms")
seas <- data.frame(s=pred[,1],date=trips$date)
ggplot(seas,aes(x=date,y=s)) + geom_line()
This plot is included below:
My question is: in the original data the seasonal peaks move around a little from year to year. In the embarassingly simple GAM I have specified the seasonal effect is constant. Is there a way to accommodate time varying seasonality with a GAM?
I have analyzed these data using the STL approach of Cleveland et al.:
Using the STL paradigm, how wiggly or smooth one allows the seasonal effects to be seems to be a matter of preference or choice. I would prefer if I could allow the data to tell me the difference between random error and a shifting seasonal peak. GAMS seem better suited to this goal as they lend themselves more readily to statistical model fitting-type exercises...but I would like to know if there is a parameter in the R package for fitting gams that allows time varying seasonal effects.