Project Background We will learn to use linear & multi-linear regression model using crime dataset. We want to know the relationship among variables, especially between the crime rate in city with other variables. We also want to predict the crime rate based on the historical data. You can download the data here crime

DATA PREPARATION

Load the requirement library packages

library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
library(GGally)
## Loading required package: ggplot2
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(MLmetrics)
## 
## Attaching package: 'MLmetrics'
## The following object is masked from 'package:base':
## 
##     Recall
library(naniar)
library(caret)
## Loading required package: lattice
## 
## Attaching package: 'caret'
## The following objects are masked from 'package:MLmetrics':
## 
##     MAE, RMSE
#The scipen value is an indicator of the integer's prompt for exponential notation
options(scipen = 100, max.print = 1e+06)

Load the Data

crime <- read.csv('crime.csv')
head(crime)
##   X   M So  Ed Po1 Po2  LF  M.F Pop  NW  U1 U2 GDP Ineq     Prob    Time    y
## 1 1 151  1  91  58  56 510  950  33 301 108 41 394  261 0.084602 26.2011  791
## 2 2 143  0 113 103  95 583 1012  13 102  96 36 557  194 0.029599 25.2999 1635
## 3 3 142  1  89  45  44 533  969  18 219  94 33 318  250 0.083401 24.3006  578
## 4 4 136  0 121 149 141 577  994 157  80 102 39 673  167 0.015801 29.9012 1969
## 5 5 141  0 121 109 101 591  985  18  30  91 20 578  174 0.041399 21.2998 1234
## 6 6 121  0 110 118 115 547  964  25  44  84 29 689  126 0.034201 20.9995  682
#Removing the column "X" due to unnecessary row numbering data 
crime <- crime %>% select(-X)
head(crime) 
##     M So  Ed Po1 Po2  LF  M.F Pop  NW  U1 U2 GDP Ineq     Prob    Time    y
## 1 151  1  91  58  56 510  950  33 301 108 41 394  261 0.084602 26.2011  791
## 2 143  0 113 103  95 583 1012  13 102  96 36 557  194 0.029599 25.2999 1635
## 3 142  1  89  45  44 533  969  18 219  94 33 318  250 0.083401 24.3006  578
## 4 136  0 121 149 141 577  994 157  80 102 39 673  167 0.015801 29.9012 1969
## 5 141  0 121 109 101 591  985  18  30  91 20 578  174 0.041399 21.2998 1234
## 6 121  0 110 118 115 547  964  25  44  84 29 689  126 0.034201 20.9995  682
rmarkdown::paged_table(crime)

Rename all the column name as of user-defined

# rename all columns
names(crime) <- c("percent_m", "is_south", "mean_education", "police_exp60", "police_exp59", "labour_participation", "m_per1000f", "state_pop", "nonwhites_per1000", "unemploy_m24", "unemploy_m39", "gdp", "inequality", "prob_prison", "time_prison", "crime_rate")

rmarkdown::paged_table(crime)

Columns Description:

Pre-liminary Data Inspection (PRE-EDA) Checking the data structured and type

str(crime)
## 'data.frame':    47 obs. of  16 variables:
##  $ percent_m           : int  151 143 142 136 141 121 127 131 157 140 ...
##  $ is_south            : int  1 0 1 0 0 0 1 1 1 0 ...
##  $ mean_education      : int  91 113 89 121 121 110 111 109 90 118 ...
##  $ police_exp60        : int  58 103 45 149 109 118 82 115 65 71 ...
##  $ police_exp59        : int  56 95 44 141 101 115 79 109 62 68 ...
##  $ labour_participation: int  510 583 533 577 591 547 519 542 553 632 ...
##  $ m_per1000f          : int  950 1012 969 994 985 964 982 969 955 1029 ...
##  $ state_pop           : int  33 13 18 157 18 25 4 50 39 7 ...
##  $ nonwhites_per1000   : int  301 102 219 80 30 44 139 179 286 15 ...
##  $ unemploy_m24        : int  108 96 94 102 91 84 97 79 81 100 ...
##  $ unemploy_m39        : int  41 36 33 39 20 29 38 35 28 24 ...
##  $ gdp                 : int  394 557 318 673 578 689 620 472 421 526 ...
##  $ inequality          : int  261 194 250 167 174 126 168 206 239 174 ...
##  $ prob_prison         : num  0.0846 0.0296 0.0834 0.0158 0.0414 ...
##  $ time_prison         : num  26.2 25.3 24.3 29.9 21.3 ...
##  $ crime_rate          : int  791 1635 578 1969 1234 682 963 1555 856 705 ...

The data has 47 rows and 16 columns. There is a variable name “is_south”, that it could be a factor data type.

Transforming is_south data type from ‘int’ into ‘factor’

crime$is_south <- as.factor (crime$is_south)
str(crime)
## 'data.frame':    47 obs. of  16 variables:
##  $ percent_m           : int  151 143 142 136 141 121 127 131 157 140 ...
##  $ is_south            : Factor w/ 2 levels "0","1": 2 1 2 1 1 1 2 2 2 1 ...
##  $ mean_education      : int  91 113 89 121 121 110 111 109 90 118 ...
##  $ police_exp60        : int  58 103 45 149 109 118 82 115 65 71 ...
##  $ police_exp59        : int  56 95 44 141 101 115 79 109 62 68 ...
##  $ labour_participation: int  510 583 533 577 591 547 519 542 553 632 ...
##  $ m_per1000f          : int  950 1012 969 994 985 964 982 969 955 1029 ...
##  $ state_pop           : int  33 13 18 157 18 25 4 50 39 7 ...
##  $ nonwhites_per1000   : int  301 102 219 80 30 44 139 179 286 15 ...
##  $ unemploy_m24        : int  108 96 94 102 91 84 97 79 81 100 ...
##  $ unemploy_m39        : int  41 36 33 39 20 29 38 35 28 24 ...
##  $ gdp                 : int  394 557 318 673 578 689 620 472 421 526 ...
##  $ inequality          : int  261 194 250 167 174 126 168 206 239 174 ...
##  $ prob_prison         : num  0.0846 0.0296 0.0834 0.0158 0.0414 ...
##  $ time_prison         : num  26.2 25.3 24.3 29.9 21.3 ...
##  $ crime_rate          : int  791 1635 578 1969 1234 682 963 1555 856 705 ...

Checking Missing Value, NA, null, or bad data

vis_miss(crime)

The database has no missing value or NA

Exploratory Data Analysis Exploratory data analysis is a phase where we explore the data variables, see if there are any pattern that can indicate any kind of correlation between variables.

#Create a heat map correlation of each variable 
ggcorr(crime, label = T, label_size = 2.9, hjust = 1, layout.exp = 3)
## Warning in ggcorr(crime, label = T, label_size = 2.9, hjust = 1, layout.exp =
## 3): data in column(s) 'is_south' are not numeric and were ignored

What the insight ? The graphic shows that only police_exp59, police_exp60, gdp, and prob_prison that have strong correlation with the crime rate target variables.

MODELLING

Before we can build the model, we must divide the data into train and test datasets. The train dataset will be used to train the linear regression model. The test dataset will be used as a comparison to check if the model becomes overfit and is unable to predict new data that was not encountered during the training phase. We will use 70% of the data for training and the remaining 30% for testing.

set.seed(123)

samplesize <- round(0.7 * nrow(crime), 0)
index <- sample(seq_len(nrow(crime)), size = samplesize)

data_train <- crime[index, ]
data_test <- crime[-index, ]

What’s next? We will now attempt to model the linear regression with crime rate as the target variable.

crime_lm_all <- lm(formula = crime_rate ~ . , data = data_train)
summary(crime_lm_all)
## 
## Call:
## lm(formula = crime_rate ~ ., data = data_train)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -304.71  -91.82  -31.14  101.84  426.16 
## 
## Coefficients:
##                         Estimate  Std. Error t value Pr(>|t|)    
## (Intercept)          -10332.3968   2232.6587  -4.628  0.00024 ***
## percent_m                 9.6677      4.4748   2.160  0.04530 *  
## is_south1                 8.1011    223.3111   0.036  0.97148    
## mean_education           26.2959      7.6978   3.416  0.00329 ** 
## police_exp60             20.0908     11.8628   1.694  0.10858    
## police_exp59            -14.8569     13.5366  -1.098  0.28771    
## labour_participation     -4.6592      2.1721  -2.145  0.04669 *  
## m_per1000f                8.2657      2.8606   2.889  0.01019 *  
## state_pop                 1.3699      1.7925   0.764  0.45521    
## nonwhites_per1000         1.3334      0.9991   1.335  0.19960    
## unemploy_m24            -11.9795      5.1323  -2.334  0.03212 *  
## unemploy_m39             14.2389      9.8688   1.443  0.16724    
## gdp                       0.6436      1.2624   0.510  0.61671    
## inequality                4.2472      2.8552   1.488  0.15518    
## prob_prison             940.1481   3823.0185   0.246  0.80869    
## time_prison              14.7756      9.4304   1.567  0.13558    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 199.9 on 17 degrees of freedom
## Multiple R-squared:  0.8813, Adjusted R-squared:  0.7766 
## F-statistic: 8.414 on 15 and 17 DF,  p-value: 0.00003846

What’s Insight? The summary of the crime_lm_all model contains a wealth of information. However, for the time being, we should concentrate on the Pr(>|t|). This column displays the variable’s significance to the model. If the value is less than 0.05, we may reasonably assume that the variable has a significant effect on the model (i.e., the estimated coefficient is equal to 0), and vice versa. As a result, we may simplify our model by deleting variables with p-values greater than 0.05, as they have no meaningful effect on our model. Each variable’s coefficient is represented by the estimate value. To interpret the value of each coefficient, consider that every unit-point increase in timeprison contributes to a 14.7756 rise in crime rate. [[percent_m, mean_education, labour_participation, m_per1000f, unemploy_m24]]

crime2 <- crime %>% select(percent_m, mean_education, labour_participation, m_per1000f, unemploy_m24, crime_rate, police_exp59, police_exp60, gdp, prob_prison) #We will try to combine the most significant parameters both of heat map corr and crime_lm_all 

data_train2 <- crime2[index, ] #Training Data
data_test2 <- crime2[-index, ] #Testing Data

set.seed(123)
crime_lm2 <- lm(crime_rate ~ ., data = data_train2)

summary(crime_lm2)
## 
## Call:
## lm(formula = crime_rate ~ ., data = data_train2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -502.86  -90.21    5.78  123.33  617.32 
## 
## Coefficients:
##                        Estimate Std. Error t value Pr(>|t|)  
## (Intercept)          -4642.0119  1855.3833  -2.502   0.0199 *
## percent_m               11.5842     5.2653   2.200   0.0381 *
## mean_education           5.2396     8.2333   0.636   0.5308  
## labour_participation    -2.5121     2.0916  -1.201   0.2420  
## m_per1000f               4.9419     2.6199   1.886   0.0719 .
## unemploy_m24            -6.0904     4.5863  -1.328   0.1972  
## police_exp59           -14.2331    16.2524  -0.876   0.3902  
## police_exp60            24.0697    14.8871   1.617   0.1196  
## gdp                     -0.5492     1.4152  -0.388   0.7015  
## prob_prison          -2241.2811  3689.5754  -0.607   0.5495  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 270.1 on 23 degrees of freedom
## Multiple R-squared:  0.7067, Adjusted R-squared:  0.592 
## F-statistic: 6.159 on 9 and 23 DF,  p-value: 0.000205

After adjusting the model (crime_lm2), we got the updated P-value 0,000205 < 0,05, while the adjusted R-squared is only 0.593, still below the best practice standard. Hence, we need to conduct stepwise regresion evaluation (backward, forward, and both)

##Feature Selection Using Step-wise Regression

Step-wise regression assists us in selecting suitable predictors by searching for the optimal model based on the AIC value.

There are three techniques to step-wise regression: 1. Backward 2. Forward 3. Both

Backward for model crime_lm2

#stepwise regression: backward elimination
model_backward <- step(object = crime_lm2, direction = "backward")
## Start:  AIC=377.6
## crime_rate ~ percent_m + mean_education + labour_participation + 
##     m_per1000f + unemploy_m24 + police_exp59 + police_exp60 + 
##     gdp + prob_prison
## 
##                        Df Sum of Sq     RSS    AIC
## - gdp                   1     10987 1688547 375.81
## - prob_prison           1     26915 1704474 376.12
## - mean_education        1     29539 1707099 376.18
## - police_exp59          1     55939 1733499 376.68
## <none>                              1677560 377.60
## - labour_participation  1    105213 1782773 377.61
## - unemploy_m24          1    128625 1806184 378.04
## - police_exp60          1    190665 1868225 379.15
## - m_per1000f            1    259515 1937075 380.35
## - percent_m             1    353055 2030615 381.90
## 
## Step:  AIC=375.81
## crime_rate ~ percent_m + mean_education + labour_participation + 
##     m_per1000f + unemploy_m24 + police_exp59 + police_exp60 + 
##     prob_prison
## 
##                        Df Sum of Sq     RSS    AIC
## - prob_prison           1     16313 1704860 374.13
## - mean_education        1     18554 1707101 374.18
## - police_exp59          1     57107 1745654 374.91
## - labour_participation  1    101695 1790241 375.74
## <none>                              1688547 375.81
## - unemploy_m24          1    139564 1828110 376.44
## - police_exp60          1    185608 1874154 377.26
## - m_per1000f            1    260714 1949260 378.55
## - percent_m             1    420833 2109380 381.16
## 
## Step:  AIC=374.13
## crime_rate ~ percent_m + mean_education + labour_participation + 
##     m_per1000f + unemploy_m24 + police_exp59 + police_exp60
## 
##                        Df Sum of Sq     RSS    AIC
## - mean_education        1     18748 1723607 372.49
## - police_exp59          1     52194 1757053 373.13
## - labour_participation  1     90130 1794989 373.83
## <none>                              1704860 374.13
## - unemploy_m24          1    127415 1832275 374.51
## - police_exp60          1    184513 1889372 375.52
## - m_per1000f            1    245605 1950464 376.57
## - percent_m             1    422426 2127285 379.44
## 
## Step:  AIC=372.49
## crime_rate ~ percent_m + labour_participation + m_per1000f + 
##     unemploy_m24 + police_exp59 + police_exp60
## 
##                        Df Sum of Sq     RSS    AIC
## - police_exp59          1     36118 1759725 371.18
## - labour_participation  1     71965 1795573 371.84
## <none>                              1723607 372.49
## - unemploy_m24          1    122773 1846381 372.76
## - police_exp60          1    165983 1889590 373.53
## - m_per1000f            1    273627 1997234 375.36
## - percent_m             1    423491 2147098 377.74
## 
## Step:  AIC=371.18
## crime_rate ~ percent_m + labour_participation + m_per1000f + 
##     unemploy_m24 + police_exp60
## 
##                        Df Sum of Sq     RSS    AIC
## - labour_participation  1     52038 1811764 370.14
## - unemploy_m24          1     99868 1859593 371.00
## <none>                              1759725 371.18
## - m_per1000f            1    262116 2021841 373.76
## - percent_m             1    446087 2205812 376.63
## - police_exp60          1   3315095 5074820 404.13
## 
## Step:  AIC=370.14
## crime_rate ~ percent_m + m_per1000f + unemploy_m24 + police_exp60
## 
##                Df Sum of Sq     RSS    AIC
## - unemploy_m24  1     47868 1859632 369.00
## <none>                      1811764 370.14
## - m_per1000f    1    254298 2066062 372.47
## - percent_m     1    477451 2289214 375.86
## - police_exp60  1   3367626 5179390 402.80
## 
## Step:  AIC=369
## crime_rate ~ percent_m + m_per1000f + police_exp60
## 
##                Df Sum of Sq     RSS    AIC
## <none>                      1859632 369.00
## - m_per1000f    1    208254 2067886 370.50
## - percent_m     1    553572 2413204 375.60
## - police_exp60  1   3612637 5472269 402.62

Forward for model crime_lm2

# model without predictor
crime_model_none <- lm(crime_rate ~ 1, data = crime)

model_forward <- step(object = crime_model_none,
                      direction = "forward",
                      scope = list(lower = crime_model_none, upper = crime_lm2))
## Start:  AIC=561.02
## crime_rate ~ 1
## 
##                        Df Sum of Sq     RSS    AIC
## + police_exp60          1   3253302 3627626 532.94
## + police_exp59          1   3058626 3822302 535.39
## + gdp                   1   1340152 5540775 552.84
## + prob_prison           1   1257075 5623853 553.54
## + mean_education        1    717146 6163781 557.85
## + m_per1000f            1    314867 6566061 560.82
## <none>                              6880928 561.02
## + labour_participation  1    245446 6635482 561.32
## + percent_m             1     55084 6825844 562.65
## + unemploy_m24          1     17533 6863395 562.90
## 
## Step:  AIC=532.94
## crime_rate ~ police_exp60
## 
##                        Df Sum of Sq     RSS    AIC
## + percent_m             1    616741 3010885 526.18
## + m_per1000f            1    250522 3377104 531.57
## + gdp                   1    180872 3446754 532.53
## <none>                              3627626 532.94
## + police_exp59          1    146167 3481459 533.00
## + prob_prison           1     92278 3535348 533.72
## + labour_participation  1     77479 3550147 533.92
## + unemploy_m24          1      2878 3624748 534.90
## + mean_education        1       767 3626859 534.93
## 
## Step:  AIC=526.18
## crime_rate ~ police_exp60 + percent_m
## 
##                        Df Sum of Sq     RSS    AIC
## + m_per1000f            1    261264 2749621 523.91
## + prob_prison           1    189432 2821452 525.12
## + labour_participation  1    138481 2872404 525.96
## <none>                              3010885 526.18
## + police_exp59          1     88881 2922004 526.77
## + mean_education        1     84861 2926024 526.83
## + unemploy_m24          1     31826 2979059 527.68
## + gdp                   1       771 3010113 528.17
## 
## Step:  AIC=523.91
## crime_rate ~ police_exp60 + percent_m + m_per1000f
## 
##                        Df Sum of Sq     RSS    AIC
## + prob_prison           1    173170 2576451 522.85
## <none>                              2749621 523.91
## + police_exp59          1     62422 2687199 524.83
## + gdp                   1     31879 2717742 525.36
## + labour_participation  1     16070 2733551 525.64
## + mean_education        1      1118 2748503 525.89
## + unemploy_m24          1        80 2749541 525.91
## 
## Step:  AIC=522.85
## crime_rate ~ police_exp60 + percent_m + m_per1000f + prob_prison
## 
##                        Df Sum of Sq     RSS    AIC
## <none>                              2576451 522.85
## + gdp                   1    101752 2474698 522.96
## + police_exp59          1     65526 2510925 523.64
## + labour_participation  1      1385 2575066 524.83
## + mean_education        1      1334 2575116 524.83
## + unemploy_m24          1        14 2576436 524.85

Both for model crime_lm2

model_both <- step(object = crime_model_none, direction = "both", scope = list(upper = crime_lm2))
## Start:  AIC=561.02
## crime_rate ~ 1
## 
##                        Df Sum of Sq     RSS    AIC
## + police_exp60          1   3253302 3627626 532.94
## + police_exp59          1   3058626 3822302 535.39
## + gdp                   1   1340152 5540775 552.84
## + prob_prison           1   1257075 5623853 553.54
## + mean_education        1    717146 6163781 557.85
## + m_per1000f            1    314867 6566061 560.82
## <none>                              6880928 561.02
## + labour_participation  1    245446 6635482 561.32
## + percent_m             1     55084 6825844 562.65
## + unemploy_m24          1     17533 6863395 562.90
## 
## Step:  AIC=532.94
## crime_rate ~ police_exp60
## 
##                        Df Sum of Sq     RSS    AIC
## + percent_m             1    616741 3010885 526.18
## + m_per1000f            1    250522 3377104 531.57
## + gdp                   1    180872 3446754 532.53
## <none>                              3627626 532.94
## + police_exp59          1    146167 3481459 533.00
## + prob_prison           1     92278 3535348 533.72
## + labour_participation  1     77479 3550147 533.92
## + unemploy_m24          1      2878 3624748 534.90
## + mean_education        1       767 3626859 534.93
## - police_exp60          1   3253302 6880928 561.02
## 
## Step:  AIC=526.18
## crime_rate ~ police_exp60 + percent_m
## 
##                        Df Sum of Sq     RSS    AIC
## + m_per1000f            1    261264 2749621 523.91
## + prob_prison           1    189432 2821452 525.12
## + labour_participation  1    138481 2872404 525.96
## <none>                              3010885 526.18
## + police_exp59          1     88881 2922004 526.77
## + mean_education        1     84861 2926024 526.83
## + unemploy_m24          1     31826 2979059 527.68
## + gdp                   1       771 3010113 528.17
## - percent_m             1    616741 3627626 532.94
## - police_exp60          1   3814959 6825844 562.65
## 
## Step:  AIC=523.91
## crime_rate ~ police_exp60 + percent_m + m_per1000f
## 
##                        Df Sum of Sq     RSS    AIC
## + prob_prison           1    173170 2576451 522.85
## <none>                              2749621 523.91
## + police_exp59          1     62422 2687199 524.83
## + gdp                   1     31879 2717742 525.36
## + labour_participation  1     16070 2733551 525.64
## + mean_education        1      1118 2748503 525.89
## + unemploy_m24          1        80 2749541 525.91
## - m_per1000f            1    261264 3010885 526.18
## - percent_m             1    627483 3377104 531.57
## - police_exp60          1   3768612 6518233 562.48
## 
## Step:  AIC=522.85
## crime_rate ~ police_exp60 + percent_m + m_per1000f + prob_prison
## 
##                        Df Sum of Sq     RSS    AIC
## <none>                              2576451 522.85
## + gdp                   1    101752 2474698 522.96
## + police_exp59          1     65526 2510925 523.64
## - prob_prison           1    173170 2749621 523.91
## + labour_participation  1      1385 2575066 524.83
## + mean_education        1      1334 2575116 524.83
## + unemploy_m24          1        14 2576436 524.85
## - m_per1000f            1    245002 2821452 525.12
## - percent_m             1    719903 3296354 532.43
## - police_exp60          1   2757238 5333688 555.05

Summary Modelling crime_lm2, model_backward, model_forward, both through R-squared evaluation

summary(crime_lm2)$adj.r.squared
## [1] 0.5919829
summary(model_backward)$adj.r.squared
## [1] 0.6412786
summary(model_forward)$adj.r.squared
## [1] 0.5899061
summary(model_both)$adj.r.squared
## [1] 0.5899061

What’s the insight above? we can conclude that the best model is model_backward with the highest R-squared 0.64. However, this improvement is not significant impact to our base model. So, we need to re-adjust the variable predictors to become more suitable and better R-squared result.

RE-ADJUSTED THE VARIABLE PREDICTOR

#Define_data_train3 & data_test3 using crime database
data_train3 <- crime[index, ]
data_test3 <- crime[-index, ]

#Perform LM for all variables in crime data 
set.seed(123)
crime_lm3 <- lm(crime_rate ~ ., data = data_train3)

summary(crime_lm3)
## 
## Call:
## lm(formula = crime_rate ~ ., data = data_train3)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -304.71  -91.82  -31.14  101.84  426.16 
## 
## Coefficients:
##                         Estimate  Std. Error t value Pr(>|t|)    
## (Intercept)          -10332.3968   2232.6587  -4.628  0.00024 ***
## percent_m                 9.6677      4.4748   2.160  0.04530 *  
## is_south1                 8.1011    223.3111   0.036  0.97148    
## mean_education           26.2959      7.6978   3.416  0.00329 ** 
## police_exp60             20.0908     11.8628   1.694  0.10858    
## police_exp59            -14.8569     13.5366  -1.098  0.28771    
## labour_participation     -4.6592      2.1721  -2.145  0.04669 *  
## m_per1000f                8.2657      2.8606   2.889  0.01019 *  
## state_pop                 1.3699      1.7925   0.764  0.45521    
## nonwhites_per1000         1.3334      0.9991   1.335  0.19960    
## unemploy_m24            -11.9795      5.1323  -2.334  0.03212 *  
## unemploy_m39             14.2389      9.8688   1.443  0.16724    
## gdp                       0.6436      1.2624   0.510  0.61671    
## inequality                4.2472      2.8552   1.488  0.15518    
## prob_prison             940.1481   3823.0185   0.246  0.80869    
## time_prison              14.7756      9.4304   1.567  0.13558    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 199.9 on 17 degrees of freedom
## Multiple R-squared:  0.8813, Adjusted R-squared:  0.7766 
## F-statistic: 8.414 on 15 and 17 DF,  p-value: 0.00003846

The result shows better Adjusted R-squared value: 0.7766 compare to the previous modelling. After that, as the previous method, we can deploy stepwise regression analysis (backward, forward, both) for this new model (crime_lm3)

Backward for crime_lm3 model

model_backward1 <- step(object = crime_lm3, direction = "backward")
## Start:  AIC=359.75
## crime_rate ~ percent_m + is_south + mean_education + police_exp60 + 
##     police_exp59 + labour_participation + m_per1000f + state_pop + 
##     nonwhites_per1000 + unemploy_m24 + unemploy_m39 + gdp + inequality + 
##     prob_prison + time_prison
## 
##                        Df Sum of Sq     RSS    AIC
## - is_south              1        53  679085 357.76
## - prob_prison           1      2416  681448 357.87
## - gdp                   1     10383  689416 358.25
## - state_pop             1     23329  702361 358.87
## <none>                               679033 359.75
## - police_exp59          1     48115  727148 360.01
## - nonwhites_per1000     1     71147  750179 361.04
## - unemploy_m39          1     83151  762184 361.57
## - inequality            1     88386  767419 361.79
## - time_prison           1     98056  777088 362.20
## - police_exp60          1    114569  793601 362.90
## - labour_participation  1    183780  862813 365.66
## - percent_m             1    186444  865477 365.76
## - unemploy_m24          1    217618  896650 366.93
## - m_per1000f            1    333491 1012523 370.94
## - mean_education        1    466102 1145135 375.00
## 
## Step:  AIC=357.76
## crime_rate ~ percent_m + mean_education + police_exp60 + police_exp59 + 
##     labour_participation + m_per1000f + state_pop + nonwhites_per1000 + 
##     unemploy_m24 + unemploy_m39 + gdp + inequality + prob_prison + 
##     time_prison
## 
##                        Df Sum of Sq     RSS    AIC
## - prob_prison           1      2364  681450 355.87
## - gdp                   1     11061  690146 356.29
## - state_pop             1     24951  704037 356.95
## <none>                               679085 357.76
## - police_exp59          1     48985  728070 358.05
## - unemploy_m39          1     89682  768768 359.85
## - time_prison           1    100182  779268 360.30
## - police_exp60          1    115574  794659 360.94
## - nonwhites_per1000     1    136646  815731 361.81
## - inequality            1    138689  817775 361.89
## - percent_m             1    187112  866197 363.79
## - labour_participation  1    244731  923817 365.91
## - unemploy_m24          1    245363  924448 365.93
## - m_per1000f            1    333731 1012816 368.95
## - mean_education        1    490535 1169620 373.70
## 
## Step:  AIC=355.87
## crime_rate ~ percent_m + mean_education + police_exp60 + police_exp59 + 
##     labour_participation + m_per1000f + state_pop + nonwhites_per1000 + 
##     unemploy_m24 + unemploy_m39 + gdp + inequality + time_prison
## 
##                        Df Sum of Sq     RSS    AIC
## - gdp                   1      9229  690678 354.31
## - state_pop             1     24282  705731 355.03
## <none>                               681450 355.87
## - police_exp59          1     55730  737180 356.46
## - unemploy_m39          1     96511  777961 358.24
## - police_exp60          1    125056  806506 359.43
## - inequality            1    141439  822888 360.09
## - nonwhites_per1000     1    143129  824578 360.16
## - time_prison           1    150025  831475 360.44
## - percent_m             1    184813  866263 361.79
## - labour_participation  1    242368  923817 363.91
## - unemploy_m24          1    244548  925998 363.99
## - m_per1000f            1    339823 1021273 367.22
## - mean_education        1    503886 1185335 372.14
## 
## Step:  AIC=354.31
## crime_rate ~ percent_m + mean_education + police_exp60 + police_exp59 + 
##     labour_participation + m_per1000f + state_pop + nonwhites_per1000 + 
##     unemploy_m24 + unemploy_m39 + inequality + time_prison
## 
##                        Df Sum of Sq     RSS    AIC
## - state_pop             1     28240  718918 353.64
## <none>                               690678 354.31
## - police_exp59          1     51483  742161 354.69
## - unemploy_m39          1    116128  806806 357.44
## - police_exp60          1    122245  812923 357.69
## - nonwhites_per1000     1    137395  828073 358.30
## - inequality            1    144212  834890 358.57
## - percent_m             1    177929  868607 359.88
## - time_prison           1    213364  904042 361.20
## - labour_participation  1    245231  935909 362.34
## - unemploy_m24          1    266982  957660 363.10
## - m_per1000f            1    388189 1078867 367.03
## - mean_education        1    639260 1329938 373.94
## 
## Step:  AIC=353.64
## crime_rate ~ percent_m + mean_education + police_exp60 + police_exp59 + 
##     labour_participation + m_per1000f + nonwhites_per1000 + unemploy_m24 + 
##     unemploy_m39 + inequality + time_prison
## 
##                        Df Sum of Sq     RSS    AIC
## - police_exp59          1     35183  754102 353.21
## <none>                               718918 353.64
## - unemploy_m39          1    106513  825431 356.20
## - police_exp60          1    108581  827499 356.28
## - nonwhites_per1000     1    139272  858191 357.48
## - percent_m             1    151792  870711 357.96
## - labour_participation  1    217241  936159 360.35
## - inequality            1    217382  936301 360.36
## - unemploy_m24          1    243808  962726 361.27
## - time_prison           1    248908  967826 361.45
## - m_per1000f            1    407117 1126035 366.44
## - mean_education        1    614356 1333275 372.02
## 
## Step:  AIC=353.21
## crime_rate ~ percent_m + mean_education + police_exp60 + labour_participation + 
##     m_per1000f + nonwhites_per1000 + unemploy_m24 + unemploy_m39 + 
##     inequality + time_prison
## 
##                        Df Sum of Sq     RSS    AIC
## <none>                               754102 353.21
## - nonwhites_per1000     1    112421  866523 355.80
## - unemploy_m39          1    123900  878001 356.23
## - percent_m             1    156886  910987 357.45
## - labour_participation  1    182728  936829 358.37
## - unemploy_m24          1    231604  985706 360.05
## - inequality            1    249087 1003189 360.63
## - time_prison           1    262614 1016716 361.07
## - m_per1000f            1    405612 1159714 365.42
## - mean_education        1    579494 1333596 370.03
## - police_exp60          1    765063 1519165 374.33

Forward for crime_lm3_model

model_forward1 <- step(object = crime_model_none,
                      direction = "forward",
                      scope = list(lower = crime_model_none, upper = crime_lm3))
## Start:  AIC=561.02
## crime_rate ~ 1
## 
##                        Df Sum of Sq     RSS    AIC
## + police_exp60          1   3253302 3627626 532.94
## + police_exp59          1   3058626 3822302 535.39
## + gdp                   1   1340152 5540775 552.84
## + prob_prison           1   1257075 5623853 553.54
## + state_pop             1    783660 6097267 557.34
## + mean_education        1    717146 6163781 557.85
## + m_per1000f            1    314867 6566061 560.82
## <none>                              6880928 561.02
## + labour_participation  1    245446 6635482 561.32
## + inequality            1    220530 6660397 561.49
## + unemploy_m39          1    216354 6664573 561.52
## + time_prison           1    154545 6726383 561.96
## + is_south              1     56527 6824400 562.64
## + percent_m             1     55084 6825844 562.65
## + unemploy_m24          1     17533 6863395 562.90
## + nonwhites_per1000     1      7312 6873615 562.97
## 
## Step:  AIC=532.94
## crime_rate ~ police_exp60
## 
##                        Df Sum of Sq     RSS    AIC
## + inequality            1    739819 2887807 524.22
## + percent_m             1    616741 3010885 526.18
## + m_per1000f            1    250522 3377104 531.57
## + nonwhites_per1000     1    232434 3395192 531.82
## + is_south              1    219098 3408528 532.01
## + gdp                   1    180872 3446754 532.53
## <none>                              3627626 532.94
## + police_exp59          1    146167 3481459 533.00
## + prob_prison           1     92278 3535348 533.72
## + labour_participation  1     77479 3550147 533.92
## + time_prison           1     43185 3584441 534.37
## + unemploy_m39          1     17848 3609778 534.70
## + state_pop             1      5666 3621959 534.86
## + unemploy_m24          1      2878 3624748 534.90
## + mean_education        1       767 3626859 534.93
## 
## Step:  AIC=524.22
## crime_rate ~ police_exp60 + inequality
## 
##                        Df Sum of Sq     RSS    AIC
## + mean_education        1    587050 2300757 515.53
## + m_per1000f            1    454545 2433262 518.17
## + prob_prison           1    280690 2607117 521.41
## + labour_participation  1    260571 2627236 521.77
## + gdp                   1    213937 2673871 522.60
## + percent_m             1    181236 2706571 523.17
## + state_pop             1    130377 2757430 524.04
## <none>                              2887807 524.22
## + nonwhites_per1000     1     36439 2851369 525.62
## + is_south              1     33738 2854069 525.66
## + police_exp59          1     30673 2857134 525.71
## + unemploy_m24          1      2309 2885498 526.18
## + time_prison           1       497 2887310 526.21
## + unemploy_m39          1       253 2887554 526.21
## 
## Step:  AIC=515.53
## crime_rate ~ police_exp60 + inequality + mean_education
## 
##                        Df Sum of Sq     RSS    AIC
## + percent_m             1    239405 2061353 512.37
## + prob_prison           1    234981 2065776 512.47
## + m_per1000f            1    117026 2183731 515.08
## <none>                              2300757 515.53
## + gdp                   1     79540 2221218 515.88
## + unemploy_m39          1     62112 2238646 516.25
## + time_prison           1     61770 2238987 516.26
## + police_exp59          1     42584 2258174 516.66
## + state_pop             1     39319 2261438 516.72
## + unemploy_m24          1      7365 2293392 517.38
## + labour_participation  1      7254 2293503 517.39
## + nonwhites_per1000     1      4210 2296547 517.45
## + is_south              1      4135 2296622 517.45
## 
## Step:  AIC=512.37
## crime_rate ~ police_exp60 + inequality + mean_education + percent_m
## 
##                        Df Sum of Sq     RSS    AIC
## + prob_prison           1    258063 1803290 508.08
## + unemploy_m39          1    200988 1860365 509.55
## + gdp                   1    163378 1897975 510.49
## <none>                              2061353 512.37
## + m_per1000f            1     74398 1986955 512.64
## + unemploy_m24          1     50835 2010518 513.20
## + police_exp59          1     45392 2015961 513.32
## + time_prison           1     42746 2018607 513.39
## + nonwhites_per1000     1     16488 2044865 513.99
## + state_pop             1      8101 2053251 514.19
## + is_south              1      3189 2058164 514.30
## + labour_participation  1      2988 2058365 514.30
## 
## Step:  AIC=508.08
## crime_rate ~ police_exp60 + inequality + mean_education + percent_m + 
##     prob_prison
## 
##                        Df Sum of Sq     RSS    AIC
## + unemploy_m39          1    192233 1611057 504.79
## + gdp                   1     86490 1716801 507.77
## + m_per1000f            1     84509 1718781 507.83
## <none>                              1803290 508.08
## + unemploy_m24          1     52313 1750977 508.70
## + state_pop             1     47719 1755571 508.82
## + police_exp59          1     37967 1765323 509.08
## + is_south              1     21971 1781320 509.51
## + time_prison           1     10194 1793096 509.82
## + labour_participation  1       990 1802301 510.06
## + nonwhites_per1000     1       797 1802493 510.06
## 
## Step:  AIC=504.79
## crime_rate ~ police_exp60 + inequality + mean_education + percent_m + 
##     prob_prison + unemploy_m39
## 
##                        Df Sum of Sq     RSS    AIC
## <none>                              1611057 504.79
## + gdp                   1     59910 1551147 505.00
## + unemploy_m24          1     54830 1556227 505.16
## + state_pop             1     51320 1559737 505.26
## + m_per1000f            1     30945 1580112 505.87
## + police_exp59          1     25017 1586040 506.05
## + is_south              1     17958 1593098 506.26
## + labour_participation  1     13179 1597878 506.40
## + time_prison           1      7159 1603898 506.58
## + nonwhites_per1000     1       359 1610698 506.78

Both for crime_lm3_model

model_both1 <- step(object = crime_model_none, direction = "both", scope = list(upper = crime_lm3))
## Start:  AIC=561.02
## crime_rate ~ 1
## 
##                        Df Sum of Sq     RSS    AIC
## + police_exp60          1   3253302 3627626 532.94
## + police_exp59          1   3058626 3822302 535.39
## + gdp                   1   1340152 5540775 552.84
## + prob_prison           1   1257075 5623853 553.54
## + state_pop             1    783660 6097267 557.34
## + mean_education        1    717146 6163781 557.85
## + m_per1000f            1    314867 6566061 560.82
## <none>                              6880928 561.02
## + labour_participation  1    245446 6635482 561.32
## + inequality            1    220530 6660397 561.49
## + unemploy_m39          1    216354 6664573 561.52
## + time_prison           1    154545 6726383 561.96
## + is_south              1     56527 6824400 562.64
## + percent_m             1     55084 6825844 562.65
## + unemploy_m24          1     17533 6863395 562.90
## + nonwhites_per1000     1      7312 6873615 562.97
## 
## Step:  AIC=532.94
## crime_rate ~ police_exp60
## 
##                        Df Sum of Sq     RSS    AIC
## + inequality            1    739819 2887807 524.22
## + percent_m             1    616741 3010885 526.18
## + m_per1000f            1    250522 3377104 531.57
## + nonwhites_per1000     1    232434 3395192 531.82
## + is_south              1    219098 3408528 532.01
## + gdp                   1    180872 3446754 532.53
## <none>                              3627626 532.94
## + police_exp59          1    146167 3481459 533.00
## + prob_prison           1     92278 3535348 533.72
## + labour_participation  1     77479 3550147 533.92
## + time_prison           1     43185 3584441 534.37
## + unemploy_m39          1     17848 3609778 534.70
## + state_pop             1      5666 3621959 534.86
## + unemploy_m24          1      2878 3624748 534.90
## + mean_education        1       767 3626859 534.93
## - police_exp60          1   3253302 6880928 561.02
## 
## Step:  AIC=524.22
## crime_rate ~ police_exp60 + inequality
## 
##                        Df Sum of Sq     RSS    AIC
## + mean_education        1    587050 2300757 515.53
## + m_per1000f            1    454545 2433262 518.17
## + prob_prison           1    280690 2607117 521.41
## + labour_participation  1    260571 2627236 521.77
## + gdp                   1    213937 2673871 522.60
## + percent_m             1    181236 2706571 523.17
## + state_pop             1    130377 2757430 524.04
## <none>                              2887807 524.22
## + nonwhites_per1000     1     36439 2851369 525.62
## + is_south              1     33738 2854069 525.66
## + police_exp59          1     30673 2857134 525.71
## + unemploy_m24          1      2309 2885498 526.18
## + time_prison           1       497 2887310 526.21
## + unemploy_m39          1       253 2887554 526.21
## - inequality            1    739819 3627626 532.94
## - police_exp60          1   3772590 6660397 561.49
## 
## Step:  AIC=515.53
## crime_rate ~ police_exp60 + inequality + mean_education
## 
##                        Df Sum of Sq     RSS    AIC
## + percent_m             1    239405 2061353 512.37
## + prob_prison           1    234981 2065776 512.47
## + m_per1000f            1    117026 2183731 515.08
## <none>                              2300757 515.53
## + gdp                   1     79540 2221218 515.88
## + unemploy_m39          1     62112 2238646 516.25
## + time_prison           1     61770 2238987 516.26
## + police_exp59          1     42584 2258174 516.66
## + state_pop             1     39319 2261438 516.72
## + unemploy_m24          1      7365 2293392 517.38
## + labour_participation  1      7254 2293503 517.39
## + nonwhites_per1000     1      4210 2296547 517.45
## + is_south              1      4135 2296622 517.45
## - mean_education        1    587050 2887807 524.22
## - inequality            1   1326101 3626859 534.93
## - police_exp60          1   3782666 6083423 559.23
## 
## Step:  AIC=512.37
## crime_rate ~ police_exp60 + inequality + mean_education + percent_m
## 
##                        Df Sum of Sq     RSS    AIC
## + prob_prison           1    258063 1803290 508.08
## + unemploy_m39          1    200988 1860365 509.55
## + gdp                   1    163378 1897975 510.49
## <none>                              2061353 512.37
## + m_per1000f            1     74398 1986955 512.64
## + unemploy_m24          1     50835 2010518 513.20
## + police_exp59          1     45392 2015961 513.32
## + time_prison           1     42746 2018607 513.39
## + nonwhites_per1000     1     16488 2044865 513.99
## + state_pop             1      8101 2053251 514.19
## + is_south              1      3189 2058164 514.30
## + labour_participation  1      2988 2058365 514.30
## - percent_m             1    239405 2300757 515.53
## - mean_education        1    645219 2706571 523.17
## - inequality            1    864671 2926024 526.83
## - police_exp60          1   4000849 6062202 561.07
## 
## Step:  AIC=508.08
## crime_rate ~ police_exp60 + inequality + mean_education + percent_m + 
##     prob_prison
## 
##                        Df Sum of Sq     RSS    AIC
## + unemploy_m39          1    192233 1611057 504.79
## + gdp                   1     86490 1716801 507.77
## + m_per1000f            1     84509 1718781 507.83
## <none>                              1803290 508.08
## + unemploy_m24          1     52313 1750977 508.70
## + state_pop             1     47719 1755571 508.82
## + police_exp59          1     37967 1765323 509.08
## + is_south              1     21971 1781320 509.51
## + time_prison           1     10194 1793096 509.82
## + labour_participation  1       990 1802301 510.06
## + nonwhites_per1000     1       797 1802493 510.06
## - prob_prison           1    258063 2061353 512.37
## - percent_m             1    262486 2065776 512.47
## - mean_education        1    598315 2401605 519.55
## - inequality            1    968199 2771489 526.28
## - police_exp60          1   3268577 5071868 554.69
## 
## Step:  AIC=504.79
## crime_rate ~ police_exp60 + inequality + mean_education + percent_m + 
##     prob_prison + unemploy_m39
## 
##                        Df Sum of Sq     RSS    AIC
## <none>                              1611057 504.79
## + gdp                   1     59910 1551147 505.00
## + unemploy_m24          1     54830 1556227 505.16
## + state_pop             1     51320 1559737 505.26
## + m_per1000f            1     30945 1580112 505.87
## + police_exp59          1     25017 1586040 506.05
## + is_south              1     17958 1593098 506.26
## + labour_participation  1     13179 1597878 506.40
## + time_prison           1      7159 1603898 506.58
## + nonwhites_per1000     1       359 1610698 506.78
## - unemploy_m39          1    192233 1803290 508.08
## - prob_prison           1    249308 1860365 509.55
## - percent_m             1    400611 2011667 513.22
## - mean_education        1    776207 2387264 521.27
## - inequality            1    949221 2560278 524.56
## - police_exp60          1   2817067 4428124 550.31

CONCLUSION OF NEW MODELLING

summary(crime_lm3)$adj.r.squared
## [1] 0.7765553
summary(model_backward1)$adj.r.squared
## [1] 0.8082499
summary(model_forward1)$adj.r.squared
## [1] 0.7307463
summary(model_both1)$adj.r.squared
## [1] 0.7307463

We can conclude that the best model with the highest R-squared is model_backward1: 0.81

EVALUATION Model Performance

Our model’s performance (how well it predicts the target variable) may be calculated using root mean squared error:

\[ RMSE = \sqrt{\frac{1}{n} \sum (\hat y - y)^2} \]

RMSE is superior to MAE or mean absolute error since it squares the difference between the actual and forecast values, implying that predictions with more error will be heavily penalized. Despite being more difficult to comprehend than MAE, this statistic is frequently used to compare two or more alternative models. We can utilize the caret package’s RMSE () routines. The performance of the first model (with all variables) is shown below.

crime_lm_pred <- predict(model_backward1, newdata = data_test3 %>% select(-crime_rate))

# RMSE of train dataset
RMSE(pred = model_backward1$fitted.values, obs = data_train3$crime_rate)
## [1] 151.1674
# RMSE of test dataset
RMSE(pred = crime_lm_pred, obs = data_test$crime_rate)
## [1] 376.4441

Information that can be earned by RMSE above is the error of the training dataset is lower than the test dataset, suggesting that our model may be overfit. To validate more, we can check the linearity of our model.

Check Linearity of our Model

plot(model_backward1, which = 1)

Shapiro Test Shapiro-Wilk hypothesis test:

shapiro.test(model_backward1$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  model_backward1$residuals
## W = 0.97453, p-value = 0.6143

Conclusion: P-value (0,8492) > alpha (0,05) => Error distributed normally

Check No Multicollinearity Multicollinearity is a condition that some predictors variable are detected have strong correlation to others.

vif(model_backward1)
##            percent_m       mean_education         police_exp60 
##             2.008588             4.492356             3.075057 
## labour_participation           m_per1000f    nonwhites_per1000 
##             3.797952             3.449977             2.662333 
##         unemploy_m24         unemploy_m39           inequality 
##             4.145015             4.330561             3.842859 
##          time_prison 
##             1.739850

Conclusion: From the result above, there is no multicollinearity detected from our model.

We have already learn how to build a linear regression model and what need to be concerned when building the model.