Search code examples
rglmgam

Generalized linear model vs Generalized additive model


I'm trying to follow this paper: Using a data science approach to predict cocaine use frequency from depressive symptoms where they use glm, gam with the beck inventory depression. So I did found a similiar dataset to test those models. However I'm having a hard time with both models. For example I have two variables d64a and d64b, and they're coded with 1,2,3,4 meaning that they're ordinal. Also, in the paper y2 is only the value of 1 but i have also a variable extra (that can be dependent, the proportion of consume)

For the GAM model I have:

b<-gam(y2~s(d64a)+s(d64b),data=DATOS2)

but I have the following error:

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

Meanwhile for the glm, I have the following:

d<-glm(y2~d64a+d64b,data=DATOS2)

I don't know since d64a and d64b are ordinal I have to use factor()?


Solution

  • The error message tells you that one or both of d64a and d64b do not have 9 (nine) unique values.

    By default s(...) will create a basis with nine functions. You get this error if there are fewer than nine unique values in the covariate.

    Check which covariates are affected using:

    length(unique(d64a))
    length(unique(d64b))
    

    and see what the number of unique values is for each of the covariates you wish to include. Then set the k argument to the number returned above if it is less than nine. FOr example, assume that the above checks returned 5 and 7 unique covariates, then you would indicate this by setting k as follows:

    b <- gam(y2 ~ s(d64a, k = 5) + s(d64b, k = 7), data = DATOS2)