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(MASS)
##
## Attaching package: 'MASS'
##
## The following object is masked from 'package:dplyr':
##
## select
library(pastecs)
##
## Attaching package: 'pastecs'
##
## The following objects are masked from 'package:dplyr':
##
## first, last
##
## The following object is masked from 'package:tidyr':
##
## extract
library(dplyr)
library(lmtest)
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
Elections<-read_xlsx("Quantitative Methods/2024Elections.xlsx")#removes United States total row
## New names:
## • `` -> `...4`
Elections2024 <- (Elections[-1,])
Elections2024$INELIGIBLE_FELONS_TOTAL<- parse_number(Elections2024$INELIGIBLE_FELONS_TOTAL)
Elections2024$TOTAL_BALLOTS_COUNTED<- parse_number(Elections2024$TOTAL_BALLOTS_COUNTED)
Elections2024$VEP<- parse_number(Elections2024$VEP)
Elections2024$VAP<- parse_number(Elections2024$VAP)
#Elections2024$INELIGIBLE_FELONS_TOTAL <- as.numeric(Elections2024$INELIGIBLE_FELONS_TOTAL)
#Elections2024$INELIGIBLE_FELONS_TOTAL <- as.numeric(Elections2024$INELIGIBLE_FELONS_TOTAL)
#MutantElections2024<- Elections2024 %>% mutate(INELIGIBLE_FELONS_TOTAL) = as.numeric(INELIGIBLE_FELONS_TOTAL)
Elections_1<-lm(TOTAL_BALLOTS_COUNTED~VEP+INELIGIBLE_FELONS_TOTAL+VAP,data=Elections2024)
plot(Elections_1, which = 1)
This model is not linear by visual inspection
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
durbinWatsonTest(Elections_1)
## lag Autocorrelation D-W Statistic p-value
## 1 0.2353251 1.486418 0.066
## Alternative hypothesis: rho != 0
This model’s errors are not independent per the D-W Test. (P-Value > .05 and D-W statistic at around 2.0)
plot(Elections_1, which=3)
bptest(Elections_1)
##
## studentized Breusch-Pagan test
##
## data: Elections_1
## BP = 21.964, df = 3, p-value = 6.636e-05
Because the p-value of this model is not over .05 it does not meet the assumption of the BP Test, meaning the model is not homoscedastistic.
plot(Elections_1, which=2)
Model is normal by visual inspection. : )
vif(Elections_1)
## VEP INELIGIBLE_FELONS_TOTAL VAP
## 424.8958 1.7087 430.0732
For the sake of the material, I understand that VEP and VAP have a strong relationship given their linked nature, but I wanted to include at least a third variable to ensure th VIF is not autocorrelated.
Election_model_log <- lm(log(TOTAL_BALLOTS_COUNTED) ~ log(VEP) + log1p(INELIGIBLE_FELONS_TOTAL) + log(VAP),
data = Elections2024)
plot(Election_model_log,which=1)
Model was log transformed to help with linearity, it looks better
visually and like there is a trend.
Election_model_rlm<- rlm(TOTAL_BALLOTS_COUNTED~VEP+INELIGIBLE_FELONS_TOTAL+VAP,data=Elections2024)
durbinWatsonTest(Election_model_rlm)
## lag Autocorrelation D-W Statistic p-value
## 1 0.2203333 1.512883 0.058
## Alternative hypothesis: rho != 0
The RLM change has increased the P-value to meet the assumption of non-independence.(P-Value > .05 and D-W statistic at around 2.0)