# Correct way to account for repeated measures and spatial autocorrelation in GAMM (R)

I am using the gamm function in the mgcv package in R to specify a model that predicts abundance with respect to elevation and year based on repeated measures from several sites. My overarching question is how abundance changes over elevation, knowing and accounting for the fact that it also changes over time. GAMM is used because I do not want to restrict the relationship of either elevation or year with abundance to be linear. I expect the data to be spatially autocorrelated within years (sites closer together will have more similar values than sites further apart within each year) and would like to account for this.

I'm having trouble figuring out the proper full formulation of the GAMM.

The variables are: 1) abundance (continuous response) 2) elevation (numeric, continuous predictor) 3) year (numeric, continuous predictor as we expect a gradual change in abundance over time) 4) spatial autocorrelation term (based on the lat/long of the sites where data were collected) 5) site (random factor)

The model formulation I have come up with is:

model <- gamm(abundance ~ s(elevation, bs = "tp")+s(year, bs = "tp"),
correlation=corSpher(form = ~ y+x|year),
random=list(site=~1), data=input.data)


My questions are:

Is the repeated measures nature of the data adequately accounted for here or do I also need to include year in my random effects (nest site in year)?

Have I properly taken year into account in my spatial autocorrelation term?

Finally, when running this model, I get an error and a warning:

Error in corFactor.corSpatial(object):
Na/NaN/Inf in foreign function call (arg 1)

• If this could be illustrated with data it would be more susceptible to investigation. I'm guessing a poorly populated stratum in the RE specification is the cause, ... who knows? Since you cross posted to wrong R-project list, I would wait a couple of days and repost to the mixed models mailing list in plain text if you don't get an answer to this data-free question. Better yet: Post link to data, or failing that, see if you can mangle one of the datasets that come with mgcv by stripping out some large fraction of cases in one of the RE variables to see if you can make that error occur. – DWin Feb 27 '19 at 4:34