Search code examples
rlogistic-regressiongam

Issue with gam function in R


I'm trying to fit a generalized additive logistic regression model, but I'm getting a strange error:

gam_object = gam(event ~ s(time) + ., data = lapse_train, family = "binomial") 

Error in terms.formula(gf, specials = c("s", "te", "ti", "t2")) : '.' in formula and no 'data' argument

Why would it be telling me there is no data argument here when there obviously is?


Solution

  • Note that the error message is from a call to terms.formula() which is called inside the function. This function doesn't see the data= parameter you passed to gam().

    If you check out the ?formula.gam help page you'll see that

    The formulae supplied to gam are exactly like that supplied to glm except that smooth terms, s, te, ti and t2 can be added to the right hand side (and . is not supported in gam formulae).

    You could expand the formula before passing it to gam via the standard terms() function. For example

    gam_object <- gam(terms(event ~ s(time) + ., data=lapse_train), 
        data = lapse_train, family = "binomial") 
    

    You didn't supply any sort of reproducible example so it's not possible to verify that this will work for you.