Hi I''m trying to predict using a model I fitted with SVM. I constructed the model (called Svm) using a training set, and now I want to use a test set (called BankTest) for prediction. The response variable is in the first column of BankTest.> SvmPred = predict(Svm, BankTest[,-1], probability=TRUE) > SvmPredRes = table(Pred = SvmPred, True = BankTest[,1])I get this error: Error in table(Pred = SvmPred, True = BankTest) : all arguments must have the same length I checked the length of both BankTest[,1] and SvmPredict.> length(SvmPred)[1] 2282> length(BankTest[,1])[1] 2598 How are these two columns not the same size? Surely the prediction should try to predict all the values of BankTest[,1]? Any help would be much appreciated. -- View this message in context: http://r.789695.n4.nabble.com/SVM-Prediction-and-Plot-tp3254421p3254421.html Sent from the R help mailing list archive at Nabble.com.

Hi, On Wed, Feb 2, 2011 at 9:56 AM, Brian <cttobin at hotmail.com> wrote:> > Hi > > I''m trying to predict using a model I fitted with SVM. > > I constructed the model (called Svm) using a training set, and now I want to > use a test set (called BankTest) for prediction. > > The response variable is in the first column of BankTest. > >> SvmPred = predict(Svm, BankTest[,-1], probability=TRUE) >> SvmPredRes = table(Pred = SvmPred, True = BankTest[,1]) > > I get this error: > Error in table(Pred = SvmPred, True = BankTest) : ?all arguments must have > the same length > > ?I checked the length of both BankTest[,1] and SvmPredict. >> length(SvmPred) > [1] 2282 >> length(BankTest[,1]) > [1] 2598 > > How are these two columns not the same size? Surely the prediction should > try to predict all the values of BankTest[,1]?What do you get when you type: R> head(SvmPred) -- 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

Thanks for the reply. When I run this:> head(SvmPred)3570 2361 5406 2041 3440 4123 N N Y Y Y Y Levels: N Y -- View this message in context: http://r.789695.n4.nabble.com/SVM-Prediction-and-Plot-tp3254421p3254570.html Sent from the R help mailing list archive at Nabble.com.

Hi, On Wed, Feb 2, 2011 at 10:35 AM, Brian <cttobin at hotmail.com> wrote:> > Thanks for the reply. > > When I run this: > >> head(SvmPred) > 3570 2361 5406 2041 3440 4123 > ? N ? ?N ? ?Y ? ?Y ? ?Y ? ?Y > Levels: N YHmmm ... I think we''ll need more info. It looks like it should be working. By your output, your `probabilites=TRUE` parameter in your call to `predict` isn''t actually doing anything. You need to train the svm and set `probability=TRUE` if you''re really after class probabilties. Also, maybe there are some NA''s in your data? Also, to be extra careful, the second argument to predict should be a matrix, I guess it already is, but ... what happens if you call predict like so: R> SvmPred = predict(Svm, as.matrix(BankTest[,-1]), na.omit=na.fail) -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

There are a good few blanks in some variables in the data. There were in the training set too. Is that a problem? I don''t have any na.action in my svm call either. So na.omit=na.fail causes this error: Error in na.fail.default(newdata) : missing values in object I tried the matrix.>SvmPred = predict(Svm, as.matrix(BankTest[,-1]))Error in scale.default(newdata[, object$scaled, drop = FALSE], center object$x.scale$"scaled:center", : length of ''center'' must equal the number of columns of ''x'' Also is.matrix fails for BankTest so it isn''t actually a matrix. Not sure it matters though. The prediction is actually working somewhat, but it only predicts for 2282 cases instead of 2598. P.s. I do have the probability set to TRUE in the Svm call already. -- View this message in context: http://r.789695.n4.nabble.com/SVM-Prediction-and-Plot-tp3254421p3255323.html Sent from the R help mailing list archive at Nabble.com.

Hi, On Wed, Feb 2, 2011 at 1:40 PM, Brian <cttobin at hotmail.com> wrote:> > There are a good few blanks in some variables in the data. There were in the > training set too. Is that a problem?Well, it looks like it''s the root cause of what prompted you to post to R-help ... whether or not it''s *really* a problem is another issue, I guess ...> I don''t have any na.action in my svm > call either.Makes sense ... the SVM is just skipping these examples and not using them for training. The default setting for na.action is na.omit, which just removes these cases from training.> So na.omit=na.fail causes this error: > Error in na.fail.default(newdata) : missing values in objectBingo.> I tried the matrix. >>SvmPred = predict(Svm, as.matrix(BankTest[,-1])) > Error in scale.default(newdata[, object$scaled, drop = FALSE], center > object$x.scale$"scaled:center", ?: > ?length of ''center'' must equal the number of columns of ''x'' > > Also is.matrix fails for BankTest so it isn''t actually a matrix. Not sure it > matters though. The prediction is actually working somewhat, but it only > predicts for 2282 cases instead of 2598.We know why now. Please read the help for the predict.svm function: R> ?predict.svm Specifically the documentation for the `na.action` Argument. Perhaps it would be more friendly if predict just returned an NA in these situations, but I guess this is how it works for the time being. Anyway, it looks like this explains why you have a different number of predictions than you have observations in your BankTest. -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