2017-02-10 04:00:49 +00:00
|
|
|
# https://rpubs.com/ppaquay/65560
|
|
|
|
|
2017-02-10 03:59:40 +00:00
|
|
|
library(ISLR)
|
|
|
|
library(MASS)
|
|
|
|
library(class)
|
|
|
|
|
|
|
|
auto = read.table("auto.data",header=T,na.strings="?")
|
|
|
|
|
|
|
|
|
|
|
|
auto$mpg01=rep(0,397)
|
|
|
|
auto$mpg01[auto$mpg>median(auto$mpg)]=1
|
|
|
|
|
|
|
|
sample(auto,size=length(mpg01)/2)
|
|
|
|
|
|
|
|
train_bools <- (auto$year %% 2 == 0)
|
|
|
|
train_data = auto[train_bools,]
|
|
|
|
test_data = auto[!train_bools,]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lda.fit=lda(mpg01 ~ horsepower + weight + cylinders + displacement,data=train_data)
|
|
|
|
lda.pred=predict(lda.fit, test_data)
|
|
|
|
mean(lda.pred$class!=test_data$mpg01,na.rm=T)
|
|
|
|
|
2017-02-10 04:12:09 +00:00
|
|
|
qda.fit=qda(mpg01 ~ horsepower + weight + cylinders + displacement,data=train_data)
|
2017-02-10 03:59:40 +00:00
|
|
|
qda.pred=predict(qda.fit,test_data,na.rm=T)
|
|
|
|
mean(qda.pred$class!=test_data$mpg01,na.rm=T)
|
|
|
|
|
2017-02-10 04:12:09 +00:00
|
|
|
glm.fit=glm(mpg01 ~ horsepower + weight + cylinders + displacement,data=train_data,family=binomial)
|
2017-02-10 03:59:40 +00:00
|
|
|
glm.probs=predict(glm.fit,test_data,type="response")
|
2017-02-10 04:12:09 +00:00
|
|
|
glm.pred=rep(0,nrow(test_data))
|
2017-02-10 03:59:40 +00:00
|
|
|
glm.pred[glm.probs>.5]=1
|
|
|
|
mean(glm.pred!=test_data$mpg01)
|
|
|
|
|
|
|
|
set.seed(1)
|
|
|
|
auto <- na.omit(auto)
|
|
|
|
train_bools <- (auto$year %% 2 == 0)
|
|
|
|
train_data = auto[train_bools,]
|
|
|
|
test_data = auto[!train_bools,]
|
|
|
|
|
|
|
|
|
2017-02-10 04:12:09 +00:00
|
|
|
train.X = cbind(auto$horsepower,auto$displacement,auto$weight,auto$cylinders)[train_bools,]
|
|
|
|
test.X = cbind(auto$horsepower,auto$displacement,auto$weight,auto$cylinders)[!train_bools,]
|
2017-02-10 03:59:40 +00:00
|
|
|
train.mpg01 = auto$mpg01[train_bools]
|
|
|
|
|
|
|
|
knn.pred = knn(train.X,test.X,train.mpg01,k=1)
|
2017-02-10 04:12:09 +00:00
|
|
|
mean(knn.pred != test_data$mpg01)
|
|
|
|
|
|
|
|
knn.pred = knn(train.X,test.X,train.mpg01,k=2)
|
|
|
|
mean(knn.pred != test_data$mpg01)
|
|
|
|
|
|
|
|
knn.pred = knn(train.X,test.X,train.mpg01,k=3)
|
|
|
|
mean(knn.pred != test_data$mpg01)
|
|
|
|
|
|
|
|
knn.pred = knn(train.X,test.X,train.mpg01,k=4)
|
|
|
|
mean(knn.pred != test_data$mpg0)
|