Search code examples
rdistancesamplinggam

"invalid `method'" error when I try the step.Gam function


I have a gam function with many covariates and I would like to simplify it (find the minimum model)

I used a dsm function to model the density of a species across line transcect segments as a function of covariates. And it works fine! But it was the maximum model with too many covariates and I would like to reduce their number automatically. So I tried using the gam::step.Gam function. (I also used the gam.scope function to make sure I do everything correctly).

DSM code:

GamModel = dsm(
  ddf.obj=PreparedDdf, 
  formula = D ~ x + y + Cov1 + Cov2 +...+ Covn factor1+ factor2+...+factorn,
  family=gaussian(link='identity'), 
  group=FALSE,
  engine='gam',
  convert.units=1,
  segment.data=segment.df, 
  observation.data=observation.df
)

step.Gam code:

GamScope=gam.scope(segment.df[,c(5:6,11:16)], response=1, smoother="s", arg=NULL, form=TRUE)
MinModel = step.Gam(GamModel, GamScope, trace=TRUE, direction="backward")

I hoped to get the minimal model, instead it gives me the following error:

Error in gam(formula = D ~ x + Cov1 + Cov2 + Cov3, : invalid `method': REML

And I don't understand why this happens! I tried different methods (GACV.Cp, ML) and I get the same kind of error (invalid method: GACV.Cp etc) Why is this happening? Is it because it's gam model produced by the dsm function? And more importantly, how can I minimize the model automatically??

(When I used engine='glm' instead of 'gam' in the dsm function) and I try the stats::step function to find the minimal model it works, but the results seem a bit scetcy... so I want to use the gam engine)


Solution

  • The gam package doesn't fit models using REML or the other options you state. Those are options to the gam() function in the mgcv package.

    The only allowed options for the method argument in gam::gam() are:

    1. "glm.fit", which is the default, and
    2. "model.frame", which doesn't really do anything as it instructs the function to just spit out the model frame resulting from the formula.

    It is quite important to differentiate between these two packages that both provide a gam() function. They are very different approaches to the estimation of GAMs.

    As you are using dsm(), you'll be fitting using mgcv::gam() not gam::gam() and in that case you cannot apply the gam::step.gam() function to the model.

    I believe that the authors of dsm() recommend that you use the select = TRUE argument to mgcv::gam(), which you can provide when using dsm() and which will get passed on the gam(). This will add extra penalties to the smooth terms in the model such that they can be shrunk out of the model.