# [R-sig-ME] gamm error/problem fitting a smooth by factor

Ben Bolker bbolker at gmail.com
Mon Dec 26 20:33:06 CET 2016

``` I think (others may correct me) you don't want both s(time) and
s(time,by=fac1) in the same model, these will be redundant.

>From ?s:

In the factor ‘by’ variable case a replicate of the smooth is produced
for each factor level (these smooths will be centered, so the factor
usually needs to be added as a main effect as well).

This suggests to me that you want

> m2 = gamm(v1 ~ fac1 +s(time, by=fac1), random=list(id=~1+time), data=dat)

I would try that and check that the results make sense (plot
predictions, etc etc)

On 16-12-26 02:21 PM, Mathew Guilfoyle wrote:
> I have a (unbalanced) dataset of time series collected across several subjects (n~500, ~60000 observations total).  I would like to model the overall smooth time trend of a variable and how this trend differs by various categorical factors, with the subject as a random effect, using gamm (mgcv).
>
> My baseline model
>
> m1 = gamm(v1 ~ s(time), random=list(id=~1+time), data=dat)
>
> where v1 is a continuous numerical variable, time is measured at irregular subject-specific intervals, id is subject identifier.  Model m1 shows that time is a significant term.
>
> I have then tried to run this model:
>
> m2 = gamm(v1 ~ s(time)+s(time, by=fac1), random=list(id=~1+time), data=dat)
>
> where fac1 is a binary factor.
>
> My intuitive understanding of this is that the first smooth term will capture the overall major trend common to both groups of fac1 and the second smooth will model the deviation from this mean trend for the fac1==1 subgroup.  However, I get the error:
>
> Error in MEestimate(lmeSt, grps) :
>  Singularity in backsolve at level 0, block 1
>
> I've tried other models to isolate the problem but get the exact same error
>
> m3 = gamm(v1 ~ s(time)+s(time, by=fac1), random=list(id=~1), data=dat)   #remove the time random effect
> m4 = gamm(v1 ~ fac1+s(time)+s(time, by=fac1), random=list(id=~1+time), data=dat)   #have the factor as a main effect also
>
> I'm not sure if the whole notion of what I'm trying to do is wrong-headed or if I need to adjust some parameters to get the model (m2) to fit.
>
> Thanks
>
> _______________________________________________
> R-sig-mixed-models at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
>

```