GAMs with categorical predictors throw errors regarding degrees of freedom & basis dimension

Viewed 14

I have a dataset similar to the one in the code below. The response variable is binary and the two predictor variables are categorical (one is binary and the other has four categories). I have created a candidate set of models, and I want to find the model with the lowest AIC in the candidate set. However, I get two error messages when running the models.

I think the problem is that it is not possible to build a spline due to the small data available or the fewer combinations of categories across the two predictor variables.

Is there a way of analysing my data using GAMs (i.e. overcoming the errors below)?

library(mgcv)

set.seed(123)

# Dummy data
dat <- data.frame(resp = sample(c(0, 1), 280, replace = T, prob = c(0.8, 0.2)),
                  pre1 = sample(c(0, 1), 280, replace = T, prob = c(0.6, 0.4)),
                  pre2 = factor(sample(c("none", "little", "some", "plenty"), 280, replace = T, 
                         prob = c(0.25, 0.25, 0.15, 0.35))))

# Define candidate set of models
m1 <- gam(resp ~ 1, method = "REML", data = dat)
m2 <- gam(resp ~ s(pre1, k = 2), method = "REML", data = dat)

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
In addition: Warning message:
In smooth.construct.tp.smooth.spec(object, dk$data, dk$knots) : basis dimension, k, increased to minimum possible

m3 <- gam(resp ~ s(pre2, k = 2), method = "REML", data = dat)

Error in smooth.construct.tp.smooth.spec(object, dk$data, dk$knots) : 
NA/NaN/Inf in foreign function call (arg 1)
In addition: Warning messages:
1: In mean.default(xx) : argument is not numeric or logical: returning NA
2: In Ops.factor(xx, shift[i]) : ‘-’ not meaningful for factors
3: In smooth.construct.tp.smooth.spec(object, dk$data, dk$knots) : basis dimension, k, increased to minimum possible

m4 <- gam(resp ~ s(pre1, k = 2) + s(pre2, k = 2), method = "REML", data = dat)
m5 <- gam(resp ~ s(pre1, k = 2) * s(pre2, k = 2), method = "REML", data = dat)

# Calculate AICs
AIC(m1, m2, m3, m4, m5)
0 Answers
Related