I want to build a model using R, save it and reloading it when required. I am able to save the model and reload it without any problem, however when i use the model along with predict statement, I am getting errors.
This is what i do:
set.seed(345) df <- data.frame(x = rnorm(20)) df <- transform(df, y = 5 + (2.3 * x) + rnorm(20))
m1 <- lm(y ~ x, data = df)
save this model
save(m1, file = "my_model1.rda")
a month later, new observations are available:
newdf <- data.frame(x = rnorm(20))
load the model
When i use the above model to predict for new values, I am getting output values as "NA"
predict(m1, newdata = newdf)
"NA" "NA" "NA" "NA" "NA" "NA" "NA" "NA" "NA"
I could able to see that the model has been loaded correctly by giving a print command
Call: lm(formula = y ~ x, data = df)
Is there anything that I miss here?