Is it possible to read out the coefficients of linear model instead of training with data in R?

46 views Asked by At

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

  1. 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.

  1. 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?

2

There are 2 answers

1
Sola Cong Mou On

try

    # saving the model
    saveRDS(lmod, file = "model.rda")

    #loading the model
    model_old = readRDS("model.rda")

then you will get:

model_old
    Call:
    lm(formula = z ~ x + y + I(x^2), data = df)

    Coefficients:
   (Intercept)            x            y       I(x^2)  
         8.825      -12.937        2.625        1.437 
0
Noskario On

I ended up writing my own class (S3-class, I think) to solve this:

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)+x:y, df)
co <- coefficients(lmod)

create_loaded_model <- function(coeffs, paramToReconstruct){
  loaded_model <- list(
    formula=formula(paste(paramToReconstruct, "~", paste(setdiff(names(coeffs), "(Intercept)"), collapse = " + "))),
    coefficients = coeffs
  )
  class(loaded_model) <- "loaded_lm"
  loaded_model
}

predict.loaded_lm <- function(model, newdata){
  fm <- model$formula
  co <- model$coefficients
  model.matrix.lm(fm, data = newdata, na.action="na.pass") %*% co |> as.numeric()
}
loaded_model <- create_loaded_model(co, "z")
predict(loaded_model, df)
predict(lmod, df)

This still has some flaws, like I rely on the fact that the first entry of coeffs is named "(Intercept)", otherwise the model fails, but it is sufficient for my purpose.