0

Is there an R package/class/code that will deparse the output object from lm or glm into text (or JSON/XML/Other MarkUp) and can also parse it back into an object with the same structure as the original (i.e. so code can query the new object in the same way as the original)?

To complicate matters, I want to be able to do the same with output from the segmented package. So I am looking for something that can read the syntax of any parsed list, including the attr, into the originally structured list object with attr. But a solution to the single lm would be a good starting point.

So far I have tried:

  1. toJSON //does not support R type 6)//
  2. toString then as.list //gives a list with all in one item and escape symbols prevent parsing//
  3. deparse(str(summary(OUTPUT))) //No error but returns NULL//
  4. deparse(OUTPUT) then as.list //gives a list with one item per top level but sublevels are just in a string.
  5. deparse (OUTPUT) then parse directly //Error, must be a string or connection//
  6. passing the string output to a new structure() object //Same Result as 4 except not recognised as list by rstudio//
  7. Exporting the summary metrics to a data frame and then to JSON //Keeps the coeffs but loses other information//

I wish to avoid having to make any assumptions about the original structure or number of terms in the formula (so other regression models can be used) which means code to scan the deparsed lm model will need to be quite subtle.

stevec
  • 41,291
  • 27
  • 223
  • 311
AnserGIS
  • 103
  • 3
  • 1
    [How to correctly `dput` a fitted linear model (by `lm`) to an ASCII file and recreate it later?](https://stackoverflow.com/q/41645120/4891738) – Zheyuan Li Aug 08 '22 at 10:09
  • Thankyou, this will solve for the lm, and I dont (yet!) see why it shouldnt work for the segemented models too. If you make this an answer I will accept. I will also come back and leave a comment if/when I have it working for the segmented model. – AnserGIS Aug 08 '22 at 10:28
  • 1
    It works for "lm" and "glm" for sure. You can try "segmented" model... I should not re-post it as an answer here. You can just vote the them up (they are my answers in 2017). – Zheyuan Li Aug 08 '22 at 10:32
  • 3
    I think what you're after is not deparsing, it's serializing. (De-)parsing basically means to turn code into a string and vice versa. (De-)serializing means to convert a given object (not code) into a format that can be stored and from which it can be restored later. You can serialize into strings, but usually binary formats are a better fit. R has the `saveRDS` function for this purpose. – AEF Aug 08 '22 at 12:42
  • I said I would come back about the other models. In planning I believed it would be useful to have a human readable encoding, but that proved inconsistent. So I went with saveRDS, which works fine with lm, GLM and Segmented. Also, it can be emdedded into JSON and then parsed again if notes are needed. – AnserGIS Dec 13 '22 at 15:31

1 Answers1

1

As @AEF mentions in their comment, you might be looking for serialization with saveRDS(). If you specifically need a text format rather than a binary one, you can use ascii = TRUE.

fit0 <- lm(mpg ~ wt, data = mtcars)
saveRDS(fit0, "model.rds", ascii = TRUE)
fit1 <- readRDS("model.rds")

predict(fit0, data.frame(wt = 3))
#>        1 
#> 21.25171
predict(fit1, data.frame(wt = 3))
#>        1 
#> 21.25171
Mikko Marttila
  • 10,972
  • 18
  • 31