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(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)

data1<-(X2020Election[-1,])

Elections<-read_xlsx("Quantitative Methods/2024Elections.xlsx")
## New names:
## • `` -> `...4`
Elections2024 <- (Elections[-1,])
#removes United States total row 
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$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)

HOMEWORK

  1. Use your approved data-set
  2. Select some variables of interest and see if there is any obvious correlations using the COR command
  3. Examine the same variables visually using the PAIRS command
  4. Select two variables that seem correlated (positively or negatively) and examine them using PEARSON, SPEARMAN or KENDALL (depending on which is more appropriate)
  5. Explain your findings and justify your choice in selecting the correlation method
  6. publish to Rpubs and submit via CANVAS
ElectionCor <- Elections2024%>%select(TOTAL_BALLOTS_COUNTED,INELIGIBLE_FELONS_TOTAL,VEP) %>%arrange(TOTAL_BALLOTS_COUNTED,INELIGIBLE_FELONS_TOTAL,VEP)
cor(ElectionCor)
##                         TOTAL_BALLOTS_COUNTED INELIGIBLE_FELONS_TOTAL       VEP
## TOTAL_BALLOTS_COUNTED               1.0000000               0.5939897 0.9942173
## INELIGIBLE_FELONS_TOTAL             0.5939897               1.0000000 0.6302325
## VEP                                 0.9942173               0.6302325 1.0000000
pairs(ElectionCor)

cor.test(ElectionCor$TOTAL_BALLOTS_COUNTED,ElectionCor$VEP,method = "pearson")
## 
##  Pearson's product-moment correlation
## 
## data:  ElectionCor$TOTAL_BALLOTS_COUNTED and ElectionCor$VEP
## t = 64.808, df = 49, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.9898399 0.9967118
## sample estimates:
##       cor 
## 0.9942173
#Running again for better information on Ineligible Felons and because it is an uneven distribution I will use the Spearman test
cor.test(ElectionCor$TOTAL_BALLOTS_COUNTED,ElectionCor$INELIGIBLE_FELONS_TOTAL,method = "spearman")
## Warning in cor.test.default(ElectionCor$TOTAL_BALLOTS_COUNTED,
## ElectionCor$INELIGIBLE_FELONS_TOTAL, : cannot compute exact p-value with ties
## 
##  Spearman's rank correlation rho
## 
## data:  ElectionCor$TOTAL_BALLOTS_COUNTED and ElectionCor$INELIGIBLE_FELONS_TOTAL
## S = 7236.7, p-value = 6.471e-08
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
##       rho 
## 0.6725496
#The test shows a sample estimate rho with a .6725 results, meaning that there is a significant relationship and that there is a signficant trend. 

The choice for a pearson test on the first two variables of Total_Ballots_Counted and VEP was due to there being a visible linear relationship between the two variables. There is a strong, nearly perfect relationship shown in the test. I also chose to do another test with two different variables, Total_Ballots_Counted and Ineligible_Felons_Total. Due to the non-normal distribution of data, I chose to use the Spearman test to identify if there was a relationship. With a Rho of 0.67254, there is evidence of a strong relationship, and although not one to one or normal, there is a strong trend that exists between these two variables.