kNN CLASSIFIERS
______ : Use your own data features / variables
library(class)
str(clean_nhanes)
# distance metric only works with quantitative variables
train_q <- train %>%
select(Age, AgeMonths, HHIncomeMid, Poverty, HomeRooms, Weight, Length, HeadCirc, Height,
BMI, Pulse, BPSysAve, BPDiaAve, BPSys1, BPDia1, BPSys2, BPDia2, BPSys3, BPDia3,
Testosterone, DirectChol, TotChol, UrineVol1, UrineFlow1, UrineVol2, UrineFlow2,
DiabetesAge, DaysPhysHlthBad, DaysMentHlthBad, nPregnancies, nBabies, Age1stBaby,
SleepHrsNight, PhysActiveDays, TVHrsDayChild, CompHrsDayChild, AlcoholDay, AlcoholYear,
SmokeAge, AgeFirstMarij, AgeRegMarij, SexAge, SexNumPartYear)
test_q <- test %>%
select(Age, AgeMonths, HHIncomeMid, Poverty, HomeRooms, Weight, Length, HeadCirc, Height,
BMI, Pulse, BPSysAve, BPDiaAve, BPSys1, BPDia1, BPSys2, BPDia2, BPSys3, BPDia3,
Testosterone, DirectChol, TotChol, UrineVol1, UrineFlow1, UrineVol2, UrineFlow2,
DiabetesAge, DaysPhysHlthBad, DaysMentHlthBad, nPregnancies, nBabies, Age1stBaby,
SleepHrsNight, PhysActiveDays, TVHrsDayChild, CompHrsDayChild, AlcoholDay, AlcoholYear,
SmokeAge, AgeFirstMarij, AgeRegMarij, SexAge, SexNumPartYear)
# knn for test set (k=10)
SleepTrouble_knn10 <- knn(train_q, test = test_q, cl = train$SleepTrouble, k = 10)
SleepTrouble_knn <- knn(train_q, test = test_q, cl = train$SleepTrouble, k = 5) #더 좋아짐.
# performance of knn for test set
confusionMatrix(table(test$SleepTrouble, SleepTrouble_knn10))
confusionMatrix(table(train$SleepTrouble, SleepTrouble_knn10))
###Optimizing the value of the parameter k with cross-validation in kNN
knn_error_rate <-function(x, y, numNeighbors, z=x) {
y_hat <- knn(train=x, test=z, cl=y, k=numNeighbors)
return(sum(y_hat !=y) / nrow(x))
}
ks<-c(1:10, 15, 20, 25, 30)
train_rates <- sapply(ks, FUN=knn_error_rate, x=train_q, y=train$SleepTrouble)
knn_error_rates <- data.frame(k=ks, train_rate=train_rates)
ggplot(data=knn_error_rates, aes(x=k, y=train_rate)) +
geom_point() + geom_line() + ylab("Misclassification Rate")
▼ R 프로그래밍 학습용 추천 도서
|
▼ Python 파이썬 프로그래밍 학습용 추천 도서
|
댓글