This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Cmd+Shift+Enter.
Install.packages('AppliedPredictiveModeling')
Error in Install.packages("AppliedPredictiveModeling") :
could not find function "Install.packages"
#Load in Dataset
social <- read.csv("~/Downloads/social.csv")
socialdf = social[3:5] #just want Age EstimatedSalary and Purchased columns
socialdf$Purchased = factor(socialdf$Purchased, levels = c(0, 1)) #Factor
#Data Exploration
boxplot(socialdf$Age)
boxplot(socialdf$EstimatedSalary)
sapply(socialdf, function(x) sum(is.na(x)))
Age EstimatedSalary Purchased
0 0 0
set.seed(123)
#Split Data into Train and test set
split = sample.split(socialdf$Purchased, SplitRatio = 0.75)
training_set = subset(socialdf, split == TRUE)
test_set = subset(socialdf, split == FALSE)
training_set[-3] = scale(training_set[-3]) #excluding targert variable purchased
test_set[-3] = scale(test_set[-3])
#Employ SVM
MidtermSvm = svm(formula = Purchased ~ .,
data = training_set,
type = 'C-classification',
kernel = 'linear')
MidtermSvm
Call:
svm(formula = Purchased ~ ., data = training_set, type = "C-classification",
kernel = "linear")
Parameters:
SVM-Type: C-classification
SVM-Kernel: linear
cost: 1
Number of Support Vectors: 116
#Show Predictions and Confusion Matrix
y_pred = predict(MidtermSvm, newdata = test_set[-3])
y_pred
2 4 5 9 12 18 19 20 22 29 32 34 35 38 45 46 48 52 66 69
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
74 75 82 84 85 86 87 89 103 104 107 108 109 117 124 126 127 131 134 139
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
148 154 156 159 162 163 170 175 176 193 199 200 208 213 224 226 228 229 230 234
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 0 1 1
236 237 239 241 255 264 265 266 273 274 281 286 292 299 302 305 307 310 316 324
1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 0 1 0 0 0
326 332 339 341 343 347 353 363 364 367 368 369 372 373 380 383 389 392 395 400
0 1 0 1 0 1 1 0 1 1 1 0 1 0 1 1 0 0 0 0
Levels: 0 1
cm = table(test_set[, 3], y_pred)
cm
y_pred
0 1
0 57 7
1 13 23
# Create a grid of values for Age and EstimatedSalary
X1 = seq(min(training_set$Age) - 1, max(training_set$Age) + 1, by = 0.01)
X2 = seq(min(training_set$EstimatedSalary) - 1, max(training_set$EstimatedSalary) + 1, by = 0.01)
grid_set = expand.grid(X1, X2)
colnames(grid_set) = c('Age', 'EstimatedSalary')
y_grid = predict(MidtermSvm, newdata = grid_set)
y_pred = predict(MidtermSvm, newdata = test_set[-3])
plot(training_set[, -3], main = 'SVM (Training set)', xlab = 'Age', ylab = 'Estimated Salary')
contour(X1, X2, matrix(as.numeric(y_grid), length(X1), length(X2)), add = TRUE)
points(grid_set, pch = '.', col = ifelse(y_grid == 1, 'green', 'orange'))
points(training_set[, -3], pch = 21, bg = ifelse(training_set[, 3] == 1, 'black', 'red'))
NA
NA
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Cmd+Option+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Cmd+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.