Saving jags.model in RData file - r

Is there any way to save a jags.model() object into a RData or txt file ?
To perform MCMC on a better computer, I have to save my model on one and using it in a new workspace. But I've some difficulty to use "save()" and "load()" function on R.
Thanks for your advices.
Added:
I tried:
jags <- jags.model('regression.bug',
data = my.data,
n.chains = 4,
n.adapt = 1000)
Then I would like save "jags"
save( jags , file="jags.RData")
It's look like if it's saved. But, when I try:
ld.jags <- load( "jags.RData" )
ld.jags
[1] "jags"
And I don't know How I could use "ld.jags" to perform my analysis.

Related

R exporting Kmeans clustering results into excel

i've run my kmeans test from an excel data source and now want to get he results out in excel. I've tried the following code but all i get is a blank worksheet. I'm relatively new to R so i imagine its something simple that I'm missing - please help!
set.seed(123)
kmeansresults<-kmeans(df[,7], 5, iter.max = 50, nstart = 100)
x<-kmeansresults$clusters
write.csv(x, "clustering results.csv")
Try the following:
data("USArrests")
m <- scale(USArrests)
set.seed(123)
km_res <- kmeans(m, 4, nstart=25)
x <- km_res$cluster
write.csv(x, "/Users/user/Desktop/foo.csv")
I guess your problem was not writing a CSV file but calling km_res$clusters that should be km_res$cluster. In R you can access the structure of an object as str(km_res) to see what is "inside". There is no slot clusters but cluster.

Loading in multiple .rda files into a list in r

I have run various models (glm, rpart, earth etc) and exported the model object from each respective one into a folder on my computer. So I now have a folder with ~60 different models stored as seperate .rda files.
This was done by creating a model function and then applying it to a list of model types through the purrr map package (to avoid errors and termination).
I now want to load them back into r and compare them. Unfortunatley when I wrote my intial model script each model is stored as the same ie "Model.Object" (I didnt know how to do otherwise) so when I try to load each one individually into r it just overides each other. Each file is saved as glm.rda, rpart.rda, earth.rda etc but the model within is labelled Model.Object (for clarification).
So I guess I have a few questions;
1. It is possible to load in multiple .rda files into r into a list that can then be indexed
2. How to alter the model function that has been applied so that the 'model.object' name reads as the model type (e.g. glm, rpart etc)
Code:
Model.Function = function(Model.Type){
set.seed(0)
Model.Output = train(x = Pred.Vars.RVC.Data, y = RVC, trControl = Tcontrolparam,
preProcess = Preprocessing.Options, tuneLength = 1, metric = "RMSE",
method = Model.Type)
save(Model.Object, file = paste("./RVC Models/",Model.Type,".rda", sep = ""))
return(Model.Object)
}
Possibly.Model.Function = possibly(Model.Function, otherwise = "something wrong here")
result.possible = map(c("glm","rpart","earth"), Possibly.Model.Function)
For now, a rescue operation of your existing files might look something like this (following #nicola's comment about using the envir argument to load()):
rda2list <- function(file) {
e <- new.env()
load(file, envir = e)
as.list(e)
}
folder <- "./RVC Models"
files <- list.files(folder, pattern = ".rda$")
models <- Map(rda2list, file.path(folder, files))
names(models) <- tools::file_path_sans_ext(files)
Going forward, it would be easier to save your models as .Rds files with saveRDS() rather than using save(). Then reassignment is easy upon loading the file. See e.g. this question and answer for more details on the matter.

Random forest object not loading

I am saving two random forest objects as a rda files. When I load them- One loads as character and the other loads as randomForest object! Can someone explain this?
Here is my code snippet :
fit1 <- load("rfModel_pw2.rda")
fit2 <- load("rfModel_pw3.rda")
Pred1 <- predict(get(fit1), test, "prob")
#Error in get(fit1) : invalid first argument
Pred2 <- predict(get(fit2), test, "prob")
class(fit1)
#[1] "randomForest.formula" "randomForest"
> class(fit2)
#[1] "character"
load() places loaded objects from .rda file in global environment automatically and returns only the character names of loaded objects. Instead of using get([name]) simply use the same object-name before saving and after loading, as in example. Otherwise if you like the loader function to return loaded object, you can replace load() / save() with saveRDS() / readRDS().
library(randomForest)
X = replicate(2,rnorm(1000))
y = apply(X,1,sum)
rf = randomForest(X,y)
save(rf,file="./rf.rda")
rm(list=ls())
load(file="./rf.rda") #object is restored in global enviroment by former name
predict(rf,replicate(2,rnorm(1000)))

readBin from memory, rather than the disk

I've been working a bit lately with xgboost models:
library(xgboost)
set.seed(23)
data(agaricus.train, package='xgboost')
data(agaricus.test, package='xgboost')
train <- agaricus.train
test <- agaricus.test
bst <- xgboost(
data = train$data, label = train$label, max.depth = 2,
eta = 1, nround = 2,objective = "binary:logistic")
pred <- predict(bst, test$data)
Unfortunately, if you try to save and load and xgboost model, the result is a segfault:
f <- tempfile()
saveRDS(bst, f)
bst_new = readRDS(f)
unlink(f)
print(bst_new) #NULL pointer
pred2 <- predict(bst_new, test$data) #This line segfaults
This is because an xgboost model is actually just a pointer to a C++ object in memory: the R package is really just a thin wrapper around some C++ code:
> print(bst)
<pointer: 0x10a5ae750>
attr(,"class")
[1] "xgb.Booster"
The package authors are aware of this, and have written 2 custom functions (xgb.save and xgb.load) for saving and loading xgboost models, but they aren't as useful as just being able to use save and load from base R. I've come up with a hack for saving the model as part of an R session, but it's not very pretty:
#Hack to save an xgboost model
f1 <- tempfile()
f2 <- tempfile()
xgb.save(bst, f1)
model_raw_bytes <- readBin(f1, what='raw', file.info(f)[1, "size"])
unlink(f1)
print(head(model_raw_bytes))
saveRDS(model_raw_bytes, f2)
#Hack to load an xgboost
f3 <- tempfile()
model_raw_bytes <- readRDS(f2)
writeBin(model_raw_bytes, f3)
model_2 <- xgb.load(f3)
pred_2 <- predict(model_2, test$data)
all.equal(pred_2, pred)
unlink(f2)
unlink(f3)
Is there a way to readBin directly from a location in memory, rather than from the disk? (This would save me from writing temp files to save and load the models).
Better yet, is there an accepted way for wrapping C++ objects inside R objects that I can guide the package authors towards? They've been pretty receptive to my comments on github and if I could submit a PR to make this work, I think they'd accept it.

How to save a glmnet model to a file in R?

When I am using R, how can I save a model built by glmnet to a file, and then read it from the file so as to use it to predict?
Is it also the same if I use cv.glmnet to build the model?
Thanks!
Maybe I misunderstand your point, but it is always feasible to use the save function to save your R object in the .RData file. Next time, you simply use load(YourFile.RData) to load the object(s) into session.
library(glmnet)
library(ISLR)
# Data and model
auto = ISLR::Auto
mod = cv.glmnet(as.matrix(auto[1:300,2:6]), as.matrix(auto[1:300,1]), type.measure = "mse", nfolds = 5)
predict(mod, newx = as.matrix(auto[300:392,2:6]), s = "lambda.min")
# Save model
save(mod, file="D:/mymodel.RData")
rm(mod)
# Reload model
load("D:/mymodel.RData")
predict(mod, newx = as.matrix(auto[300:392,2:6]), s = "lambda.min")

Resources