library(ISLR)
library(PerformanceAnalytics)
library(MASS)
library(class)
data("Weekly")
This question should be answered using the Weekly data set, which is part of the ISLR package. This data is similar in nature to the Smarket data from this chapter’s lab, except that it contains 1, 089 weekly returns for 21 years, from the beginning of 1990 to the end of 2010.
(a) Produce some numerical and graphical summaries of the Weekly data. Do there appear to be any patterns?
str(Weekly)
## 'data.frame': 1089 obs. of 9 variables:
## $ Year : num 1990 1990 1990 1990 1990 1990 1990 1990 1990 1990 ...
## $ Lag1 : num 0.816 -0.27 -2.576 3.514 0.712 ...
## $ Lag2 : num 1.572 0.816 -0.27 -2.576 3.514 ...
## $ Lag3 : num -3.936 1.572 0.816 -0.27 -2.576 ...
## $ Lag4 : num -0.229 -3.936 1.572 0.816 -0.27 ...
## $ Lag5 : num -3.484 -0.229 -3.936 1.572 0.816 ...
## $ Volume : num 0.155 0.149 0.16 0.162 0.154 ...
## $ Today : num -0.27 -2.576 3.514 0.712 1.178 ...
## $ Direction: Factor w/ 2 levels "Down","Up": 1 1 2 2 2 1 2 2 2 1 ...
sum(sapply(Weekly, is.nan))
## [1] 0
sapply(Filter(is.numeric,Weekly),range)
## Year Lag1 Lag2 Lag3 Lag4 Lag5 Volume Today
## [1,] 1990 -18.195 -18.195 -18.195 -18.195 -18.195 0.087465 -18.195
## [2,] 2010 12.026 12.026 12.026 12.026 12.026 9.328214 12.026
summary(Weekly)
## Year Lag1 Lag2 Lag3
## Min. :1990 Min. :-18.1950 Min. :-18.1950 Min. :-18.1950
## 1st Qu.:1995 1st Qu.: -1.1540 1st Qu.: -1.1540 1st Qu.: -1.1580
## Median :2000 Median : 0.2410 Median : 0.2410 Median : 0.2410
## Mean :2000 Mean : 0.1506 Mean : 0.1511 Mean : 0.1472
## 3rd Qu.:2005 3rd Qu.: 1.4050 3rd Qu.: 1.4090 3rd Qu.: 1.4090
## Max. :2010 Max. : 12.0260 Max. : 12.0260 Max. : 12.0260
## Lag4 Lag5 Volume Today
## Min. :-18.1950 Min. :-18.1950 Min. :0.08747 Min. :-18.1950
## 1st Qu.: -1.1580 1st Qu.: -1.1660 1st Qu.:0.33202 1st Qu.: -1.1540
## Median : 0.2380 Median : 0.2340 Median :1.00268 Median : 0.2410
## Mean : 0.1458 Mean : 0.1399 Mean :1.57462 Mean : 0.1499
## 3rd Qu.: 1.4090 3rd Qu.: 1.4050 3rd Qu.:2.05373 3rd Qu.: 1.4050
## Max. : 12.0260 Max. : 12.0260 Max. :9.32821 Max. : 12.0260
## Direction
## Down:484
## Up :605
##
##
##
##
chart.Correlation(cor(Weekly[,-9]), histogram = FALSE, pch=19)
* The range of
Lag1,Lag2,Lag3,Lag4,Lag5 are same, but their mean, median are different. Variable Year and Volumn have significant linear relation, others are not.
(b) Use the full data set to perform a logistic regression with Direction as the response and the five lag variables plus Volume as predictors. Use the summary function to print the results. Do any of the predictors appear to be statistically significant? If so, which ones?
attach(Weekly)
Log.model = glm(Direction~.-Year-Today, data = Weekly, family= binomial)
summary(Log.model)
##
## Call:
## glm(formula = Direction ~ . - Year - Today, family = binomial,
## data = Weekly)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.6949 -1.2565 0.9913 1.0849 1.4579
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.26686 0.08593 3.106 0.0019 **
## Lag1 -0.04127 0.02641 -1.563 0.1181
## Lag2 0.05844 0.02686 2.175 0.0296 *
## Lag3 -0.01606 0.02666 -0.602 0.5469
## Lag4 -0.02779 0.02646 -1.050 0.2937
## Lag5 -0.01447 0.02638 -0.549 0.5833
## Volume -0.02274 0.03690 -0.616 0.5377
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 1496.2 on 1088 degrees of freedom
## Residual deviance: 1486.4 on 1082 degrees of freedom
## AIC: 1500.4
##
## Number of Fisher Scoring iterations: 4
(c) Compute the confusion matrix and overall fraction of correct predictions. Explain what the confusion matrix is telling you about the types of mistakes made by logistic regression.
Log.model.pred <- predict(Log.model, type = "response")
Log.model.pred = ifelse(Log.model.pred >=0.5, "Up","Down")
caret::confusionMatrix(as.factor(Log.model.pred), Direction)
## Confusion Matrix and Statistics
##
## Reference
## Prediction Down Up
## Down 54 48
## Up 430 557
##
## Accuracy : 0.5611
## 95% CI : (0.531, 0.5908)
## No Information Rate : 0.5556
## P-Value [Acc > NIR] : 0.369
##
## Kappa : 0.035
##
## Mcnemar's Test P-Value : <2e-16
##
## Sensitivity : 0.11157
## Specificity : 0.92066
## Pos Pred Value : 0.52941
## Neg Pred Value : 0.56434
## Prevalence : 0.44444
## Detection Rate : 0.04959
## Detection Prevalence : 0.09366
## Balanced Accuracy : 0.51612
##
## 'Positive' Class : Down
##
(d) Now fit the logistic regression model using a training data period from 1990 to 2008, with Lag2 as the only predictor. Compute the confusion matrix and the overall fraction of correct predictions for the held out data (that is, the data from 2009 and 2010).
Weekly.train <-Weekly[(Year<2009),]
Weekly.test <-Weekly[!(Year<2009),]
Weekly.fit<-glm(Direction~Lag2, data=Weekly.train,family=binomial)
LogWeekly.preb= predict(Weekly.fit, Weekly.test, type = "response")
LogWeekly.preb = ifelse(LogWeekly.preb > 0.5, "Up","Down")
caret::confusionMatrix(as.factor(LogWeekly.preb), Weekly.test$Direction)
## Confusion Matrix and Statistics
##
## Reference
## Prediction Down Up
## Down 9 5
## Up 34 56
##
## Accuracy : 0.625
## 95% CI : (0.5247, 0.718)
## No Information Rate : 0.5865
## P-Value [Acc > NIR] : 0.2439
##
## Kappa : 0.1414
##
## Mcnemar's Test P-Value : 7.34e-06
##
## Sensitivity : 0.20930
## Specificity : 0.91803
## Pos Pred Value : 0.64286
## Neg Pred Value : 0.62222
## Prevalence : 0.41346
## Detection Rate : 0.08654
## Detection Prevalence : 0.13462
## Balanced Accuracy : 0.56367
##
## 'Positive' Class : Down
##
(e) Repeat (d) using LDA.
lda.fit<-lda(Direction ~ Lag2, data=Weekly.train,family=binomial)
ldaWeekly.pred = predict(lda.fit,Weekly.test)
caret::confusionMatrix(as.factor(ldaWeekly.pred$class), as.factor(Direction[!(Year<2009)]))
## Confusion Matrix and Statistics
##
## Reference
## Prediction Down Up
## Down 9 5
## Up 34 56
##
## Accuracy : 0.625
## 95% CI : (0.5247, 0.718)
## No Information Rate : 0.5865
## P-Value [Acc > NIR] : 0.2439
##
## Kappa : 0.1414
##
## Mcnemar's Test P-Value : 7.34e-06
##
## Sensitivity : 0.20930
## Specificity : 0.91803
## Pos Pred Value : 0.64286
## Neg Pred Value : 0.62222
## Prevalence : 0.41346
## Detection Rate : 0.08654
## Detection Prevalence : 0.13462
## Balanced Accuracy : 0.56367
##
## 'Positive' Class : Down
##
(f) Repeat (d) using QDA.
qda.fit<-qda(Direction ~ Lag2, data=Weekly.train,family=binomial)
qdaWeekly.pred = predict(qda.fit,Weekly.test)
caret::confusionMatrix(as.factor(qdaWeekly.pred$class),Weekly.test$Direction)
## Confusion Matrix and Statistics
##
## Reference
## Prediction Down Up
## Down 0 0
## Up 43 61
##
## Accuracy : 0.5865
## 95% CI : (0.4858, 0.6823)
## No Information Rate : 0.5865
## P-Value [Acc > NIR] : 0.5419
##
## Kappa : 0
##
## Mcnemar's Test P-Value : 1.504e-10
##
## Sensitivity : 0.0000
## Specificity : 1.0000
## Pos Pred Value : NaN
## Neg Pred Value : 0.5865
## Prevalence : 0.4135
## Detection Rate : 0.0000
## Detection Prevalence : 0.0000
## Balanced Accuracy : 0.5000
##
## 'Positive' Class : Down
##
(g) Repeat (d) using KNN with K = 1.
knn.train = data.frame(Weekly.train$Lag2)
knn.test = data.frame(Weekly.test$Lag2)
set.seed(1)
knn.pred = knn(knn.train, knn.test, Weekly.train$Direction, k=1)
caret::confusionMatrix(as.factor(knn.pred),as.factor(Weekly.test$Direction))
## Confusion Matrix and Statistics
##
## Reference
## Prediction Down Up
## Down 21 30
## Up 22 31
##
## Accuracy : 0.5
## 95% CI : (0.4003, 0.5997)
## No Information Rate : 0.5865
## P-Value [Acc > NIR] : 0.9700
##
## Kappa : -0.0033
##
## Mcnemar's Test P-Value : 0.3317
##
## Sensitivity : 0.4884
## Specificity : 0.5082
## Pos Pred Value : 0.4118
## Neg Pred Value : 0.5849
## Prevalence : 0.4135
## Detection Rate : 0.2019
## Detection Prevalence : 0.4904
## Balanced Accuracy : 0.4983
##
## 'Positive' Class : Down
##
(h) Which of these methods appears to provide the best results on this data?
(i) Experiment with different combinations of predictors, including possible transformations and interactions, for each of the methods. Report the variables, method, and associated confusion matrix that appears to provide the best results on the held out data. Note that you should also experiment with values for K in the KNN classifier.
Weekly.trainC <-Weekly[(Year<2001),]
Weekly.testC <-Weekly[!(Year<2007),]
Logistic regression
logmodel = glm(Direction~Lag2 + Lag4 + Lag5, data = Weekly.trainC, family=binomial)
logmodel.pred = predict(logmodel, Weekly.testC, type ="response")
logmodel.pred = ifelse(logmodel.pred >=0.5, "Up","Down")
caret::confusionMatrix(as.factor(logmodel.pred), as.factor(Weekly.testC$Direction))
## Confusion Matrix and Statistics
##
## Reference
## Prediction Down Up
## Down 11 15
## Up 85 98
##
## Accuracy : 0.5215
## 95% CI : (0.4515, 0.5909)
## No Information Rate : 0.5407
## P-Value [Acc > NIR] : 0.7342
##
## Kappa : -0.0192
##
## Mcnemar's Test P-Value : 5.2e-12
##
## Sensitivity : 0.11458
## Specificity : 0.86726
## Pos Pred Value : 0.42308
## Neg Pred Value : 0.53552
## Prevalence : 0.45933
## Detection Rate : 0.05263
## Detection Prevalence : 0.12440
## Balanced Accuracy : 0.49092
##
## 'Positive' Class : Down
##
LDA
ldamodel = lda(Direction~Lag2 + Lag4 + Lag5, data=Weekly.trainC,family=binomial)
ldaWeekly.pred = predict(lda.fit,Weekly.testC)
caret::confusionMatrix(as.factor(ldaWeekly.pred$class), as.factor(Weekly.testC$Direction))
## Confusion Matrix and Statistics
##
## Reference
## Prediction Down Up
## Down 17 10
## Up 79 103
##
## Accuracy : 0.5742
## 95% CI : (0.5041, 0.6421)
## No Information Rate : 0.5407
## P-Value [Acc > NIR] : 0.1836
##
## Kappa : 0.0937
##
## Mcnemar's Test P-Value : 5.679e-13
##
## Sensitivity : 0.17708
## Specificity : 0.91150
## Pos Pred Value : 0.62963
## Neg Pred Value : 0.56593
## Prevalence : 0.45933
## Detection Rate : 0.08134
## Detection Prevalence : 0.12919
## Balanced Accuracy : 0.54429
##
## 'Positive' Class : Down
##
QDA
qdamodel = qda(Direction~Lag2 + Lag4 + Lag5, data=Weekly.trainC,family=binomial)
qdaWeekly.pred = predict(qda.fit,Weekly.testC)
caret::confusionMatrix(as.factor(qdaWeekly.pred$class), as.factor(Weekly.testC$Direction))
## Confusion Matrix and Statistics
##
## Reference
## Prediction Down Up
## Down 0 0
## Up 96 113
##
## Accuracy : 0.5407
## 95% CI : (0.4706, 0.6096)
## No Information Rate : 0.5407
## P-Value [Acc > NIR] : 0.5284
##
## Kappa : 0
##
## Mcnemar's Test P-Value : <2e-16
##
## Sensitivity : 0.0000
## Specificity : 1.0000
## Pos Pred Value : NaN
## Neg Pred Value : 0.5407
## Prevalence : 0.4593
## Detection Rate : 0.0000
## Detection Prevalence : 0.0000
## Balanced Accuracy : 0.5000
##
## 'Positive' Class : Down
##
KNN
knntrain = data.frame(Weekly.trainC$Lag2,Weekly.trainC$Lag4,Weekly.trainC$Lag5)
knntest = data.frame(Weekly.testC$Lag2,Weekly.testC$Lag4,Weekly.testC$Lag5)
set.seed(1)
knnpred = knn(knntrain, knntest, Weekly.trainC$Direction, k=10)
caret::confusionMatrix(as.factor(knnpred),as.factor(Weekly.testC$Direction))
## Confusion Matrix and Statistics
##
## Reference
## Prediction Down Up
## Down 39 36
## Up 57 77
##
## Accuracy : 0.555
## 95% CI : (0.4849, 0.6236)
## No Information Rate : 0.5407
## P-Value [Acc > NIR] : 0.36496
##
## Kappa : 0.0891
##
## Mcnemar's Test P-Value : 0.03809
##
## Sensitivity : 0.4062
## Specificity : 0.6814
## Pos Pred Value : 0.5200
## Neg Pred Value : 0.5746
## Prevalence : 0.4593
## Detection Rate : 0.1866
## Detection Prevalence : 0.3589
## Balanced Accuracy : 0.5438
##
## 'Positive' Class : Down
##
Lag2,Lag4, and Lag5, except KNN a tinny bit improved, other models performance went down to a low value compere to their corresponding previous models.It appears that for this data, LDA provides the best results of the methods that we have examined so far.detach(Weekly)
In this problem, you will develop a model to predict whether a given car gets high or low gas mileage based on the Auto data set.
data(Auto)
head(Auto)
## mpg cylinders displacement horsepower weight acceleration year origin
## 1 18 8 307 130 3504 12.0 70 1
## 2 15 8 350 165 3693 11.5 70 1
## 3 18 8 318 150 3436 11.0 70 1
## 4 16 8 304 150 3433 12.0 70 1
## 5 17 8 302 140 3449 10.5 70 1
## 6 15 8 429 198 4341 10.0 70 1
## name
## 1 chevrolet chevelle malibu
## 2 buick skylark 320
## 3 plymouth satellite
## 4 amc rebel sst
## 5 ford torino
## 6 ford galaxie 500
attach(Auto)
summary(Auto)
## mpg cylinders displacement horsepower weight
## Min. : 9.00 Min. :3.000 Min. : 68.0 Min. : 46.0 Min. :1613
## 1st Qu.:17.00 1st Qu.:4.000 1st Qu.:105.0 1st Qu.: 75.0 1st Qu.:2225
## Median :22.75 Median :4.000 Median :151.0 Median : 93.5 Median :2804
## Mean :23.45 Mean :5.472 Mean :194.4 Mean :104.5 Mean :2978
## 3rd Qu.:29.00 3rd Qu.:8.000 3rd Qu.:275.8 3rd Qu.:126.0 3rd Qu.:3615
## Max. :46.60 Max. :8.000 Max. :455.0 Max. :230.0 Max. :5140
##
## acceleration year origin name
## Min. : 8.00 Min. :70.00 Min. :1.000 amc matador : 5
## 1st Qu.:13.78 1st Qu.:73.00 1st Qu.:1.000 ford pinto : 5
## Median :15.50 Median :76.00 Median :1.000 toyota corolla : 5
## Mean :15.54 Mean :75.98 Mean :1.577 amc gremlin : 4
## 3rd Qu.:17.02 3rd Qu.:79.00 3rd Qu.:2.000 amc hornet : 4
## Max. :24.80 Max. :82.00 Max. :3.000 chevrolet chevette: 4
## (Other) :365
(a) Create a binary variable, mpg01, that contains a 1 if mpg contains a value above its median, and a 0 if mpg contains a value below its median. You can compute the median using the median() function. Note you may find it helpful to use the data.frame() function to create a single data set containing both mpg01 and the other Auto variables.
sum(is.na(Auto))
## [1] 0
mpg01 = ifelse(mpg>=median(mpg),1,0)
Auto =data.frame(Auto,mpg01)
(b) Explore the data graphically in order to investigate the association between mpg01 and the other features. Which of the other features seem most likely to be useful in predicting mpg01? Scatterplots and boxplots may be useful tools to answer this question.Describe your findings.
chart.Correlation(cor(Auto[,-9]), histogram = FALSE, pch=19)
* From the correlation chart, all variables in Auto dataset either negative or positive correlate moderately with mpg01.
(c) Split the data into a training set and a test set.
train <- (year %% 2 == 0)
train.auto <- Auto[train,]
test.auto <- Auto[-train,]
(d) Perform LDA on the training data in order to predict mpg01 using the variables that seemed most associated with mpg01 in (b). What is the test error of the model obtained?
autolda.fit <- lda(mpg01~ displacement + horsepower + weight + year+ cylinders + origin, data=train.auto)
autolda.pred <- predict(autolda.fit, test.auto)
table(autolda.pred$class, test.auto$mpg01)
##
## 0 1
## 0 169 7
## 1 26 189
mean(autolda.pred$class != test.auto$mpg01)
## [1] 0.08439898
(e) Perform QDA on the training data in order to predict mpg01 using the variables that seemed most associated with mpg01 in (b). What is the test error of the model obtained?
autoqda.fit <- qda(mpg01~ displacement + horsepower + weight + year+ cylinders + origin, data=train.auto)
autoqda.pred <- predict(autoqda.fit, test.auto)
table(autoqda.pred$class, test.auto$mpg01)
##
## 0 1
## 0 176 20
## 1 19 176
mean(autoqda.pred$class != test.auto$mpg01)
## [1] 0.09974425
(f) Perform logistic regression on the training data in order to predict mpg01 using the variables that seemed most associated with mpg01 in (b). What is the test error of the model obtained?
auto.fit<-glm(mpg01 ~ displacement + horsepower + weight + year+ cylinders + origin, data=train.auto,family=binomial)
auto.probs = predict(auto.fit, test.auto, type = "response")
auto.pred = rep(0, length(auto.probs))
auto.pred[auto.probs > 0.5] = 1
table(auto.pred, test.auto$mpg01)
##
## auto.pred 0 1
## 0 174 12
## 1 21 184
mean(auto.pred != test.auto$mpg01)
## [1] 0.08439898
(g) Perform KNN on the training data, with several values of K, in order to predict mpg01. Use only the variables that seemed most associated with mpg01 in (b). What test errors do you obtain? Which value of K seems to perform the best on this data set?
train.K= cbind(displacement,horsepower,weight,cylinders,year, origin)[train,]
test.K=cbind(displacement,horsepower,weight,cylinders, year, origin)[-train,]
set.seed(1)
autok.pred=knn(train.K,test.K,train.auto$mpg01,k=1)
mean(autok.pred != test.auto$mpg01)
## [1] 0.07161125
autok.pred=knn(train.K,test.K,train.auto$mpg01,k=5)
mean(autok.pred != test.auto$mpg01)
## [1] 0.112532
autok.pred=knn(train.K,test.K,train.auto$mpg01,k=10)
mean(autok.pred != test.auto$mpg01)
## [1] 0.1253197
detach(Auto)
Using the Boston data set, fit classification models in order to predict whether a given suburb has a crime rate above or below the median.Explore logistic regression, LDA, and KNN models using various subsets of the predictors. Describe your findings.
data(Boston)
summary(Boston)
## crim zn indus chas
## Min. : 0.00632 Min. : 0.00 Min. : 0.46 Min. :0.00000
## 1st Qu.: 0.08205 1st Qu.: 0.00 1st Qu.: 5.19 1st Qu.:0.00000
## Median : 0.25651 Median : 0.00 Median : 9.69 Median :0.00000
## Mean : 3.61352 Mean : 11.36 Mean :11.14 Mean :0.06917
## 3rd Qu.: 3.67708 3rd Qu.: 12.50 3rd Qu.:18.10 3rd Qu.:0.00000
## Max. :88.97620 Max. :100.00 Max. :27.74 Max. :1.00000
## nox rm age dis
## Min. :0.3850 Min. :3.561 Min. : 2.90 Min. : 1.130
## 1st Qu.:0.4490 1st Qu.:5.886 1st Qu.: 45.02 1st Qu.: 2.100
## Median :0.5380 Median :6.208 Median : 77.50 Median : 3.207
## Mean :0.5547 Mean :6.285 Mean : 68.57 Mean : 3.795
## 3rd Qu.:0.6240 3rd Qu.:6.623 3rd Qu.: 94.08 3rd Qu.: 5.188
## Max. :0.8710 Max. :8.780 Max. :100.00 Max. :12.127
## rad tax ptratio black
## Min. : 1.000 Min. :187.0 Min. :12.60 Min. : 0.32
## 1st Qu.: 4.000 1st Qu.:279.0 1st Qu.:17.40 1st Qu.:375.38
## Median : 5.000 Median :330.0 Median :19.05 Median :391.44
## Mean : 9.549 Mean :408.2 Mean :18.46 Mean :356.67
## 3rd Qu.:24.000 3rd Qu.:666.0 3rd Qu.:20.20 3rd Qu.:396.23
## Max. :24.000 Max. :711.0 Max. :22.00 Max. :396.90
## lstat medv
## Min. : 1.73 Min. : 5.00
## 1st Qu.: 6.95 1st Qu.:17.02
## Median :11.36 Median :21.20
## Mean :12.65 Mean :22.53
## 3rd Qu.:16.95 3rd Qu.:25.00
## Max. :37.97 Max. :50.00
attach(Boston)
Creating binary crim variable.
crime01 =ifelse(crim > median(crim), 1,0)
Boston= data.frame(Boston,crime01)
Splitting the dataset
train = 1:(dim(Boston)[1]/2)
test = (dim(Boston)[1]/2 + 1):dim(Boston)[1]
Boston.train = Boston[train, ]
Boston.test = Boston[test, ]
crime01.test = crime01[test]
Determination of any associations to crime01
chart.Correlation(cor(Boston), histogram = FALSE, pch=19)
Logistic Regression
set.seed(1)
Boston.fit <-glm(crime01~ indus+nox+age+dis+rad+tax, data=Boston.train,family=binomial)
Boston.probs = predict(Boston.fit, Boston.test, type = "response")
Boston.pred = rep(0, length(Boston.probs))
Boston.pred[Boston.probs > 0.5] = 1
table(Boston.pred, crime01.test)
## crime01.test
## Boston.pred 0 1
## 0 75 8
## 1 15 155
mean(Boston.pred != crime01.test)
## [1] 0.09090909
summary(Boston.fit)
##
## Call:
## glm(formula = crime01 ~ indus + nox + age + dis + rad + tax,
## family = binomial, data = Boston.train)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.97810 -0.21406 -0.03454 0.47107 3.04502
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -42.214032 7.617440 -5.542 2.99e-08 ***
## indus -0.213126 0.073236 -2.910 0.00361 **
## nox 80.868029 16.066473 5.033 4.82e-07 ***
## age 0.003397 0.012032 0.282 0.77772
## dis 0.307145 0.190502 1.612 0.10690
## rad 0.847236 0.183767 4.610 4.02e-06 ***
## tax -0.013760 0.004956 -2.777 0.00549 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 329.37 on 252 degrees of freedom
## Residual deviance: 144.44 on 246 degrees of freedom
## AIC: 158.44
##
## Number of Fisher Scoring iterations: 8
Linear Discriminat Analysis
Boston.ldafit <-lda(crime01~ indus+nox+age+dis+rad+tax, data=Boston.train,family=binomial)
Bostonlda.pred = predict(Boston.ldafit, Boston.test)
table(Bostonlda.pred$class, crime01.test)
## crime01.test
## 0 1
## 0 81 18
## 1 9 145
mean(Bostonlda.pred$class != crime01.test)
## [1] 0.1067194
K Nearest Neighbors
#K=1
train.K=cbind(indus,nox,age,dis,rad,tax)[train,]
test.K=cbind(indus,nox,age,dis,rad,tax)[test,]
Bosknn.pred=knn(train.K, test.K, crime01.test, k=1)
table(Bosknn.pred,crime01.test)
## crime01.test
## Bosknn.pred 0 1
## 0 31 155
## 1 59 8
mean(Bosknn.pred !=crime01.test)
## [1] 0.8458498
#K=10
train.K=cbind(indus,nox,age,dis,rad,tax)[train,]
test.K=cbind(indus,nox,age,dis,rad,tax)[test,]
Bosknn.pred=knn(train.K, test.K, crime01.test, k=10)
table(Bosknn.pred,crime01.test)
## crime01.test
## Bosknn.pred 0 1
## 0 42 8
## 1 48 155
mean(Bosknn.pred !=crime01.test)
## [1] 0.2213439
#K=100
train.K=cbind(indus,nox,age,dis,rad,tax)[train,]
test.K=cbind(indus,nox,age,dis,rad,tax)[test,]
Bosknn.pred=knn(train.K, test.K, crime01.test, k=100)
table(Bosknn.pred,crime01.test)
## crime01.test
## Bosknn.pred 0 1
## 0 20 6
## 1 70 157
mean(Bosknn.pred !=crime01.test)
## [1] 0.3003953