Analyze only whites

anes_white <- anes_2008 %>% filter(anes_2008$V081102 ==1)

variables for scores

anes_white <- anes_2008 %>% filter(anes_2008$V081102 ==1)

variables for scores

#variable for score for whites
anes_white$V085174A_clean <- ifelse(anes_white$V085174A %in% c(-2, -8, -9), NA, anes_white$V085174A)
anes_white$V085175A_clean <- ifelse(anes_white$V085175A %in% c(-2, -8, -9), NA, anes_white$V085175A)

anes_white$white_score <- anes_white$V085174A_clean + anes_white$V085175A_clean 
anes_white$white_score <- anes_white$white_score/2

# nonwhite variables

anes_white$V085174B_clean <- ifelse(anes_white$V085174B %in% c(-2, -8, -9), NA, anes_white$V085174B)
anes_white$V085174C_clean <- ifelse(anes_white$V085174C %in% c(-2, -8, -9), NA, anes_white$V085174C)
anes_white$V085174D_clean <- ifelse(anes_white$V085174D %in% c(-2, -8, -9), NA, anes_white$V085174D)
anes_white$V085175B_clean <- ifelse(anes_white$V085175B %in% c(-2, -8, -9), NA, anes_white$V085175B)
anes_white$V085175C_clean <- ifelse(anes_white$V085175C %in% c(-2, -8, -9), NA, anes_white$V085175C)
anes_white$V085175D_clean <- ifelse(anes_white$V085175D %in% c(-2, -8, -9), NA, anes_white$V085175D)

anes_white$nonwhite_score <- anes_white$V085174B_clean+anes_white$V085174C_clean+anes_white$V085174D_clean+anes_white$V085175B_clean+anes_white$V085175C_clean+anes_white$V085175D_clean
anes_white$nonwhite_score <- anes_white$nonwhite_score/6

hist(anes_white$white_score - anes_white$nonwhite_score)

# Creating the difference variable
anes_white$diff_score <- anes_white$white_score - anes_white$nonwhite_score

# Normalizing to a scale of -1 to 1
min_diff <- min(anes_white$diff_score, na.rm = TRUE)
max_diff <- max(anes_white$diff_score, na.rm = TRUE)

#preventing division by zero
if (max_diff != min_diff) {
  anes_white$normalized_diff <- 2 * ((anes_white$diff_score - min_diff) / (max_diff - min_diff)) - 1
} else {
  anes_white$normalized_diff <- 0  # Or some other constant value since all diffs are the same
}

summary(anes_white$normalized_diff)
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max.     NA's 
## -1.00000  0.00000  0.17391  0.08975  0.17391  1.00000      181

#obama vote

# Recode 'V085044a' to create 'voteobama'
anes_white <- anes_white %>%
  mutate(
    voteobama = case_when(
      V085044A == 1 ~ 0,  # Obama
      V085044A == 3 ~ 1,  # McCain
      V085044A == -1 ~ NA,  # Abstained
      TRUE ~ NA_real_  # Other cases
    )
  )
table(anes_white$voteobama)
## 
##   0   1 
## 468 486

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

#Party ID

# Recoding V083097 to create pid308
anes_white <- anes_white %>%
  mutate(
    pid308 = case_when(
      V083097 == 1 ~ 1,
      V083097 == 2 ~ 0,
      V083097 %in% c(3, 4, 5) ~ 0.5,
      TRUE ~ NA_real_
    )
  )

# Generating pid708 with conditional replacements
anes_white <- anes_white %>%
  mutate(
    pid708 = NA_real_,  # Initialize with NA
    pid708 = ifelse(pid308 == 0 & V083098A == 1, 0, pid708),
    pid708 = ifelse(pid308 == 0 & V083098A == 5, 0.17, pid708),
    pid708 = ifelse(V083098B == 1, 0.33, pid708),
    pid708 = ifelse(V083098B == 3, 0.5, pid708),
    pid708 = ifelse(V083098B == 5, 0.67, pid708),
    pid708 = ifelse(pid308 == 1 & V083098A == 5, 0.83, pid708),
    pid708 = ifelse(pid308 == 1 & V083098A == 1, 1, pid708)
  )

# Creating pid_new based on the given formula: pid708 * 2 - 1
anes_white <- anes_white %>%
  mutate(pid_new = pid708 * 2 - 1)

table(anes_white$pid_new)
## 
##    -1 -0.66 -0.34     0  0.34  0.66     1 
##   208   182   182   172   246   211   223

#issues

# Recoding Spending and Services
anes_white <- anes_white %>%
  mutate(
    spdsvc7a = case_when(
      V083105 == 1 ~ 0,
      V083105 == 2 ~ 0.17,
      V083105 == 3 ~ 0.33,
      V083105 %in% c(4, -7, -8) ~ 0.5,
      V083105 == 5 ~ 0.67,
      V083105 == 6 ~ 0.83,
      V083105 == 7 ~ 1,
      TRUE ~ NA_real_  # For other cases
    ),
    spdsvc7b = case_when(
      V083108X == 7 ~ 0,
      V083108X == 6 ~ 0.17,
      V083108X == 5 ~ 0.33,
      V083108X %in% c(4, -7, -8) ~ 0.5,
      V083108X == 3 ~ 0.67,
      V083108X == 2 ~ 0.83,
      V083108X == 1 ~ 1,
      TRUE ~ NA_real_
    ),
    spdsvcnew = case_when(
      spdsvc7a >= 0 & spdsvc7a <= 2 ~ 0,
      spdsvc7b >= 0 & spdsvc7b <= 2 ~ 1,
      TRUE ~ NA_real_
    ),
    spdsvc708 = ifelse(spdsvcnew == 1, spdsvc7b, spdsvc7a)
  )

# Recode Defense Spending
anes_white <- anes_white %>%
  mutate(
    spddef7a = case_when(
      V083112 == 1 ~ 1,
      V083112 == 2 ~ 0.83,
      V083112 == 3 ~ 0.67,
      V083112 %in% c(4, -7, -8) ~ 0.5,
      V083112 == 5 ~ 0.33,
      V083112 == 6 ~ 0.17,
      V083112 == 7 ~ 0,
      TRUE ~ NA_real_
    ),
    spddef7b = case_when(
      V083115X == 7 ~ 1,
      V083115X == 6 ~ 0.83,
      V083115X == 5 ~ 0.67,
      V083115X %in% c(4, -7, -8) ~ 0.5,
      V083115X == 3 ~ 0.33,
      V083115X == 2 ~ 0.17,
      V083115X == 1 ~ 0,
      TRUE ~ NA_real_
    ),
    spddefnew = case_when(
      spddef7a >= 0 & spddef7a <= 2 ~ 0,
      spddef7b >= 0 & spddef7b <= 2 ~ 1,
      TRUE ~ NA_real_
    ),
    spddef708 = ifelse(spddefnew == 1, spddef7b, spddef7a)
  )

# Guaranteed Jobs
anes_white <- anes_white %>%
  mutate(
    jobsol708 = case_when(
      V083128 == 1 ~ 1,
      V083128 == 2 ~ 0.83,
      V083128 == 3 ~ 0.67,
      V083128 %in% c(4, -7, -8) ~ 0.5,
      V083128 == 5 ~ 0.33,
      V083128 == 6 ~ 0.17,
      V083128 == 7 ~ 0,
      TRUE ~ NA_real_
    )
  )

# Health Insurance
anes_white <- anes_white %>%
  mutate(
    hlthins08 = case_when(
      V083119 == 1 ~ 1,
      V083119 == 2 ~ 0.83,
      V083119 == 3 ~ 0.67,
      V083119 %in% c(4, -7, -8) ~ 0.5,
      V083119 == 5 ~ 0.33,
      V083119 == 6 ~ 0.17,
      V083119 == 7 ~ 0,
      TRUE ~ NA_real_
    )
  )

# Calculating missing issue scale
anes_white <- anes_white %>%
  mutate(
    issuemis = rowSums(is.na(cbind(spdsvc708, spddef708, jobsol708, hlthins08))),
    spdsvc708a = ifelse(is.na(spdsvc708), 0, spdsvc708),
    spddef708a = ifelse(is.na(spddef708), 0, spddef708),
    jobsol708a = ifelse(is.na(jobsol708), 0, jobsol708),
    hlthins08a = ifelse(is.na(hlthins08), 0, hlthins08),
    issuescale = (spdsvc708a + spddef708a + jobsol708a + hlthins08a) / (4 - issuemis),
    issuescale = ifelse(issuemis < 3, issuescale, NA),
    issue_new = issuescale * 2 - 1
  )

# Summarize the issue scale
hist(anes_white$issuescale)

#economic assessement

# Recoding variables 
anes_white <- anes_white %>%
  mutate(
    ecprog08 = case_when(
      V083083X == 1 ~ 1,
      V083083X == 2 ~ 0.75,
      V083083X == 3 ~ 0.5,
      V083083X == 4 ~ 0.25,
      V083083X == 5 ~ 0,
      TRUE ~ NA_real_  # For other cases
    ),
    ueprog08 = case_when(
      V083087X == 1 ~ 1,
      V083087X == 2 ~ 0.75,
      V083087X == 3 ~ 0.5,
      V083087X == 4 ~ 0.25,
      V083087X == 5 ~ 0,
      TRUE ~ NA_real_
    ),
    inprog08 = case_when(
      V083089X == 1 ~ 1,
      V083089X == 2 ~ 0.75,
      V083089X == 3 ~ 0.5,
      V083089X == 4 ~ 0.25,
      V083089X == 5 ~ 0,
      TRUE ~ NA_real_
    )
  )

# Creating a data frame with only the required variables
alpha_data <- anes_white %>%
  select(ecprog08, ueprog08, inprog08)

# Computing alpha reliability
alpha_result <- alpha(alpha_data, na.rm = TRUE)

# Outputing the alpha result
print(alpha_result)
## 
## Reliability analysis   
## Call: alpha(x = alpha_data, na.rm = TRUE)
## 
##   raw_alpha std.alpha G6(smc) average_r S/N  ase mean   sd median_r
##       0.56      0.56    0.46       0.3 1.3 0.02 0.16 0.15     0.28
## 
##     95% confidence boundaries 
##          lower alpha upper
## Feldt     0.52  0.56   0.6
## Duhachek  0.52  0.56   0.6
## 
##  Reliability if an item is dropped:
##          raw_alpha std.alpha G6(smc) average_r  S/N alpha se var.r med.r
## ecprog08      0.42      0.42    0.26      0.26 0.72    0.031    NA  0.26
## ueprog08      0.44      0.44    0.28      0.28 0.80    0.029    NA  0.28
## inprog08      0.52      0.52    0.35      0.35 1.09    0.025    NA  0.35
## 
##  Item statistics 
##             n raw.r std.r r.cor r.drop mean   sd
## ecprog08 1436  0.73  0.75  0.54   0.40 0.11 0.18
## ueprog08 1411  0.76  0.74  0.52   0.38 0.19 0.21
## inprog08 1411  0.71  0.71  0.44   0.33 0.18 0.20
## 
## Non missing response frequency for each item
##             0 0.25  0.5 0.75 1 miss
## ecprog08 0.66 0.24 0.08 0.01 0 0.00
## ueprog08 0.47 0.34 0.16 0.03 0 0.02
## inprog08 0.48 0.34 0.17 0.01 0 0.02
# Creating ecprog08scale and ecprog_new
anes_white <- anes_white %>%
  mutate(
    ecprog08scale = (ecprog08 + ueprog08 + inprog08) / 3,  # Calculate the average
    ecprog_new = ecprog08scale * 2 - 1  # Transform to the desired scale
  )

hist(anes_white$ecprog08scale)

#political information

# Initializing and replacing 'ptycon' based on conditions
anes_white <- anes_white %>%
  mutate(
    ptycon = NA_real_,  # Initialize with NA
    ptycon = ifelse(V085119A == 5, 1, ptycon),  # If V085119a is 5, set to 1
    ptycon = ifelse(V085119A %in% c(-8, -9, 1), 0, ptycon),
    ptycon = ifelse(V085119 %in% c(5, -8, 9), 0, ptycon)  # Ensure correct variable names
  )

#  Recoding V085066 into 'hormaj' and V085067 into 'senmaj'
anes_white <- anes_white %>%
  mutate(
    hormaj = case_when(
      V085066 == 1 ~ 1,
      V085066 == 5 ~ 0,
      V085066 %in% c(-8, -9) ~ 0,
      TRUE ~ NA_real_  # For other cases
    ),
    senmaj = case_when(
      V085067 == 1 ~ 1,
      V085067 == 5 ~ 0,
      V085067 %in% c(-8, -9) ~ 0,
      TRUE ~ NA_real_
    )
  )

# Calculating 'infopty08' as the average of 'ptycon', 'hormaj', and 'senmaj'
anes_white <- anes_white %>%
  mutate(
    infopty08 = (ptycon + hormaj + senmaj) / 3
  )

hist(anes_white$infopty08)

#education

anes_white <- anes_white %>%
  mutate(
    educ08 = ifelse(V083217 %in% c(-9, -8), 0, V083217)  # Replace -9 and -8 with 0
  )

# Tabulate 'educ08'
table(anes_white$educ08)
## 
##   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17 
##   4   2   1   4   2   4  14   6  28  31  38  65 428 143 209  72 216 175
#Divide 'educ08' by 17
anes_white <- anes_white %>%
  mutate(
    educ08 = educ08 / 17  # Normalize or scale 'educ08'
  )

hist(anes_white$educ08)

#gender

anes_white <- anes_white %>%
  mutate(female08 = V081101 - 1)

table(anes_white$female08)
## 
##   0   1 
## 627 815

#household ecnomic conditions

anes_white <- anes_white %>%
  mutate(
    hhecon08 = case_when(
      V083057X == 1 ~ 1,  # Recode 1 to 1
      V083057X == 2 ~ 0.75,  # Recode 2 to 0.75
      V083057X == 3 ~ 0.5,  # Recode 3 to 0.5
      V083057X == 4 ~ 0.25,  # Recode 4 to 0.25
      V083057X == 5 ~ 0,  # Recode 5 to 0
      TRUE ~ NA_real_  # For other cases, set to NA
    ),
    
    # Creating 'hhecon_new' by transforming 'hhecon08'
    hhecon_new = hhecon08 * 2 - 1  # Transform to a scale of -1 to 1
  )

hist(anes_white$hhecon_new)

#MAKE TABLE

library(nnet)  # For multinomial logistic regression
library(broom)  # For tidy model results

mnl_model <- multinom(
  voteobama ~ normalized_diff + hhecon_new + educ08 + infopty08 + ecprog08scale + issuescale + pid_new,
  data = anes_white
)
## # weights:  9 (8 variable)
## initial  value 611.355813 
## iter  10 value 317.554146
## iter  20 value 307.195637
## final  value 307.194662 
## converged
# Display a summary of the model
summary(mnl_model)
## Call:
## multinom(formula = voteobama ~ normalized_diff + hhecon_new + 
##     educ08 + infopty08 + ecprog08scale + issuescale + pid_new, 
##     data = anes_white)
## 
## Coefficients:
##                      Values Std. Err.
## (Intercept)      0.67607185 0.7169357
## normalized_diff -1.40178634 0.4889157
## hhecon_new      -0.04128192 0.1769155
## educ08           0.86171925 0.8102326
## infopty08        0.54429186 0.3030066
## ecprog08scale    0.36654037 0.8111269
## issuescale      -3.02754187 0.5957933
## pid_new         -2.74300780 0.1917186
## 
## Residual Deviance: 614.3893 
## AIC: 630.3893