Extract the dataset and create a new category “crime factor”

Boston <- Boston %>%
  mutate(chas = factor(chas),
         crime_factor = factor(ifelse(crim > median(crim), 
                                      'High', 'Low'), 
                               levels = c('High', 'Low')))

Correlation plot

cor_test <- Boston %>%
  select(-chas, -crime_factor) %>%
  cor.mtest(conf.level = .95)

Boston %>%
  select(-chas, -crime_factor) %>%
  cor %>%
  corrplot(method = 'color', 
           order = 'hclust', addrect = 2,
           tl.col = 'black', addCoef.col = 'black', number.cex = 0.65,
           p.mat = cor_test$p, sig.level = .05)

Faceted Boxplots

Boston %>%
  dplyr::select(zn:crime_factor) %>%
  gather(value_type, value, -crime_factor, -chas) %>%
  ggplot(aes(value_type, value, fill = crime_factor)) +
  geom_boxplot(alpha = 0.5) +
  facet_wrap(~value_type, scales = 'free') +
  scale_fill_discrete(name = 'Crime Rate') +
  theme(legend.position = 'top')

Given both the correlation matrix, we can see there are some predictors that are more correlated with the “crime_factor” we created: rad, tax, nox, indus, age and istat.

Regarding the boxplot, some standout as highly separated: rad, tax, nox, indus and age.

Train/Test Split

set.seed(123)
train_size <- nrow(Boston) * 0.75
inTrain <- sample(1:nrow(Boston), size = train_size)
train <- Boston[inTrain,]
test <- Boston[-inTrain,]

Establishing different predictor subsets

We will create a subset with the most correlated predictors we measured, and another one with mixed predictors:

subset_strong = (rad + nox + tax + age + indus), subset_mixed = (rad + nox + tax + black + dis)

Logistic Regression subset_strong

lr_model <- glm(crime_factor ~ rad + nox + tax + age + indus, data = train, family = binomial)

lr_model %>% tidy
## # A tibble: 6 × 5
##   term         estimate std.error statistic       p.value
##   <chr>           <dbl>     <dbl>     <dbl>         <dbl>
## 1 (Intercept)  19.2       3.16        6.05  0.00000000144
## 2 rad          -0.543     0.133      -4.09  0.0000434    
## 3 nox         -33.8       6.80       -4.97  0.000000686  
## 4 tax           0.00885   0.00279     3.17  0.00153      
## 5 age          -0.0172    0.0102     -1.69  0.0909       
## 6 indus         0.0181    0.0524      0.344 0.730
pred <- predict(lr_model, test, type = 'response')
pred_value <- ifelse(pred >= 0.5, 'Low', 'High')
acc <- mean(pred_value == test$crime_factor)

table(pred_value, test$crime_factor) %>%
  kable(format = 'html') %>%
  kable_styling() %>%
  add_header_above(c('Predicted' = 1, 'Observed' = 2)) %>%
  column_spec(1, bold = T) %>%
  add_footnote(label = acc)
Predicted
Observed
High Low
High 58 8
Low 8 53
a 0.874015748031496

Logistic Regression subset_mixed

lr_model <- glm(crime_factor ~ rad + nox + tax + black + dis, data = train, family = binomial)

lr_model %>% tidy
## # A tibble: 6 × 5
##   term        estimate std.error statistic       p.value
##   <chr>          <dbl>     <dbl>     <dbl>         <dbl>
## 1 (Intercept)   8.15     6.07        1.34  0.179        
## 2 rad          -0.546    0.126      -4.33  0.0000150    
## 3 nox         -37.0      6.24       -5.94  0.00000000286
## 4 tax           0.0100   0.00268     3.74  0.000187     
## 5 black         0.0300   0.0124      2.42  0.0155       
## 6 dis          -0.0763   0.164      -0.464 0.643
pred <- predict(lr_model, test, type = 'response')
pred_value <- ifelse(pred >= 0.5, 'Low', 'High')
acc <- mean(pred_value == test$crime_factor)

table(pred_value, test$crime_factor) %>%
  kable(format = 'html') %>%
  kable_styling() %>%
  add_header_above(c('Predicted' = 1, 'Observed' = 2)) %>%
  column_spec(1, bold = T) %>%
  add_footnote(label = acc)
Predicted
Observed
High Low
High 60 6
Low 6 55
a 0.905511811023622

LDA subset_strong

lda_model <- lda(crime_factor ~ rad + nox + tax + age + indus, data = train)
pred <- predict(lda_model, test)
acc <- mean(pred$class == test$crime_factor)

table(pred$class, test$crime_factor) %>%
  kable(format = 'html') %>%
  kable_styling() %>%
  add_header_above(c('Predicted' = 1, 'Observed' = 2)) %>%
  column_spec(1, bold = T) %>%
  add_footnote(label = acc)
Predicted
Observed
High Low
High 50 4
Low 16 57
a 0.84251968503937

LDA subset_mixed

lda_model <- lda(crime_factor ~ rad + nox + tax + black + dis, data = train)
pred <- predict(lda_model, test)
acc <- mean(pred$class == test$crime_factor)

table(pred$class, test$crime_factor) %>%
  kable(format = 'html') %>%
  kable_styling() %>%
  add_header_above(c('Predicted' = 1, 'Observed' = 2)) %>%
  column_spec(1, bold = T) %>%
  add_footnote(label = acc)
Predicted
Observed
High Low
High 52 5
Low 14 56
a 0.850393700787402

QDA subset_strong

qda_model <- qda(crime_factor ~ rad + nox + tax + age + indus, data = train)
pred <- predict(qda_model, test)
acc <- mean(pred$class == test$crime_factor) 

table(pred$class, test$crime_factor) %>%
  kable(format = 'html') %>%
  kable_styling() %>%
  add_header_above(c('Predicted' = 1, 'Observed' = 2)) %>%
  column_spec(1, bold = T) %>%
  add_footnote(label = acc)
Predicted
Observed
High Low
High 51 4
Low 15 57
a 0.850393700787402

QDA subset_mixed

qda_model <- qda(crime_factor ~ rad + nox + tax + black + dis, data = train)
pred <- predict(qda_model, test)
acc <- mean(pred$class == test$crime_factor) 

table(pred$class, test$crime_factor) %>%
  kable(format = 'html') %>%
  kable_styling() %>%
  add_header_above(c('Predicted' = 1, 'Observed' = 2)) %>%
  column_spec(1, bold = T) %>%
  add_footnote(label = acc)
Predicted
Observed
High Low
High 47 1
Low 19 60
a 0.84251968503937

KNN subset_strong

require(class)
## Loading required package: class
variables <- c('rad', 'nox', 'tax', 'age', 'indus')

x_train <- train[, variables]
y_train <- train$crime_factor
x_test <- test[, variables]
acc <- list()

knn_pred <- knn(train = x_train, test = x_test, cl = y_train, k = 1)
acc <- mean(knn_pred == test$crime_factor)

table(knn_pred, test$crime_factor) %>%
  kable(format = 'html') %>%
  kable_styling() %>%
  add_header_above(c('Predicted' = 1, 'Observed' = 2)) %>%
  column_spec(1, bold = T) %>%
  add_footnote(label = acc)
Predicted
Observed
High Low
High 59 5
Low 7 56
a 0.905511811023622

KNN subset_mixed

variables <- c('rad', 'nox', 'tax', 'black', 'dis')

x_train <- train[, variables]
y_train <- train$crime_factor
x_test <- test[, variables]
acc <- list()

knn_pred <- knn(train = x_train, test = x_test, cl = y_train, k = 1)
acc <- mean(knn_pred == test$crime_factor)

table(knn_pred, test$crime_factor) %>%
  kable(format = 'html') %>%
  kable_styling() %>%
  add_header_above(c('Predicted' = 1, 'Observed' = 2)) %>%
  column_spec(1, bold = T) %>%
  add_footnote(label = acc)
Predicted
Observed
High Low
High 64 5
Low 2 56
a 0.94488188976378

The results are unexpectedly better in general for the Mixed subset. I’m not very sure about the conclusion, but it would seem there’s not a linear relation between the features and the target class.