-
Notifications
You must be signed in to change notification settings - Fork 7
/
KNN.R
53 lines (38 loc) · 1.11 KB
/
KNN.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
EDD <- read.csv('http://math.mercyhurst.edu/~sousley/STAT_139/data/EDDat3.csv')
library(MASS)
library(caret)
library(e1071)
library(janitor)
library(tidyr)
library(class)
NewEDD <- subset(EDD, select = c(Grp, i2, i3, i6, i7, i10, i22, i24, i35, i36, i37, i39, i45))
head(NewEDD)
newEDD2 <- na.omit(NewEDD)
head(newEDD2)
table(newEDD2$Grp)
dim(newEDD2)
table(newEDD2$Grp)
ED <-cbind(newEDD2["Grp"], scale(newEDD2[,c(2:13)]) )
head(ED)
class(ED$Grp)
#[1] "integer"
ED$Grp <- as.factor(ED$Grp)
ED$Grp <- as.numeric(ED$Grp)
class(ED$Grp)
#[1] "numeric"
#KNN Model
set.seed(1234)
Accuracies <- c(0.00)
for (i in seq(100))
{
inTrain <- createDataPartition(y=as.factor(ED$Grp), p=0.75, list=FALSE)
training <- ED[inTrain,]
testing <- ED[-inTrain,]
model_knn <- train(Grp ~ . ,data=training, method='knn')
Pred <- predict(model_knn,type="raw",newdata = testing)
cm <- confusionMatrix((Pred), testing$Grp)
Accuracies <- cm$overall["Accuracy"]
}
summary(Accuracies)
#Min. 1st Qu. Median Mean 3rd Qu. Max.
#0.7619 0.7619 0.7619 0.7619 0.7619 0.7619