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(MASS)
##
## Attaching package: 'MASS'
##
## The following object is masked from 'package:dplyr':
##
## select
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
CountyClean<-read_excel("CountyClean.xlsx")
## New names:
## • `National Z-Score` -> `National Z-Score...11`
## • `National Z-Score` -> `National Z-Score...24`
Dependent Variable: # Drug Overdose Deaths (DOD) Independent Variables: Primary Care Physician Rate (PCP), Mental Health Provider Rate (MHP), Unemployment Rate (UNEM), # of Uninsured Adults (UNIN), and Severe Housing Cost Burden Rate (HSHCB)
county_model1<-lm(DOD~UNIN+HSHCB+PCP+MHP+UNEM,data=CountyClean)
plot(county_model1,which=1)
This model is not linear based on visual inspection.
durbinWatsonTest(county_model1)
## lag Autocorrelation D-W Statistic p-value
## 1 -0.03458428 2.069099 0.73
## Alternative hypothesis: rho != 0
The model’s errors are independent per Durbinwatson test as the p-value is 0.706 and over 0.05.
plot(county_model1,which=3)
bptest(county_model1)
##
## studentized Breusch-Pagan test
##
## data: county_model1
## BP = 2.6562, df = 5, p-value = 0.7528
Model is homoskedastic per bp-test (p value > 0.05)
plot(county_model1,which=2)
Model is mostly abnormal by visual inspection as the data points stray from the line.
vif(county_model1)
## UNIN HSHCB PCP MHP UNEM
## 1299.4470 3177.9897 917.2266 632.7910 2038.8465
This model has multicolinearity.
The model meets the assumptions of the Independence of Errors and Homoskedasticity
My model violates the assumptions of linearity, Normality of Residuals and multicoliniarity
LINEAR TRANSFORMATION:
county_model_log1<-lm(DOD~UNIN+HSHCB+PCP+MHP+UNEM,data=CountyClean)
plot(county_model_log1,which=1)
Model was log transformed to help with linearity but the final result
was still nonlinear
NORMALITY OF RESIDUALS:
shapiro.test(county_model1$residuals)
##
## Shapiro-Wilk normality test
##
## data: county_model1$residuals
## W = 0.77955, p-value = 1.705e-11
county_model2<-lm(log(DOD)~UNIN+HSHCB+PCP+MHP+UNEM,data=CountyClean)
shapiro.test(county_model2$residuals)
##
## Shapiro-Wilk normality test
##
## data: county_model2$residuals
## W = 0.92039, p-value = 6.578e-06
plot(county_model2,which=2)
This somewhat fixed the violation but the model is still somewhat abnormal.
MULTICOLINEARITY:
kitchen_sink<-lm(DOD~UNIN+HSHCB+PCP+MHP+UNEM,data=CountyClean)
vif(kitchen_sink)
## UNIN HSHCB PCP MHP UNEM
## 1299.4470 3177.9897 917.2266 632.7910 2038.8465
Model was transformed to help with Multicolinearity but the variables still violate no multivolinearity