AM HW8
ISYE 6501: Homework 8
2023-03-07
Question 11.1, Part 1: Stepwise Regression
Using the crime data set uscrime.txt from Questions 8.2, 9.1, and 10.1, build a
regression model using: 1. Stepwise regression
We begin by retrieving and preparing our data as we did in previous weeks. As part of our
data preparation, we take the following steps:
• identify and remove outliers,
• replace them using a K-means imputation method, and
• scale data before and after imputation.
# Url address
url <- "http://www.statsci.org/data/general/uscrime.txt"
# Destination File location
destfile <- "C:/Users/arvin/Downloads/uscrime.txt"
# Script to download and save file
download.file(url, destfile)
# Convert file to table
cdf <- read.table("C:/Users/arvin/Downloads/uscrime.txt", header = TRUE)
# Preview the data
head(cdf)
## M So Ed Po1 Po2 LF M.F NW U1 U2 Wealth Ineq
Pop
Prob
## 1 15.1 1 9.1 5.8 5.6 95.0 30.1 0.108 3940 26.1
0.510 33 4.1
0.084602
## 2 14.3 0 11.3 10.3 9.5 101.2 10.2 0.096 5570 19.4
0.583 13 3.6
0.029599
## 3 14.2 1 8.9 4.5 4.4 96.9 21.9 0.094 3180 25.0
0.533 18 3.3
0.083401
## 4 13.6 0 12.1 14.9 14.1 99.4 8.0 0.102 6730 16.7
0.577 157 3.9
0.015801
## 5 14.1 0 12.1 10.9 10.1 98.5 3.0 0.091 5780 17.4
0.591 18 2.0
0.041399
## 6 12.1 0 11.0 11.8 11.5 96.4 4.4 0.084 6890 12.6
0.547 25 2.9
0.034201
## Time Crime
## 1 26.2011 791
## 2 25.2999 1635
about:blan 1/
k 25
,10/15/24, 9:27 Homew ork 8 -
AM HW8
## 3 578
24.3006
## 4 1969
29.9012
## 5 1234
21.2998
## 6 682
20.9995
# Outliers
cdf_out <- boxplot.stats(cdf$Crime)$out
# Indices of outliers
cdfout_ind <- which(cdf$Crime %in% c(cdf_out))
# copy df
cdf_copy <- cdf
# Replace outliers with NA prior to imputation
cdf_copy$Crime[cdfout_ind] <- NA
# Split response from predictors
cdf_x <- cdf_copy[, -16]
cdf_y <- cdf_copy[, 16]
# Scale data for Kmeans
cdf_xs <- scale(cdf_x)
# K-means to Elbow plot to determine ideal k for
imputation # later
set.seed(77)
tot_withinss <- map_dbl(2:10, function(k) {
km_mod <- kmeans(cdf_xs, centers = k, nstart =
25) km_mod$tot.withinss
})
# Elbow point appears to be k = 3
wss <- data.frame(k = 2:10, tot_withinss = tot_withinss)
ggplot(wss, aes(k, tot_withinss)) + geom_line() +
geom_point()
about:blan 2/
k 25
, 10/15/24, 9:27 Homew ork 8 -
AM HW8
about:blan 3/
k 25