Prediction using saved model object

Ravi picture Ravi · Dec 3, 2013 · Viewed 7.8k times · Source

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.

Ravi

Answer

Roland picture Roland · Dec 3, 2013

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")