How to properly `dput` install a linear model (` lm`) in an ASCII file and recreate it later?

I want to save an object lmin a file and reload it into another program. I know that I can do this by writing / reading the binary via saveRDS/ readRDS, but I want to have an ASCII file instead of the binary. On a more general level, I would like to know why my idioms for reading to dputoutput do not behave at all as I expected.

The following are examples of a simple fit and successful and unsuccessful model recreation:

dat_train <- data.frame(x=1:4, z=c(1, 2.1, 2.9, 4))
fit <- lm(z ~ x, dat_train)
rm(dat_train) # Just to make sure fit is not dependent upon `dat_train existence`

dat_score <- data.frame(x=c(1.5, 3.5))

## This works (of course)
predict(fit, dat_score)
#    1    2 
# 1.52 3.48

Saving a binary file:

## http://stackoverflow.com/questions/5118074/reusing-a-model-built-in-r
saveRDS(fit, "model.RDS")
fit2 <- readRDS("model.RDS")
predict(fit2, dat_score)
#    1    2 
# 1.52 3.48

It does this ( dputit is not for the file in the R session):

fit2 <- eval(dput(fit))
predict(fit2, dat_score)
#    1    2 
# 1.52 3.48

But if I save the file to disk, I cannot figure out how to return to normal form:

dput(fit, file = "model.R")
fit3 <- source("model.R")$value

# Error in is.data.frame(data): object 'dat_train' not found

predict(fit3, dat_score)
# Error in predict(fit3, dat_score): object 'fit3' not found

Trying to be explicit with help evaldoesn't work either:

## http://stackoverflow.com/questions/9068397/import-text-file-as-single-character-string
dput(fit, file="model.R")
fit4 <- eval(parse(text=paste(readLines("model.R"), collapse=" ")))

# Error in is.data.frame(data): object 'dat_train' not found

predict(fit4, dat_score)
# Error in predict(fit4, dat_score): object 'fit4' not found

, , fit3 fit4 , lm, predict().

- , structure(...) ASCII- , lm, predict()? ?

+7
2

1:

:

dput(fit, control = c("quoteExpressions", "showAttributes"), file = "model.R") 

?.deparseOpts.


"quoteExpressions" // quote, , . :

  • source ;
  • call "lm" - :

    fit$call
    # lm(formula = z ~ x, data = dat_train)
    

, "quoteExpressions" R lm . , , R dat_train, R.


"showAttributes" - , "lm" . , , "", ? , "lm" , model ( ), qr ( QR-) terms ( ) .., . .


control, :

control = c("keepNA", "keepInteger", "showAttributes")

. , "quoteExpressions", .

"keepInteger" "keepNA", "lm" .

------

2:

source . :

fit1 <- source("model.R")$value

, summary predict. ?

- terms fit1 "", ( , "" ""!). fit$terms fit1$terms, . ; "quoteExpressions". call, terms. terms.

, :

fit1$terms <- terms.formula(fit1$terms)

fit$terms (, , ), "terms".

""? . , , , .

, predict ( summary):

predict(fit1)  ## no `newdata` given, using model frame `fit1$model`
#   1    2    3    4 
#1.03 2.01 2.99 3.97 

predict(fit1, dat_score)  ## with `newdata`
#   1    2 
#1.52 3.48 

-------

:

, , . "lm" , , , residuals, fitted.values , qr model / . .

+13

!

, $terms . terms.formula OP, bs() poly():

dat <- data.frame(x1 = runif(20), x2 = runif(20), x3 = runif(20), y = rnorm(20))
library(splines)
fit <- lm(y ~ bs(x1, df = 3) + poly(x2, degree = 3) + x3, data = dat)
rm(dat)

:

dput(fit, control = c("quoteExpressions", "showAttributes"), file = "model.R") 
fit1 <- source("model.R")$value
fit1$terms <- terms.formula(fit1$terms)

, summary.lm anova.lm , predict.lm:

predict(fit1, newdata = data.frame(x1 = 0.5, x2 = 0.5, x3 = 0.5))

bs (x1, df = 3): "bs"

, ".Environment" $terms .

environment(fit1$terms) <- .GlobalEnv

predict, :

poly (x2, degree = 3):

'degree'

, "predvars" / bs() poly().

, dput :

dput(attr(fit$terms, "predvars"), control = "quoteExpressions", file = "predvars.R")

attr(fit1$terms, "predvars") <- source("predvars.R")$value

predict .

, "dataClass" $terms , - .

+2

Source: https://habr.com/ru/post/1014073/


All Articles