Using update() function in conjunction with gamm4() in mgcv package

28 Views Asked by At

The update() function of R does not work with objects returned from the gamm4() function of the mgcv package.
Question: *Does anyone know of an equivalent way of updating gamm4 objects?
*

I have found a function called "uGamm" in the MuMIn package that is supposed to do this, but it does not seem to work either. Here is some example code:

This returns a gamm4 object:

temp<-gamm4::gamm4(X4~X3,random=~(1|group),data=sim_normal.with.nesting,family=gaussian)

#Now, run update:

update(temp,~.+X1)

The following error message is returned:

Error in update.default(temp, ~. + X1) : need an object with call component

#If I run uGamm, this is what happens:

uGamm(temp)

Error in gf[[1]][2] : object of type 'S4' is not subsettable

1

There are 1 best solutions below

0
Gavin Simpson On

update() doesn't work because the object returned by gamm4::gamm4() (and mgcv::gamm() for that matter) is a list with the two faces of the GAMM

  1. a $lmer or $lme component for the mixed model face, and
  2. $gam for the GAM face

This object in an of itself doesn't have a call component and this can't be updated.

I think the intended use is that you fit the GAMM with uGamm() (not mgcv::gamm() or gamm4::gamm4()) and that the resulting object has a call that can be updated:

set.seed(0) 
dat <- gamSim(6, n = 100, scale = 5, dist = "normal") 
    
fmm1 <- uGamm(y ~ s(x0) + s(x2) + s(x3),
  family = gaussian, data = dat,  
  random = list(fac = ~1))

# update to remove f(x3)
fmm2 <- update(fmm1, ~ . - s(x3))

Results in:

> fmm2$gam

Family: gaussian 
Link function: identity 

Formula:
y ~ s(x0) + s(x2)

Estimated degrees of freedom:
1.00 4.11  total = 6.1

(Not that I think this kind of model selection is that useful; use select = TRUE with gam() for example...)