Chi-Square in R on by State Politics (Red/Blue) and Income (Higher/Lower)

This is a significant result, but instead of a logistic regression looking at the income average per state and the likelihood of being a Democratic state, it uses Chi-Square. Interpreting this is pretty straightforward, in that liberal states typically have cities and people that earn more money. When using adjusted incomes, by cost of living, this difference disappears.

Example Code

 # R - Chi Square  
 rm(list = ls())  
 stateData <- read.table("CostByStateAndSalary.csv", header = TRUE, sep = ",")  
   
 # Create vectors   
 affluence.median <- median(stateData$Y2014, na.rm = TRUE)  
 affluence.v <- ifelse(stateData$Y2014 > affluence.median, 1, 0)  
 liberal.v <- stateData$Liberal  
   
 # Solve  
 pol.Data = table(liberal.v, affluence.v)  
 result <- chisq.test(pol.Data)  
 print(result)  
 print(pol.Data)  

Example Results

 Pearson's Chi-squared test with Yates' continuity correction  
   
 data: pol.Data  
 X-squared = 12.672, df = 1, p-value = 0.0003711  
   
 > print(pol.Data)  
 +   
            affluence.v  
 liberal.v     0  1  
            0  22 7  
            1  4 16  

Sample Data

Popular posts from this blog

Charting Correlation Matrices in R

Neural Networks (Part 4 of 4) - R Packages and Resources

Performance Improvements in R: Vectorization & Memoisation