Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does load(...) return character name of object instead of the object itself?

Tags:

r

save

svm

The svm model is created with the package e1071 in R. To use the model, I need to save it and read as needed. The package has write.svm, but does not have read.svm. If I use

model <- svm(x, y)

save(model, 'modelfile.rdata')
M <- load('modelfile.rdata')

object M contains just the word 'model'.

How to save the svm model and read back later, to apply to some new data?

like image 834
Marina Avatar asked Jun 12 '14 22:06

Marina


People also ask

How do I get the object name in R?

names() function in R Language is used to get or set the name of an Object. This function takes object i.e. vector, matrix or data frame as argument along with the value that is to be assigned as name to the object.

How do I save a list as a RData?

To save data as an RData object, use the save function. To save data as a RDS object, use the saveRDS function. In each case, the first argument should be the name of the R object you wish to save. You should then include a file argument that has the file name or file path you want to save the data set to.


1 Answers

Look at the return value for the function load in the help file:

Value:

 A character vector of the names of objects created, invisibly.

So "model" is indeed the expected value of M. Your svm has been restored under its original name, which is model.

If you find it a bit confusing that load does not return the object loaded but instead restores it under the name used in saving it, consider using saveRDS and readRDS.

saveRDS(model, 'modelfile.rds')
M <- readRDS('modelfile.rds')

and M should contain your svm model.

I prefer saveRDS and readRDS because with them I know what objects I'm creating in my workspace - see the blog post of Gavin Simpson (linked in his answer) for a detailed discussion.

like image 145
James King Avatar answered Sep 20 '22 14:09

James King