SVM with cross validation in R using caret

后端 未结 1 1767
傲寒
傲寒 2021-01-30 14:57

I was told to use the caret package in order to perform Support Vector Machine regression with 10 fold cross validation on a data set I have. I\'m plotting my response variable

1条回答
  •  孤独总比滥情好
    2021-01-30 15:42

    You have to save your CV predictions via the "savePred" option in your trainControl object. I'm not sure what package your "cadets" data is from, but here is a trivial example using iris:

    > library(caret)
    > ctrl <- trainControl(method = "cv", savePred=T, classProb=T)
    > mod <- train(Species~., data=iris, method = "svmLinear", trControl = ctrl)
    > head(mod$pred)
            pred        obs      setosa  versicolor   virginica rowIndex   .C Resample
    1     setosa     setosa 0.982533940 0.009013592 0.008452468       11 0.25   Fold01
    2     setosa     setosa 0.955755054 0.032289120 0.011955826       35 0.25   Fold01
    3     setosa     setosa 0.941292675 0.044903583 0.013803742       46 0.25   Fold01
    4     setosa     setosa 0.983559919 0.008310323 0.008129757       49 0.25   Fold01
    5     setosa     setosa 0.972285699 0.018109218 0.009605083       50 0.25   Fold01
    6 versicolor versicolor 0.007223973 0.971168170 0.021607858       59 0.25   Fold01
    

    EDIT: The "C" is one of tuning parameters for your SVM. Check out the help for the ksvm function in the kernlab package for more details.

    EDIT2: Trivial regression example

    > library(caret)
    > ctrl <- trainControl(method = "cv", savePred=T)
    > mod <- train(Sepal.Length~., data=iris, method = "svmLinear", trControl = ctrl)
    > head(mod$pred)
          pred obs rowIndex   .C Resample
    1 4.756119 4.8       13 0.25   Fold01
    2 4.910948 4.8       31 0.25   Fold01
    3 5.094275 4.9       38 0.25   Fold01
    4 4.728503 4.8       46 0.25   Fold01
    5 5.192965 5.3       49 0.25   Fold01
    6 5.969479 5.9       62 0.25   Fold01
    

    0 讨论(0)
提交回复
热议问题