Is there a way to get a prediction from an gam model (from package mgcv) that contains random effect smooths, where the new data contains a level of the random effect that didn't exist in the training set? Something comparable to the "re.form=NA" parameter in lme4:predict.merMod ?
library(hflights) # get a big data set
hflights$UniqueCarrier <- factor(hflights$UniqueCarrier) # turn carrier into a factor
newhflights <- head(hflights) # create "new" d.f. for prediction
newhflights$UniqueCarrier <- "ZZ" # change carrier to a previously unseen value
library(mgcv)
m_gam <- gam(ActualElapsedTime ~ s(Distance) + s(TaxiOut, UniqueCarrier, bs="re"), data=hflights)
predict(m_gam, newdata=newhflights) # fails with "ZZ not in original fit"
library(lme4)
m_lmer <- lmer(ActualElapsedTime ~ Distance + (TaxiOut | UniqueCarrier), data=hflights)
predict(m_lmer, newdata=newhflights) # fails with "new levels detected in newdata"
predict(m_lmer, newdata=newhflights, re.form=NA) # works
I have searched for an answer and found this question, but it didn't appear to be directly related to my problem. Predicting with random effects in mgcv gam But I did try adding a dummy variable to the model based on it.
hflights$dummy <- 1
newhflights$dummy <- 0
m_gam <- gam(ActualElapsedTime ~ s(Distance) + s(TaxiOut, UniqueCarrier, bs="re", by=dummy), data=hflights)
predict(m_gam, newdata=newhflights) # still doesn't work, not in original fit
I'm out of ideas. Am I overlooking something obvious?