Script – UACES Conference Paper

#Note: set.seed() function was not used when the paper was being drafted.
# Imputations may generate different datasets in each iteration.
# Obtained values may be slightly different from what is presented in the paper.

library(Zelig)
library(Amelia)
source("customfunctions.R")

data <- read.csv("complete2.csv")
attach(data)

## Generation and transformation of variables ##

data$MLP.fin <- MLP7^3/25
data$EI.fin <- EuInteg^2/5
data$SNA.fin <- devolution5
data$TNOuse.fin <- UsefulIntOrg^2/5

data$country <- data$country2
data$country2 <- NULL
data$MLP7 <- NULL
data$EuInteg <- NULL
data$UsefulIntOrg <- NULL
data$devolution5 <- NULL
data$TNI.sum <- NULL

data$surveyID <- factor(surveyID)
data$fundsRec <- factor(fundsRec)
data$effective.intorg <- factor(effective.intorg)
data$effective.directrel <- factor(effective.directrel)
data$effective.natdel <- factor(effective.natdel)
data$fundsImp <- factor(fundsImp)
data$education <- ordered(education)
data$LeftRight <- ordered(LeftRight)
data$PolId.local <- factor(PolId.local)
data$PolId.national <- factor(PolId.national)
data$PolId.european <- factor(PolId.european)
data$PolId.cultural <- factor(PolId.cultural)
data$PolId.ideology <- factor(PolId.ideology)
data$represent01 <- factor(represent01)
data$office01 <- factor(office01)

## Multiple imputation for missing data ##

#Matrix to define the limits of imputed values:
colnum <- 1:31
mins <- c(NA, NA, NA, NA, 
 NA, NA, NA, NA, 
 NA, 20, NA, 1, 
 1, NA, NA, NA, 
 NA, NA, NA, NA, 
 NA, NA, 2, 16, 
 2, 16, NA, 2, 
 2, 2, 2)
maxs <- c(NA, NA, NA, NA, 
 NA, NA, NA, NA, 
 NA, 80, NA, 3, 
 9, NA, NA, NA, 
 NA, NA, NA, NA, 
 NA, NA, 15, 31, 
 15, 29, NA, 5, 
 5, 5, 5)
minmax <- cbind(colnum, mins, maxs)

#Imputation:
a.out <- amelia(data, 
 idvars = c("surveyID", "EU.mem", "domInst", "represent", "weight"), 
 cs = "country", 
 noms = c("fundsRec", "effective.intorg", "effective.directrel", 
 "effective.natdel", "fundsImp", "gender", "PolId.local", 
 "PolId.national", "PolId.european", "PolId.cultural", 
 "PolId.ideology", "represent01", "office01", 
 "repORoff"), 
 ords = c("education", "LeftRight"), 
 bounds = minmax)

## Further variable generation and transformation ##

for (i in 1:5) {
 a.out$imputations[[i]]$TNI.fin <- log(
 2*(a.out$imputations[[i]]$TNI.nonMem + a.out$imputations[[i]]$TNI.mem) + 
 a.out$imputations[[i]]$TNI.nonOff + a.out$imputations[[i]]$TNI.off)
}

for (i in 1:5) {
 a.out$imputations[[i]]$LRC <-seq(1:802)
 a.out$imputations[[i]][a.out$imputations[[i]]$LeftRight<4, ]$LRC <- "Left"
 a.out$imputations[[i]][a.out$imputations[[i]]$LeftRight>3 & 
 a.out$imputations[[i]]$LeftRight<7, ]$LRC <- "Centre"
 a.out$imputations[[i]][a.out$imputations[[i]]$LeftRight>6, ]$LRC <- "Right"
 a.out$imputations[[i]]$LRC <- factor(a.out$imputations[[i]]$LRC)
}

for (i in 1:5) {
 a.out$imputations[[i]]$education01 <- ifelse(a.out$imputations[[i]]$education==1, 0, 1)
}

for (i in 1:5) {
 a.out$imputations[[i]]$represent <- factor(a.out$imputations[[i]]$represent, 
 levels=c("NONE", "CLRAE", "COR", "OTHER"))
}

## The main model ##

z.out1 <- zelig(MLP.fin ~ represent01 + EI.fin + SNA.fin + 
 fundsImp + effective.intorg + effective.directrel + 
 PolId.european + PolId.cultural + PolId.ideology + 
 LRC + EU.mem + age + gender + education01 + domInst, 
 data=a.out$imputations, model="ls", weights=weight)

results <- summary(z.out1)
print(summary(z.out1), subset = 1:5)
write.csv(results[[4]], "zelig-out1.csv")
write.csv(z.modelstats(results), "mainmodel-stats.csv")

# Re-run with categorical variable of membership ##

z.out2 <- zelig(MLP.fin ~ represent + EI.fin + SNA.fin + 
 fundsImp + effective.intorg + effective.directrel + 
 PolId.european + PolId.cultural + PolId.ideology + 
 LRC + EU.mem + age + gender + education01 + domInst, 
 data=a.out$imputations, model="ls", weights=weight)

results2 <- summary(z.out2)
print(summary(z.out2), subset = 1:5)
write.csv(results2[[4]], "zelig-out2.csv")
write.csv(z.modelstats(results2), "rerunmodel-stats.csv")

## Subset of data: members only ##

a.out2 <- a.out

for (i in 1:5) {
 a.out2$imputations[[i]] <- a.out$imputations[[i]][represent01==1, ]
}

for (i in 1:5) {
 a.out2$imputations[[i]]$represent <- relevel(a.out2$imputations[[i]]$represent, ref="OTHER")
}

#Comparing means across categories of membership:
z.out3 <- zelig(MLP.fin ~ represent, 
 data=a.out2$imputations, model="ls", 
 weights=data=a.out2$imputations[[3]]$weight)
summary(z.out3)

#Comparing unweighted means:
z.out4 <- zelig(MLP.fin ~ represent, 
 data=a.out2$imputations, model="ls")
summary(z.out4)