[R] Save model and predictions from svm
Steve Lianoglou
mailinglist.honeypot at gmail.com
Tue Aug 4 05:36:48 CEST 2009
> 2) I'm coming from RapidMinder, so am still getting used to how data
> is
> handled in R. (In RM, everything is like the R data.frame and
> predictions are automatically appended as new columns to the data.)
>
> What I'd like is this:
>
> Starting with data frame of:
> label, v1, v2, v3
>
> After svm prediction, ending up with data frame of:
> label, v1, v2, v3, prediction, probability
As I said, using the code from ?predict.svm
R> data(iris)
R> attach(iris)
R> x <- subset(iris, select = -Species)
R> y <- Species
R> model <- svm(x, y, probability = TRUE)
R>
R> # New stuff
R> pred <- predict(model, x, probability = TRUE)
R> x$prediction <- pred
R> x$probability <- apply(attr(pred, 'probabilities'), 1, max)
R> head(x)
Sepal.Length Sepal.Width Petal.Length Petal.Width prediction
probability
1 5.1 3.5 1.4 0.2 setosa
0.9817288
2 4.9 3.0 1.4 0.2 setosa
0.9747833
3 4.7 3.2 1.3 0.2 setosa
0.9805357
4 4.6 3.1 1.5 0.2 setosa
0.9767454
5 5.0 3.6 1.4 0.2 setosa
0.9809744
6 5.4 3.9 1.7 0.4 setosa
0.9758503
although "pred" looks like a "huge object", it is actually just a
vector of factors (with length 150 in this case). When you "look at"
pred by typing "pred" in the R terminal, you see all the predictions,
but then probability matrix is also written to the screen, which might
be what makes it looks "huge".
This is actually just saved as an attribute onto pred, which you
access via "attr(pred, 'probabilities')." If you don't specifically
ask for the attribute, it won't get in your way (except when showing
the object on the screen) -- this is why I can do "x$prediction <-
pred" because there are as many elements in the 'pred' vector as there
are rows in the x matrix.
HTH,
-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