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("FinalData.xlsx")
FinalData1<-FinalData |> drop_na(`PCP`) %>% filter(`PCP`>0)
FinalData2<-FinalData1 |> drop_na('MHP') %>% filter(`MHP`>0)
FinalData3<-FinalData2 |> drop_na('DEN') %>% filter(`DEN`>0)
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`) %>% arrange(-`DOD`, `SHCB`, `MHP`,`UNIN`)
#Correlation: Model H
cor(CountyFinal)
## DOD SHCB MHP UNIN
## DOD 1.0000000 0.3157972 0.9545255 0.9767553
## SHCB 0.3157972 1.0000000 0.3504348 0.3390718
## MHP 0.9545255 0.3504348 1.0000000 0.9224518
## UNIN 0.9767553 0.3390718 0.9224518 1.0000000
County_model <-lm(DOD~SHCB+MHP+UNIN,data=CountyFinal)
#Summary of Data: Model G
summary(County_model)
##
## Call:
## lm(formula = DOD ~ SHCB + MHP + UNIN, data = CountyFinal)
##
## Residuals:
## Min 1Q Median 3Q Max
## -426.32 -5.14 4.21 16.72 223.51
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 44.1453207 26.2084072 1.684 0.0951 .
## SHCB -4.6372428 2.3631425 -1.962 0.0524 .
## MHP 0.1049338 0.0117132 8.959 1.42e-14 ***
## UNIN 0.0025055 0.0001576 15.903 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 59.54 on 104 degrees of freedom
## Multiple R-squared: 0.9742, Adjusted R-squared: 0.9735
## F-statistic: 1310 on 3 and 104 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,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.08905871 2.040188 0.896
## Alternative hypothesis: rho != 0
#The models errors are independent as the p-value is .966 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,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 = 32.472, df = 3, p-value = 4.163e-07
#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 = 13.009, df = 3, p-value = 0.004618
#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,data=CountyFinal)
shapiro.test(County_model$residuals)
##
## Shapiro-Wilk normality test
##
## data: County_model$residuals
## W = 0.61897, p-value = 2.515e-15
County_model_log2<-lm(log(DOD)~MHP,SHCB,UNIN,data=CountyFinal)
shapiro.test(County_model_log2$residuals)
##
## Shapiro-Wilk normality test
##
## data: County_model_log2$residuals
## W = 0.89023, p-value = 2.165e-07
plot(County_model_log2,which=2)
#Model is still abnormal by visual inspection.
#Multicolinearity: Model F
vif(County_model)
## SHCB MHP UNIN
## 1.142181 6.780560 6.720534
#Model does not have severe multivolinearity as results are less than the threshhold of 10.
#Multicolinearity: Model F-1
kitchen_sink<-lm(DOD~UNIN+SHCB+MHP,data=CountyFinal)
vif(kitchen_sink)
## UNIN SHCB MHP
## 6.720534 1.142181 6.780560