library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.3 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readxl)
library(ggplot2)
library(lmtest)
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
##
## The following object is masked from 'package:dplyr':
##
## recode
##
## The following object is masked from 'package:purrr':
##
## some
FinalData<-read_excel("Final County Data.xlsx")
FinalData1<-FinalData |> drop_na(`PCP`) %>% filter(`PCP`>0)
FinalData2<-FinalData1 |> drop_na('MHP') %>% filter(`MHP`>0)
FinalData3<-FinalData2 |> drop_na('POP') %>% filter(`POP`>1000)
FinalData4<-FinalData3 |> drop_na('PHR') %>% filter(`PHR`>0)
FinalData5<-FinalData4 |> drop_na('UIN') %>% filter(`UIN`>0)
FinalData6<-FinalData5 |> drop_na('SHCB') %>% filter(`SHCB`>0)
FinalData7<-FinalData6 |> drop_na('UNEM') %>% filter(`UNEM`>0)
FinalData8<-FinalData7 |> drop_na('DOD') %>% filter(`DOD`>0)
FinalData9<-FinalData8 |> drop_na('UNIN') %>% filter(`UNIN`>0)
CountyFinal <- FinalData9 %>% dplyr::select(`DOD`, `SHCB`, `MHP`,`UNIN`,`POP`) %>% arrange(-`DOD`, `SHCB`, `MHP`,`UNIN`,`POP`)
#Correlation: Model H
cor(CountyFinal)
## DOD SHCB MHP UNIN POP
## DOD 1.00000000 0.3234176 0.95462714 0.97672461 -0.06718933
## SHCB 0.32341760 1.0000000 0.35545040 0.34709821 -0.16380000
## MHP 0.95462714 0.3554504 1.00000000 0.92244399 -0.07487886
## UNIN 0.97672461 0.3470982 0.92244399 1.00000000 -0.06151127
## POP -0.06718933 -0.1638000 -0.07487886 -0.06151127 1.00000000
County_model <-lm(DOD~SHCB+MHP+UNIN+POP,data=CountyFinal)
#Summary of Data: Model G
summary(County_model)
##
## Call:
## lm(formula = DOD ~ SHCB + MHP + UNIN + POP, data = CountyFinal)
##
## Residuals:
## Min 1Q Median 3Q Max
## -425.67 -5.72 4.10 16.19 222.99
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.604e+01 2.786e+01 1.653 0.1015
## SHCB -4.771e+00 2.490e+00 -1.916 0.0583 .
## MHP 1.049e-01 1.194e-02 8.786 4.51e-14 ***
## UNIN 2.507e-03 1.607e-04 15.595 < 2e-16 ***
## POP -6.111e-06 1.907e-05 -0.320 0.7493
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 60.64 on 100 degrees of freedom
## Multiple R-squared: 0.9742, Adjusted R-squared: 0.9732
## F-statistic: 945.6 on 4 and 100 DF, p-value: < 2.2e-16
#Linearity: Model A
plot(County_model,which=1)
#The model is not linear based on visual inspection.
#Linear Transformation: Model A-1
County_model_log3<-lm(DOD~UNIN+SHCB+MHP+POP,data=CountyFinal)
plot(County_model_log3,which=1)
#Model was transformed but final result is still nonlinear per visual inspection.
#Independence of Errors: Model B
durbinWatsonTest(County_model)
## lag Autocorrelation D-W Statistic p-value
## 1 -0.08755978 2.037394 0.936
## Alternative hypothesis: rho != 0
#The models errors are independent as the p-value is .948 which is greater than the standard 0.05.
#Homoscedasticity: Model C
plot(County_model,which=3)
#This model is not homoscedastic per visual inspection.
#Homoscedascity Transformation: Model C-1
County_model_log<-lm(log(DOD)~MHP+SHCB+UNIN+POP,data=CountyFinal)
plot(County_model_log,which=3)
#Model is mildly improved for homoscedasticity per visual inspection.
#Homoscedasticity: Model D
bptest(County_model)
##
## studentized Breusch-Pagan test
##
## data: County_model
## BP = 31.557, df = 4, p-value = 2.356e-06
#Model is not homoscedastic per visual inspection
#Homoscedasticity Transformation: Model D-1
bptest(County_model_log)
##
## studentized Breusch-Pagan test
##
## data: County_model_log
## BP = 12.955, df = 4, p-value = 0.0115
#Model is not homoscedastic per visual inspection
#Normality of Residuals: Model E
plot(County_model,which=2)
#Model is somewhat abnormal by visual inspection as the data points stray from the line.
#Normality of Residuals Transformation: Model E-1
County_model <-lm(DOD~SHCB+MHP+UNIN+POP,data=CountyFinal)
shapiro.test(County_model$residuals)
##
## Shapiro-Wilk normality test
##
## data: County_model$residuals
## W = 0.62318, p-value = 5.092e-15
County_model_log2<-lm(log(DOD)~MHP+SHCB+UNIN+POP,data=CountyFinal)
shapiro.test(County_model_log2$residuals)
##
## Shapiro-Wilk normality test
##
## data: County_model_log2$residuals
## W = 0.97253, p-value = 0.02794
plot(County_model_log2,which=2)
#Model is still abnormal by visual inspection.
#Multicolinearity: Model F
vif(County_model)
## SHCB MHP UNIN POP
## 1.173742 6.778562 6.731312 1.028701
#Model does not have severe multicolinearity as results are less than the threshold of 10.
#Multicolinearity: Model F-1
kitchen_sink<-lm(DOD~UNIN+SHCB+MHP+POP,data=CountyFinal)
vif(kitchen_sink)
## UNIN SHCB MHP POP
## 6.731312 1.173742 6.778562 1.028701