When building a model in R, how do you save the model specifications such that you can reuse it on new data? Let\'s say I build a logistic regression on historical data but
If you use the same name of the dataframe and variables, you can (at least for lm() and glm() ) use the function update on the saved model :
Df <- data.frame(X=1:10,Y=(1:10)+rnorm(10))
model <- lm(Y~X,data=Df)
model
Df <- rbind(Df,data.frame(X=2:11,Y=(10:1)+rnorm(10)))
update(model)
This is off course without any preparation of the data and so forth. It just reuses the model specifications set. Be aware that if you change the contrasts in the meantime, the new model gets updated with the new contrasts, not the old.
So the use of a script is in most cases the better answer. One could include all steps in a convenience function that just takes the dataframe, so you can source the script and then use the function on any new dataset. See also the answer of Gavin for that.