I am using survival data to fit a flexible survival model (example below). I can capture the output of the fitted model as *.txt file and save it in my directory. My question is how can I read back the output into an object in r and use it to plot the fitted model and do predictions (without the need for the data to re-run the model again).
The reason I want this is because I am working on a server and can only export output rather than the data and want to use the fitted model in a shinyapp on my personal computer.
library(data.table)
library(flexsurv)
#load some data
data(pbc, package="randomForestSRC")
data <- as.data.table(na.omit(pbc))
data[, years := days/365.25]
fit <- flexsurvspline(Surv(years, status) ~ albumin, data=data, k=1, scale='hazard')
fit
capture.output(fit, file='fit.txt')
ndata <- data.frame(albumin = 3)
#plot
plot(fit, est=FALSE, ci=FALSE, col.obs = 'white')
lines(fit, newdata = ndata, ci=FALSE, col = 'green')
#predict
prob=summary(fit,type='survival',newdata=ndata, t=12)
prob
#how can I read the model output saved in .txt file and use
#read .txt model output
fit2 <- read #???
fit2
plot(fit2, est=FALSE, ci=FALSE, col.obs = 'white')
lines(fit2, newdata = ndata, ci=FALSE, col = 'green')
prob=summary(fit2,type='survival',newdata=ndata, t=12)
prob