'SVM doesn't plot in R
I'm doing predictive analytics on R, and I executed a plot command with neither an error message nor an output. I'll reproduce my code from a stroke dataset.
stroke <- read.csv('healthcare-dataset-stroke-data.csv')
stroke <- subset (stroke, select = -id)
stroke <- subset(stroke, bmi != 'N/A')
stroke <- subset(stroke, smoking_status != 'Unknown')
stroke <- subset(stroke, gender!= 'Other')
stroke$ever_married[stroke$ever_married == 'Yes'] <- '1'
stroke$ever_married[stroke$ever_married == 'No'] <- '0'
stroke <- dummy_cols(stroke, select_columns = 'work_type')
stroke <- dummy_cols(stroke, select_columns = 'Residence_type')
stroke <- dummy_cols(stroke, select_columns = 'smoking_status')
stroke <- dummy_cols(stroke, select_columns = 'gender')
stroke$bmi <- as.numeric(stroke$bmi)
stroke$ever_married <- as.double(stroke$ever_married)
set.seed(2468)
dt = sort(sample(nrow(stroke), nrow(stroke)*.7))
train<-stroke[dt,]
test<-stroke[-dt,]
The above was data cleaning and prep work. Then I did the train and the test. Now here's the SVM work.
svmfit = svm(stroke ~ age + hypertension + heart_disease + ever_married + avg_glucose_level + bmi + work_type_Govt_job + work_type_Never_worked + work_type_Private + `work_type_Self-employed` + Residence_type_Rural + `smoking_status_never smoked` + smoking_status_smokes + gender_Female, data = stroke, kernel = "linear", cost = 10, scale = FALSE)
print(svmfit)
This is the output:
Call:
svm(formula = stroke ~ age + hypertension + heart_disease + ever_married +
avg_glucose_level + bmi + work_type_Govt_job + work_type_Never_worked +
work_type_Private + `work_type_Self-employed` + Residence_type_Rural +
`smoking_status_never smoked` + smoking_status_smokes + gender_Female,
data = stroke, kernel = "linear", cost = 10, scale = FALSE)
Parameters:
SVM-Type: eps-regression
SVM-Kernel: linear
cost: 10
gamma: 0.07142857
epsilon: 0.1
Number of Support Vectors: 2652
svmplot <- plot(svmfit, stroke)
svmplot
It didn't work before I saved it to a variable so when I tried that and called the variable, it responded with NULL.
Any thoughts?
Solution 1:[1]
You didn't get result because plot(x,y) requires that x and y are data points, whereas you use svmfit which is not data points.
Plotting svm() results can be done in several ways depending on your purpose. You can plot the fitted values against the actual values, both in training data. In another case, you can plot the predicted values from the svm model (that you built by using the training data) against the test data.
What I understand from your example is that you built a model to predict stroke variable, and you split the data into training and test, but in svmfit you used all data, not only the training data. So, I assume you want to plot the fitted stroke values from svmfit against the actual stroke values. If that's true, you can use, for example:
plot(svmfit$fitted, col = "red", pch = 17)
points(stroke$stroke, col = "blue", pch = 19)
This plot will show the fitted values of stroke resulted from svm in red points and the actual stroke values in blue points.
You can also use, for example:
plot(svmfit$fitted,stroke$stroke, col = c("red", "blue"), pch = c(17, 19))
You can use the same logic when you want to plot the predicted stroke data and the actual data from the test dataset.
Here is a simple example by using iris data:
svmfit.iris <- svm(Sepal.Length ~ Petal.Length + Petal.Width + Species,
data = iris, kernel = "linear", cost = 10, scale = FALSE)
plot(svmfit.iris$fitted, iris$Sepal.Length, col = c("red", "blue"), pch = c(17,19))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Abdur Rohman |

