
#################################################
#                                               #    
# Supplementary file for:                       #
#                                               #
# Reconsidering the Relationship between        #
# Anti-Immigration Attitudes and Preferences    #
# for the AfD Using Implicit Attitudes Measures #
#                                               #
# by Manuel Kleinert                            #
#                                               #
# published in Politische Vierteljahresschrift  #
#                                               #
#################################################

#This file prepares the raw data of the PAIED data set, which can be 
#downloaded from https://doi.org/10.23668/psycharchives.8148 . 
#The output file "iat.Rdata" can then be used in the 
#analysis script, which is also part of the supplementary appendix.
#It also produces the numbers for the "quota chart" in the online appendix.


library(tidyverse)
library(summarytools)
library(dplyr)
library(broom)
library(ggplot2)
library(hrbrthemes)
library(viridis)
library(psych)
library(corrplot)
library(sjPlot)
library(sjmisc)
library(sjlabelled)
library(misty)
library(forestplot)

#read and clean data set####
iat <- read.csv(".../Kleinert_2022_PAIED_data_V1.0.csv", 
  header = T, sep = ";", encoding = "UTF8")

#select relevant variables
iat <- subset(iat, select = c("STARTED",
                              "IA01xD","IA02xD",
                              "SD01","SD02","SD03","SD03_10","SD04","SD05_01",
                              "MI01_01","MI01_02","MI01_03","MI02",
                              "PO01_01","PO01_02","PO01_03","PO01_04","PO01_05",
                              "PO01_06","PO01_07","QT01_01","TIME_SUM"))

#assign meaningful colnames
colnames(iat) <- c("date",
                   "iat_mig","iat_afd","age","gender","edu","edu_other","work",
                   "feedback","mig_eco","mig_cul","mig_cri","mig_com",
                   "sym_spd","sym_cdu","sym_csu","sym_gre",
                   "sym_fdp","sym_afd","sym_lin","quota","time_sum")

#dropping observations
iat <- tail(iat,-1) #remove variable labels and
iat <- tail(iat,-63) #those collected using the "old" version of the iats
iat <- iat[!(iat$quota == ""),] #and those who did not match the quotas

#descriptive analysis, collected
freq(iat$quota)
coll <- freq(iat$quota)
coll <- coll$result
coll <- head(coll,-3)

#minor adjustments

iat_chr <- subset(iat, select = c(date, edu_other, feedback, quota))
iat <- subset(iat, select = -c(date, edu_other, feedback, quota))
iat[] <- sapply(iat, as.numeric)
iat <- cbind(iat, iat_chr)
rm(iat_chr)

#remove invalid SC-IAT-cases
iat <- iat[iat$iat_mig > -7,]
iat <- iat %>% drop_na(iat_mig)

iat <- iat[iat$iat_afd > -7,]
iat <- iat %>% drop_na(iat_afd)

iat <- iat[iat$gender < 3,]

rownames(iat) <- NULL
iat[iat == -1] <- NA

#set education values for "other" entries
freq(iat$edu_other)
iat$edu[iat$edu_other == "Sekret rin"] <- 3
iat$edu[iat$edu_other == "staatl. gepr. Dolmetscherin"] <- 7
iat$edu[iat$edu == 1] <- 3
iat$edu_other <- NULL

#recode work variable to dummy
iat$work[iat$work == 2|iat$work == 3|iat$work == 4|iat$work == 5] <- 0

#create explicit and implicit anti-immigration and afd variables
iat$mig_eco <- abs(iat$mig_eco-6)
iat$mig_ex <- rowMeans(subset
                       (iat, select = c(mig_eco, mig_cul, mig_cri, mig_com)), 
                       na.rm = TRUE)
iat$mig_im <- iat$iat_mig
iat$afd_ex <- iat$sym_afd
iat$afd_im <- iat$iat_afd

#limit outliers to +2/-2
iat$mig_im[iat$mig_im < -2] <- -2
iat$mig_im[iat$mig_im > 2] <- 2
iat$afd_im[iat$afd_im < -2] <- -2
iat$afd_im[iat$afd_im > 2] <- 2

#invert implicit immigration attitudes to reflect anti-immigration attitudes
iat$mig_im <- iat$mig_im*-1

iat <- iat[complete.cases(subset(iat, select = c("age","gender","edu","work",
                                                 "mig_im","mig_ex",
                                                 "afd_im","afd_ex"))), ]

save(iat, file = "iat.Rdata")

#descriptive analyses####

# quota chart in Online Appendix
freq(iat$quota)
freq(iat$gender)
valid <- freq(iat$quota)
valid <- valid$result
valid <- head(valid,-1)

V3 <- c(34, 34, 31, 44, 34, 22, 34, 34, 31, 44, 34, 24)
planned <- as.data.frame(cbind(coll$Value,"planned",V3))

coll <- as.data.frame(cbind(coll$Value, "collected",coll$Freq))

valid <- as.data.frame(cbind(valid$Value, "valid", valid$Freq))

male_quotas <- rbind(coll[1:6,],valid[1:6,],planned[1:6,])
colnames(male_quotas) <- c("Age Group","Cases","Value")
male_quotas$`Age Group` <- c("18-29","30-39","40-49","50-59","60-69","70+")
male_quotas$Cases <- factor(male_quotas$Cases, 
                            levels = c('collected', 'planned', 'valid'))
  

female_quotas <- rbind(coll[7:12,],valid[7:12,],planned[7:12,])
colnames(female_quotas) <- c("Age Group","Cases","Value")
female_quotas$`Age Group` <- c("18-29","30-39","40-49","50-59","60-69","70+")
female_quotas$Cases <- factor(female_quotas$Cases, 
                              levels = c('collected', 'planned', 'valid'))

rm(coll, planned, valid, V3)
