I have count data of a species and would like to analyze the population trend over the years. The species was count twice a year (early summer and late summer --> column "season") for several years on different sites. The model I have set up is the following:

gam(count ~ s(site, bs = "re") + s(year) + season, data=mydata, family = nb)

If I want to include a correlation structure

gamm(count ~ s(site, bs = "re") + s(year) + season, data=mydata, family = nb, correlation = corAR1(form=~year|site))

I get an error since I have two observations for each year and site. The solutions I can think of are:

  1. Limit the analysis to one season -> only one count per year and site
  2. Sum the counts per year and site -> only one count per year and site

I don't like the first one since I use only half of my data but I also think that it might be the only way since I know that higher counts in early summer lead to higher counts in late summer - does this mean my data is not independent?

What is the best way to analyze this data?

Thank you very much for your help!


1 Answer 1


For this I think you could just specify a variable that contains the time ordering, rather than year and nest that within site.

So you want a variable time (say) that is the integers 1, 2, 3, ..., T in site 1, and again in site 2 and so on. This would be pretty straight forward to code in R if you have no missing time points in any of the sites. It needs to be coded (integers) in each site the same I think because you'll have the same correlation estimated for each site so we want time 1 in site A to match time 1 in site B.

Once you have that variable, the correlation argument would then be corAR1(form = ~ time | site)

  • $\begingroup$ Thank you, that's exactly what I was looking for! $\endgroup$
    – Schnipfi
    Commented Nov 26, 2020 at 6:30
  • $\begingroup$ @GavinSimpson, do the integers need to start over with each new day? For example, if Day 1 had 6 samples, its timestamp variable would be 1:6 (1st - 6th time stamp), Day 2 with 9 samples and timestamps would be 1:9, etc? $\endgroup$
    – Nate
    Commented Feb 11, 2023 at 17:38
  • 1
    $\begingroup$ @Nate in form = ~ x | y, x should order the data within values of y, x must be unique within y, and x = j must be the same jth time point in each y. $\endgroup$ Commented Feb 12, 2023 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.