[R-sig-ME] mgcv gamm problem with factor-smooth interaction and AR1 errors

Mathew Guilfoyle mrguilfoyle at gmail.com
Mon Apr 23 17:46:25 CEST 2018

I've posted this question to the main R help list without any joy - I wonder if someone here might be able to help...

I'm trying to fit a mgcv::gamm model including random smooths and an autocorrelation term but am getting a consistent error.  I get the same issue with my real data and the toy data in the example below.  As far as I can see the model is specified correctly and the mgcv man/help pages imply this sort of model should be possible.

Any ideas?

Many thanks

#generate some data
df = data.frame(index=rep(1:10,5), x=runif(50,0,1), subject = as.factor(sort(rep(1:5,10))))

##models M1-M4 all fit without issue, but M5 fails

# random intercept
m1 = gamm(x~s(index), random=list(subject=~1), data=df, method = 'REML')

#factor interaction, random intercept, AR errors
m2 = gamm(x~s(index, by=subject), random=list(subject=~1), correlation=corAR1(form=~index|subject), data=df, method = 'REML')

#factor interaction, random intercept and slope, AR errors
m3 = gamm(x~s(index, by=subject), random=list(subject=~index), correlation=corAR1(form=~index|subject), data=df, method = 'REML')

# the 'fs' smooth on its own works ok
m4 = gamm(x~s(index, subject, bs='fs'), data=df, method = 'REML')

#combination of 'fs' smooth and AR errors generates the error: "Error in matrix(0, size.cg[i], size.cg[i]) : object 'size.cg' not found"
m5 = gamm(x~s(index, subject, bs='fs'), correlation=corAR1(form=~index|subject), data=df, method = 'REML')

More information about the R-sig-mixed-models mailing list