I have the following situation:
I have some data and train some linear model on it (potentially with some interaction terms). I save the coefficients of this model, so other programs than R can also use it.
Is there a way now, some weeks later, to read out my model only from the coefficients?
I would imagine something like
- step: Create the model
df <- data.frame(x=1:5, y=c(1,6,8,11,9), z=c(1,2,5,10,3))
lmod <- lm(z~x+y+I(x^2), df)
predict(lmod, newdata = df)
co <- coefficients(lmod)
co
This gives the predictions -0.05 4.45 3.95 8.95 3.70 and the coefficients
(Intercept) x y I(x^2)
8.8250 -12.9375 2.6250 1.4375
I would then save co to a text-file or something like that.
- step: I read out those coefficients and do something like that:
fm <- formula(paste("z ~", paste(setdiff(names(co), "(Intercept)"), collapse = " + ")))
model <- lm(formula = fm, data = NULL)
model$coefficients <- co
predict(model, newdata = df)
and hope to get meaningful results. This approach does not work however, what would be a good way to do it?
try
then you will get: