Search code examples
rbinarydata-modelinggam

How to make GAM work with binary variables?


I am running this piece of code:

model.runs <- BIOMOD_Modeling(run.data,
                                models =  models,
                                NbRunEval = runs.CV,
                                DataSplit = 80,  
                                VarImport = 20,
                                SaveObj = T,
                                Yweights = NULL,
                                rescal.all.models = FALSE,
                                do.full.models = FALSE,
                                models.options = BIOMOD_ModelingOptions(
                                  GAM = list(k=3),
                                  MAXENT.Phillips = list(path_to_maxent.jar = "F:/xxx/xxx/")))
  save(model.runs, file = "model.runs")

and I had this error:

Error in smooth.construct.tp.smooth.spec(object, data, knots) :
  A term has fewer unique covariate combinations than specified maximum 
degrees of freedom

After some research, I understood that GAM did not like my binary variables, so I took them out, and it worked fine.

Therefore, my question is simple: I would like to keep my environmental variables, is this doable in some way?

Sorry if it is trivial, but I do not use GAM often and I did not find the answer anywhere else.

Cheers.


Solution

  • You can’t smooth binary or categorical variables, only continuous ones.

    You can create and interaction between a smooth and a categorical variable, and you could use random effects “smooths” for categorical variables. But you can’t just smooth binary or categorical variables. You would need to arrange for biomod to include those variables as linear factor terms. If you codes them as 0,1 then R, biomod, and mgcv will think those variables are numeric. Make sure they are coerced to be factors and then retry.