glmnet requires the exact number / names of variables from the training data set, which should be in the test / test set. For instance:
library(caret) library(glmnet) df <- ...
For a completely new dataset, you can limit the new df to the necessary variables using some variant of the following method:
new.df <- ...
In addition, glmnet only works with matrices. This is probably why you get an error message that you post in a comment on your question. Some users (including me) have found that as.matrix() does not solve the problem; data.matrix() seems to work though (hence why this is in the above code). This issue is addressed in a stream or two in SO.
I assume that all the variables in the new dataset that should be predicted should also be formatted in the same way as in the dataset used to develop the model. Usually I retrieve all my data from a single source, so I have not come across what glmnet will do in cases where the formatting is different.
source share