Mathew Guilfoyle
2018-Apr-18 10:48 UTC
[R] mgcv::gamm error when combining random smooths and correlation/autoregressive term
I am having difficulty fitting a mgcv::gamm model that includes both a random smooth term (i.e. 'fs' smooth) and autoregressive errors. Standard smooth terms with a factor interaction using the 'by=' option work fine. Both on my actual data and a toy example (below) I am getting the same error so am inclined to wonder if this is either a bug or a model that gamm is simply unable to fit? Any insight or suggestions would be much appreciated. M Example: library('mgcv') set.seed(1) df = data.frame(index=rep(1:10,5), x=runif(50,0,1), subject = as.factor(sort(rep(1:5,10)))) # 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') #random smooth on its own works ok m4 = gamm(x~s(index, subject, bs='fs'), data=df, method = 'REML') #combination of 'fs' smooth and AR term generates the error: "Error in matrix(0, size.cg[i], size.cg[i]) : object 'size.cg' not found" #the grouping term ( |subject ) is redundant in corAR1 as the mgcv documentation indicates that gamm will assume the grouping #from the random smooth term; the same error happens irrespective of whether the grouping is included in corAR1 or not. m5 = gamm(x~s(index, subject, bs='fs'), correlation=corAR1(form=~index|subject), data=df, method = 'REML')