Lab 6
Murders Data Set
DATA
Partition 60% Train / 40% Test
Training set number of observations: 1320
Test dataset number of observations: 877
EDA
Descriptive Statistics
vars n mean sd min max range se
arrests 1 1320 5.21 18.76 0.00 320.00 320.00 0.52
countyid 2 1320 32550.57 15488.80 1001.00 56043.00 55042.00 426.32
density 3 1320 265.34 1817.83 0.08 54058.77 54058.69 50.03
popul 4 1320 94031.70 223537.24 141.00 3126966.00 3126825.00 6152.66
perc1019 5 1320 15.11 1.88 7.60 24.54 16.94 0.05
perc2029 6 1320 12.05 3.19 5.62 34.75 29.14 0.09
percblack 7 1320 8.34 13.93 0.00 86.28 86.28 0.38
percmale 8 1320 49.33 1.63 44.83 61.64 16.82 0.04
rpcincmaint 9 1320 207.78 116.83 19.14 1281.10 1261.96 3.22
rpcpersinc 10 1320 12569.38 3031.90 5322.20 41094.22 35772.02 83.45
rpcunemins 11 1320 52.90 34.85 0.00 287.10 287.10 0.96
year 12 1320 1996.00 0.00 1996.00 1996.00 0.00 0.00
murders 13 1320 5.88 26.27 0.00 420.00 420.00 0.72
murdrate 14 1320 0.42 0.77 0.00 11.01 11.01 0.02
arrestrate 15 1320 0.48 0.94 0.00 13.99 13.99 0.03
statefips 16 1320 32.45 15.46 1.00 56.00 55.00 0.43
countyfips 17 1320 99.81 109.51 1.00 840.00 839.00 3.01
execs 18 1320 0.01 0.14 0.00 3.00 3.00 0.00
lpopul 19 1320 10.40 1.35 4.95 14.96 10.01 0.04
execrate 20 1320 0.00 0.04 0.00 1.18 1.18 0.00
statename 21 1320 NaN NA Inf -Inf -Inf NA
statecode 22 1320 NaN NA Inf -Inf -Inf NA
region 23 1320 NaN NA Inf -Inf -Inf NA
The mean number of murder arrests is 5.21.
Boxplots – All Numeric
Most of the numeric variables in the boxplot appear to have outlier points above their means. Only a couple of the variables also have points below their mean. The variable ‘murdrate’, for example, has numerous dots well above it’s mean as indicated by the box.
By region, the arrests for murder seem to be relatively even. The Northeast region has the single highest point, but no region has an overwhelming number of arrests over another. Demographically, the south has both a higher male and black population than the other regions. The income of each region, like the arrests, seem to be relatively similar. There is an outlier in each boxplot, but not a significant enough pattern to indicate one region being much better off than another.
Histograms
The histograms give a visualization of the data and the population as a whole. Most of the counties have about 15% of their population being 10-19 and another 15% being 20-29. Most counties observed also have a very low percent of black people living in the county. The percent of the population being male is just below 50%, on average. Most counties also have an arrest rate for murders of 0 (per 10,000 people).
Scatterplots (depvar ~ all x)
Look at the shape of the relationship between the dependent variable and all of the continuous potential independent variables.
For most of the numeric variables, almost all points stay near a value of 0. This makes sense because murders do not happen often and most values should be 0, excluding the case when a murder actually happens and produces a data point. AN interesting variable is the ‘percmale’ variable. As the value increases (more men are in the population), the number of murder arrests is not necessarily increasing. There are several spikes of arrests even with a low population of men. The increases in arrests happen both when there is both a low and high percentage of men in the county population.
Correlation Matrix
In the correlation matrix, there are not any variables with very high correlation. The highest observered is a positive correlation of 0.84 between the murders and arrests variable. This correlation makes perfect sense as more murders would lead to a greater number of arrests for murders. The other relatively high correlation comes from population and arrests, with a positive correlation of 0.72. This relationship also makes sense. More people in a population will lead to more murders, especially since the arrests variable is expressed as a number and not a rate. There does not appear to be any other variables with a high correlation that are negatively impacting the data with an unreasonably high correlation.
MODEL
Linear Regression Model
From the above EDA, we chose the following variables to start: density, population, percentage of population aged 10-19, percentage of population aged 20-29, percentage of population black, percentage of population male, real per capita personal income and murders per 10,000 people.
Estimate Coefficients and show coefficients table
Estimate | Standard Error | t value | Pr(>|t|) | ||
|---|---|---|---|---|---|
(Intercept) | 8.210 | 10.773 | 0.762 | 0.4462 |
|
density | 0.003 | 0.000 | 16.338 | 0.0000 | *** |
popul | 0.000 | 0.000 | 31.740 | 0.0000 | *** |
perc1019 | 0.161 | 0.184 | 0.874 | 0.3824 |
|
perc2029 | -0.078 | 0.108 | -0.725 | 0.4685 |
|
percblack | 0.091 | 0.027 | 3.428 | 0.0006 | *** |
percmale | -0.067 | 0.204 | -0.328 | 0.7433 |
|
rpcpersinc | -0.001 | 0.000 | -5.734 | 0.0000 | *** |
murdrate | 3.935 | 0.441 | 8.920 | 0.0000 | *** |
Signif. codes: 0 <= '***' < 0.001 < '**' < 0.01 < '*' < 0.05 | |||||
Residual standard error: 11.19 on 1311 degrees of freedom | |||||
Multiple R-squared: 0.6461, Adjusted R-squared: 0.644 | |||||
F-statistic: 299.2 on 1311 and 8 DF, p-value: 0.0000 | |||||
The model overall is given a multple R-squared of 0.6461. This is not a bad score, indicating that about 64% of the variability can be explained by the model. At a 0.05 level of significance, the variables density, population, percentage of population black, real per capita personal income and murder rate are signficant predictor variables.
Coefficient Magnitude Plot
The variable ‘murdrate’ has the highest coefficient in the plot with a value of 4.
Check for predictor independence
Using Variance Inflation Factors (VIF)
density popul perc1019 perc2029 percblack percmale rpcpersinc
1.266039 1.495373 1.267386 1.242670 1.439615 1.156186 1.672093
murdrate
1.227937
All values are below 10, indicating that the predictor variables are not being improperly inflated.
Residual Analysis
Residual Range
0% 25% 50% 75% 100%
-79.9700 -2.1750 0.0100 1.8225 196.2300
The quantiles at 0% and 100% have the highest deviation of the quantiles.
Residual Plots
We are looking for: - Random distribution of residuals vs fitted values - Normally distributed residuals : Normal Q-Q plot with values along line - Homoskedasticity with a Scale-Location line that is horizontal and no residual pattern - Minimal influential obs - that is, those outside the borders of Cook’s distance
For the most part, the model seems to hold up in each visualization. The residuals are not completely randomly distributed vs. the fitted values, which is a shortcoming of the model. The points in the Q-Q plot seem to stay relatively close to the line, and the line in the scale-location plot stays relatively horizontal. Most of the points also stay within the borders of Cook’s distance. Overall, the model holds up through these numerous tests.
Plot Fitted Value by Actual Value
Many of the values stay near 0, and the higher values of arrests stay relatively similar to their fitted value indicated by the y-axis.
Plot Residuals by Fitted Values
Like before, a majority of the points stay near the origin of the plot. However, this plot differs in that there is a negative linear slope in the points along the origin.
Performance Evaluation
Use Model to Score test dataset (Display First 10 values - depvar and fitted values only)
countyid | arrests | fit | lwr | upr |
|---|---|---|---|---|
1,003 | 6 | 6.0 | -15.9 | 28.0 |
1,005 | 1 | 5.4 | -16.7 | 27.4 |
1,009 | 0 | 2.3 | -19.7 | 24.3 |
1,015 | 20 | 10.8 | -11.2 | 32.8 |
1,019 | 2 | 0.8 | -21.2 | 22.7 |
1,021 | 0 | 1.3 | -20.6 | 23.3 |
1,029 | 2 | -0.7 | -22.7 | 21.3 |
1,031 | 8 | 6.6 | -15.4 | 28.6 |
1,041 | 1 | 1.7 | -20.3 | 23.7 |
1,043 | 5 | 1.8 | -20.2 | 23.7 |
n: 10 | ||||
The number of arrests and the fitted value associated with it stay close. There are a couple that differ, like counties 1,043 and 1,015. However, counties like 1,003 and 1,041 are accurately fitted by the model.
Plot Actual vs Fitted (test)
The tested values for arrests and fit show a positive and linear relationship. This is not the case for all of the points, but for most of them. This relationship shows an agreement between the actual number of arrests along with the fitted value.
Performance Metrics
Metric | Value |
|---|---|
MAE | 5.6100 |
RMSE | 38.6705 |
MAPE | Inf |
The MAE, or mean absolute error, in the model is 5.61. This means that the absolute difference between the predicted and actual number of arrests for murder differs by 5. This is not a high variation, indicating that the model does a good job of getting close to the number of the arrests for murder.
Model Fit by Region
This model compares the South and West regions. The South region does not go as high or as far as the West region in this particular model. The actual and fitted values for the West region are higher than the South, and the model does a decent job at fitting the value as indicated by the positive linear relationship. The actual values stay higher than the fitted values, but they stay relatively close.
ggplot2 explore (Intro Section)
This is just replication of the first section within Chapter 6 (as FYI)
Call:
lm(formula = arrests ~ arrestrate, data = train)
Residuals:
Min 1Q Median 3Q Max
-57.485 -2.720 -2.695 -2.409 305.944
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.6952 0.5600 4.813 0.00000166 ***
arrestrate 5.2473 0.5325 9.854 < 0.0000000000000002 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 18.11 on 1318 degrees of freedom
Multiple R-squared: 0.06862, Adjusted R-squared: 0.06791
F-statistic: 97.1 on 1 and 1318 DF, p-value: < 0.00000000000000022
In each of these plots, the line for arrests stays low, not climbing much higher than 0. However, there are points that rise much higher than the line for the arrests variable. These points do not travel far along the x axis, which is represented by arrest rate. This indicates that there at times a high number of arrests in a county, but not a high rate of arrests per 10,000 people. This result is consistent among each plot.
SUMMARY ASSESSMENT AND EVALUATION OF THE MODEL
Visualization for this model, or any model, is crucial. A numeric analysis of model performance is important to understand the overall model significance in predicting an outcome, along with understanding how much significance each predictor variable provides. However, numerical analysis is limited. Adding visuals allows for an analyst to test other variables with the model to get a more rounded understanding of the model’s performance. It also allows for additional insights to be made. For this model, I was able to better understand the relationship between arrests and region and arrests and population by demographic. This model as a whole performed pretty well. This is reinforced by the relatively close fitted values to their actual values, along with the multiple R-squared of 64%.