Ravi Ravi - 1 year ago 170
R Question

Prediction using saved model object

I am trying to use predict function in R using a model saved earlier. The model was created and saved using the following code:

lrModel1 <- glm(response ~ .,data = modelData,family = binomial,model = TRUE)
save(lrModel1,file = "lrModel100.rda")

When I load the model for later use as follows and try to use the predict function on it as follows:

bar <- load("lrModel100.rda")
predicted <- predict(bar,validationData,type = "response")

I get the following error:

Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "character"

Is there a way to get the model object name from the saved RDA file and use it for prediction?

Thank you.


Answer Source

As @droopy told you the model's name doesn't change if you save and load. You can use get to use the model:

predicted <- predict(get(bar),validationData,type = "response")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download