I trained a penalized regression model using R's glmnet package, and X constructed using a sparse.model.matrix with a formula of "~ . * (var1)" to get every term from my data and an interaction with var1:
X3 <- sparse.model.matrix(object = ~.*(var1), data = X)[,-1]
cv_lasso <- cv.glmnet(x = X3, y = Y3,
alpha = 1,
nfold = 10,
family = "binomial",
nlambda = 100,
lambda.min.ratio=0.001,
type.measure="auc",
keep = TRUE,
parallel = TRUE)
Now, I'm trying to predict on a couple of data points, but when converting the newX to a model.matrix to use with predict.glmnet(), like below:
X_pred <- sparse.model.matrix(object = ~.*(var1), data = X_holdout)
predict(object = cv_lasso,
newx = X_pred,
s = "lambda.min")
But I get the following error:
Error in
contrasts<-
(*tmp*
, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels
I believe this might be caused by a couple of columns from X_holdout that are basically constant (which is correct since I'm trying to predict now, I already trained successfully).
How can I avoid this problem? My understanding is that, since I trained my model using interactions, I have to create a model matrix with the same interactions in my predictions.