0

I'm running a series of regressions, and I need to save them out so I can reload them later. However, whatever I am doing doesn't allow me to load the regression results back in without errors. How should I save out the regression I make below (shr_fit), and how should I load it back in?

library(cmprsk)


data(Melanoma, package = "MASS")

head(Melanoma)

summary(Melanoma$time[Melanoma$status == 2])
summary(Melanoma$time[Melanoma$status == 1])
summary(Melanoma$time[Melanoma$status == 3])



shr_fit <- 
  crr(
    ftime = Melanoma$time,
    fstatus = Melanoma$status,
    cov1 = Melanoma[, c("sex", "age")],
    cencode = 2
  )

summary(shr_fit)

setwd("C:/Users/Nora Schwaller/Documents/UNC/Research/Vulnerability and Exposure/RData/")
saveRDS(shr_fit, file="crr_test.RData")

rm(shr_fit)

load("crr_test.RData")

Resulting error code:

Error in load("crr_test.RData") : bad restore file magic number (file may be corrupted) -- no data loaded In addition: Warning messages: 1: In readChar(con, 5L, useBytes = TRUE) : truncating string with embedded nuls 2: file ‘crr_test.RData’ has magic number 'X' Use of save versions prior to 2 is deprecated

tchoup
  • 971
  • 4
  • 11
  • 2
    The inverse of `saveRDS()` is `readRDS()`, not `load()`. Use `readRDS()` instead. – MrFlick May 13 '22 at 13:58
  • 2
    More specifically, if you `saveRDS(..)`, then you need to use `shr_fit <- readRDS(..)` since it returns the unnamed object itself. `*RDS` functions are purely functional, they only write/read single objects, and they never change the global environment. `load(..)`, conversely, saves the objects by their original names into the current environment, operating in side-effect; the target environment can be changed with `envir=`, the names cannot be altered during the load process. – r2evans May 13 '22 at 14:04

0 Answers0