[R] Save model and predictions from svm

Steve Lianoglou mailinglist.honeypot at gmail.com
Tue Aug 4 05:15:37 CEST 2009


Hi,

On Aug 3, 2009, at 10:55 PM, Noah Silverman wrote:

> Hello,
>
> I'm using the e1071 package for training an SVM.  It seems to be  
> working
> well.
>
> This question has two parts:
>
> 1) Once I've trained an SVM model, I want to USE it within R at a  
> later
> date to predict various new data.  I see the write.svm command, but
> don't know how to LOAD the model back in so that I can use it  
> tomorrow.
> How can I do this?

You can circumvent the e1071-specific write functions and just use R's  
builtin save() method. Eg,

R> save(mymodel, file='mymodel.rda')

You can load it later like so:

R> load('mymodel.rda')

> 2) I would like to add the prediction values(confidence) as a column  
> in
> my original data.frame.  (Again, to be used for more analysis at a  
> later
> date.)  I am using "predictions <- prediction(model,traindata)" and  
> that
> gives me a huge object with all the predictions.  Is there a single
> command that would just add the predictions, or do I need to do some
> clever data manipulation?

What do you mean "a huge object"? By default you should just be  
getting a vector of length Nx1 where N is the number of examples to  
predict over, and the value is the class it belongs to -- which seems  
like what you're after.

If you call predict.svm with decision.values=TRUE, you'll get an N x  
NUMBER_OF_CLASSES matrix. In that case, what do you mean by "a command  
that just add[s] the predictions"? If you want to add all decision  
values to your original data, you can use cbind. If you want to return  
the max value for each data point, play with the apply function and  
its MARGIN parameter along with the max/which.max functions -- but if  
you're just adding the max value, you're losing the decision label.

Anyway -- the predict.svm function just returns a vector/matrix. You  
can use the standard R vector/matrix manipulation methods to do what  
you want. If you're still having trouble with that, please post an  
example of what you're after -- and simply use the code/data from the ? 
predict.svm example section so we can play along.

-steve

--
Steve Lianoglou
Graduate Student: Computational Systems Biology
   |  Memorial Sloan-Kettering Cancer Center
   |  Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact




More information about the R-help mailing list