load("C:\\Users\\Kevin\\Documents\\R\\immigration\\gap_full.RData")objects()dim(gap)names(gap)summary(gap)gap2 <- read.delim(file="gap2.txt")newgap <- gap[ , c("country", "q14h", "q14b", "q23a", "q23f", "q24e", "q24f", "q107","q118arg","q118ban","q118bol","q118bra","q118bul","q118can","q118chil","q118chin","q118cze","q118egy","q118eth","q118fra","q118gbr","q118ger","q118gha","q118inda","q118indo","q118isr","q118ita","q118ivo","q118jap","q118jor","q118ken","q118kuw","q118leb","q118mala","q118mali","q118mex","q118mor","q118nig","q118pak","q118pal","q118per","q118pol","q118rus","q118safr","q118sen","q118skor","q118slo","q118spa","q118swe","q118tan","q118tur","q118uga","q118ukr","q118us","q118ven","q120","q120spa","q120us","q120inda","q134usr","q134ur","q134bul","q134inda","q134jap","q134ken","q134mex","q134skor","q134safr", "q20")]newgap$country <- factor(newgap$country, levels=47:1, labels=names(attributes(newgap$country)$value.labels))table(newgap$country)summary(newgap)#####################################Attitudes towards immigrants#####################################table(newgap$country, newgap$q23f)migr.x <- numeric()migr.x[newgap$q23f==1] <- 0.25migr.x[newgap$q23f==2] <- 0.50migr.x[newgap$q23f==3] <- 0.75migr.x[newgap$q23f==4] <- 1migr.x[newgap$q23f==8] <- 0migr.x[newgap$q23f==9] <- NAtable(newgap$country, migr.x)migr1 <- tapply(migr.x, newgap$country, mean,na.rm=TRUE)dotchart(migr1[order(migr1)], color=c("red","blue","darkgreen"),  xlab = "Proportions Agreeing",cex=0.8)plot(density(migr1, na.rm=TRUE))######################################Attitudes towards immigration######################################table(newgap$country, newgap$q14h)immi.x <- numeric()immi.x[newgap$q14h==1] <- 1immi.x[newgap$q14h==2] <- 0.75immi.x[newgap$q14h==3] <- 0.50immi.x[newgap$q14h==4] <- 0.25immi.x[newgap$q14h==8] <- 0immi.x[newgap$q14h==9] <- NAtable(newgap$country, immi.x)immi1 <- tapply(immi.x, newgap$country, mean,na.rm=TRUE)dotchart(immi1[order(immi1)], color=c("red","blue","darkgreen"),  xlab = "Proportions  agreeing",cex=0.8, )plot(density(immi1, na.rm=TRUE))###################################################Attitudes towards immigration restrictions###################################################table(newgap$country, newgap$q24f)rest.x <- numeric()rest.x[newgap$q24f==1] <- 1rest.x[newgap$q24f==2] <- 0.75rest.x[newgap$q24f==3] <- 0.50rest.x[newgap$q24f==4] <- 0.25rest.x[newgap$q24f==8] <- 0rest.x[newgap$q24f==9] <- NAtable(newgap$country, rest.x)rest1 <- tapply(rest.x, newgap$country, mean,na.rm=TRUE)dotchart(rest1[order(rest1)], color=c("red","blue","darkgreen"), xlab = "Proportions agreeing ",cex=0.8)plot(density(rest1, na.rm=TRUE))############################################################Attitudes towards influence from foreign protection############################################################table(newgap$country, newgap$q24e)prot.x <- numeric()prot.x[newgap$q24e==1] <- 1prot.x[newgap$q24e==2] <- 0.75prot.x[newgap$q24e==3] <- 0.50prot.x[newgap$q24e==4] <- 0.25prot.x[newgap$q24e==8] <- 0prot.x[newgap$q24e==9] <- NAtable(newgap$country, prot.x)prot1 <- tapply(prot.x, newgap$country, mean,na.rm=TRUE)prot1dotchart(prot1[order(prot1)], color=c("red","blue","darkgreen"),  xlab = "Proportions agreeing",cex=0.8)plot(density(prot1, na.rm=TRUE))prot <- tapply(prot.x, newgap$code, mean,na.rm=TRUE)###############################################Attitudes towards domestic way of life###############################################table(newgap$country, newgap$q20)life.x <- numeric()life.x[newgap$q20==1] <- 1life.x[newgap$q20==2] <- 0life.x[newgap$q20==8] <- NAlife.x[newgap$q20==9] <- NAtable(newgap$country, life.x)life <- tapply(life.x, newgap$country, mean,na.rm=TRUE)dotchart(life[order(life)], color=c("red","blue","darkgreen"),  xlab = "Proportions agreeing",cex=0.8)mean(life)plot(density(life, na.rm=TRUE))#############################################################################################Attitudes towards conflict between ethnic/racial/nationality/religious/tribal groups#############################################################################################table(newgap$country, newgap$q14b)newgap$q14bconf.x <- numeric()conf.x[newgap$q14b==1] <- 1conf.x[newgap$q14b==2] <- 0.75conf.x[newgap$q14b==3] <- 0.50conf.x[newgap$q14b==4] <- 0.25conf.x[newgap$q14b==8] <- 0conf.x[newgap$q14b==9] <- NAtable(newgap$country, conf.x)conf1 <- tapply(conf.x, newgap$country, mean,na.rm=TRUE)conf1dotchart(conf1[order(conf1)], color=c("red","blue","darkgreen"), xlab = "Proportions agreeing", cex=0.8)plot(density(conf1, na.rm=TRUE))###Average immigration measures###avrg <- (immi1+migr1+prot1+rest1+conf1+life)/6avrgdotchart(avrg[order(avrg)], color=c("red","blue","darkgreen"),  xlab="Average proportions", cex=0.8)newgap2 <- data.frame(migr1, immi1, rest1, prot1, life, conf1)newgap3 <- newgap2[c(1:39, 41:47), ]cor(newgap3[ , c("migr1", "immi1", "rest1", "prot1", "life", "conf1")])cor(newgap3[ , c("prot1", "life", "conf1")])###Correlationspar(mfrow=c(1,2))plot(immi1 ~ migr1, type="n", xlab = "Immigrants are a bad influence in this country", ylab = "Immigration is a problem in this country")text(migr1, immi1, labels=gap2$code)abline(lm(immi1 ~ migr1), col="red")plot(immi1 ~ rest1, type="n", xlab ="Immigration is a problem in this country", ylab ="This country needs more restrictions and Control of Immigrants" )text(rest1, immi1, labels=gap2$code)abline(lm(immi1 ~ rest1), col="red")dev.off()par(mfrow=c(1,2))###Protection from foreign interference vs way of life being affected###plot(rest1 ~ prot1, type="n", xlab="This country needs more restrictions and Control of Immigrants", ylab="This country needs protection against foreign influence")text(prot1, rest1, labels=gap2$code)abline(lm(rest1 ~ prot1), col="red")###Way of life vs Protection from foreign influence###plot(life~prot1, type="n", xlab="Traditional way of life remains strong", ylab="This country needs protection against foreign influence")text(prot1,life, labels=gap2$code)abline(lm(life ~ prot1), col="red")dev.off()par(mfrow=c(1,2))plot(migr1~conf1, type="n", xlab="Immigration is a problem in this country", ylab="Conflict is a problem in this country")text(conf1, migr1, labels=gap2$code)abline(lm(migr1 ~ conf1, data=newgap), col="red")plot(immi1~conf1, type="n", xlab="Immigrants are a bad influence in this country", ylab="Conflict is a problem in this country")text(conf1, immi1, labels=gap2$code)abline(lm(immi1 ~ conf1, data=newgap), col="red")dev.0ff()dev.off()###Chapter 4###sa.only <- subset(newgap, newgap$country=="South Africa")###Immmigration###table(sa.only$q14h)immi2.x <- numeric()immi2.x[sa.only$q14h==1] <- 1immi2.x[sa.only$q14h==2] <- 1immi2.x[sa.only$q14h==3] <- 0immi2.x[sa.only$q14h==4] <- 0immi2.x[sa.only$q14h==8] <- NAimmi2.x[sa.only$q14h==9] <- NAtable(immi2.x)immi2.labs <- c("Problem", "Not a problem")immi2 <- factor(immi2.x, levels=c(1,0), labels=immi2.labs)table(immi2)immi2.1 <- table(immi2)immi.tab <- prop.table(immi2.1)round(immi.tab, digits=2)###Immigrants###table(sa.only$q23f)migr2.x <- numeric()migr2.x[sa.only$q23f==1] <- 1migr2.x[sa.only$q23f==2] <- 1migr2.x[sa.only$q23f==3] <- 0migr2.x[sa.only$q23f==4] <- 0migr2.x[sa.only$q23f==8] <- NAmigr2.x[sa.only$q23f==9] <- NAtable(migr2.x)migr2.labs <- c("Bad", "Good")migr2 <- factor(migr2.x, levels=c(0,1), labels=migr2.labs)migr2.1 <- table(migr2)migr2.tab <- prop.table(migr2.1)round(migr2.tab, digits=2)###Protection from foreign influence###table(sa.only$q24e)prot2.x <- numeric()prot2.x[sa.only$q24e==1] <- 1prot2.x[sa.only$q24e==2] <- 1prot2.x[sa.only$q24e==3] <- 0prot2.x[sa.only$q24e==4] <- 0prot2.x[sa.only$q24e==8] <- NAprot2.x[sa.only$q24e==9] <- NAtable(prot2.x)prot2.labs <- c("Agree", "Disagree")prot2 <- factor(prot2.x, levels=c(1,0), labels=prot2.labs)prot2.1 <- table(prot2)prot.tab <- prop.table(prot2.1)round(prot.tab, digits=2)###Restrictions###table(sa.only$q24f)rest2.x <- numeric()rest2.x[sa.only$q24f==1] <- 1rest2.x[sa.only$q24f==2] <- 1rest2.x[sa.only$q24f==3] <- 0rest2.x[sa.only$q24f==4] <- 0rest2.x[sa.only$q24f==8] <- NArest2.x[sa.only$q24f==9] <- NAtable(rest2.x)rest2.labs <- c("Agree", "Disagree")rest2 <- factor(rest2.x, levels=c(1,0), labels=rest2.labs)rest2.1 <- table(rest2)rest.tab <- prop.table(rest2.1)round(rest.tab, digits=2)###Conflict###table(sa.only$q14b)conf2.x <- numeric()conf2.x[sa.only$q14b==1] <- 1conf2.x[sa.only$q14b==2] <- 1conf2.x[sa.only$q14b==3] <- 0conf2.x[sa.only$q14b==4] <- 0conf2.x[sa.only$q14b==8] <- NAtable(conf2.x)conf2.labs <- c("Problem", "Not a problem")conf2 <- factor(conf2.x, levels=c(1,0), labels=conf2.labs)conf2.1 <- table(conf2)conf.tab <- prop.table(conf2.1)round(conf.tab, digits=2)###Traditional way of life###table(sa.only$q20)life2.x <- numeric()life2.x[sa.only$q20==1] <- 0life2.x[sa.only$q20==2] <- 1life2.x[sa.only$q20==8] <- NAlife2.x[sa.only$q20==9] <- NAtable(life2.x)life2.labs <- c("Getting Lost", "Remains Strong")life2 <- factor(life2.x, levels=c(0,1), labels=life2.labs)life2.1 <- table(life2)life.tab <- prop.table(life2.1)round(life.tab, digits=2)###S.A Employment levels###table(sa.only$q118safr)emp2.x <- numeric()emp2.x [sa.only$q118safr==1] <- 1emp2.x [sa.only$q118safr==2] <- 1emp2.x [sa.only$q118safr==3] <- 1emp2.x [sa.only$q118safr==4] <- 1emp2.x [sa.only$q118safr==5] <- 0emp2.x [sa.only$q118safr==6] <- 0emp2.x [sa.only$q118safr==7] <- 0emp2.x [sa.only$q118safr==8] <- 0emp2.x [sa.only$q118safr==9] <- 0emp2.x [sa.only$q118safr==99] <- NAtable(emp2.x)emp2.labs  <- c("Low level of Employment", "High level of Employment")emp2.1 <- factor(emp2.x, levels=0:1, labels=emp2.labs)emp2.1.tab <- table(emp2.1)emp2.tab <- prop.table(emp2.1.tab)emp2.tabbarplot(emp2.tab, ylab="proportion")###S.A Educations levels###table(sa.only$q118safr)edu2.x <- numeric()edu2.x [sa.only$q118safr==1] <- 0edu2.x [sa.only$q118safr==2] <- 0edu2.x [sa.only$q118safr==3] <- 0edu2.x [sa.only$q118safr==4] <- 0edu2.x [sa.only$q118safr==5] <- 1edu2.x [sa.only$q118safr==6] <- 1edu2.x [sa.only$q118safr==98] <- NAtable(edu2.x)edu2.labs  <- c("Low level of Education", "High level of Education")edu2.1 <- factor(edu2.x, levels=0:1, labels=edu2.labs)edu2.1.tab <- table(edu2.1)edu2.tab <- prop.table(edu2.1.tab)edu2.tabbarplot(edu2.tab, ylab="proportion")###immigration###ptab1.1.2x2 <- table(immi2, emp2.1)ptab1.2x2 <- prop.table(ptab1.1.2x2)ptab1.2x2mosaicplot(t(ptab1.2x2), las=1,           col=TRUE,           main="",           xlab="Employment",           ylab="attitude toward immigration")d <- ptab1.2x2[1,2] - ptab1.2x2[1,1]round(d, digits=2)ptab2.1.2x2 <- table(immi2, edu2.1)ptab2.2x2 <- prop.table(ptab2.1.2x2)mosaicplot(t(ptab2.2x2), las=1,           col=TRUE,           main="",           xlab="Education",           ylab="attitude toward immigration")d <- ptab2.2x2[1,2] - ptab2.2x2[1,1]round(d, digits=2)###Immigrants###ptab3.1.2x2 <- table(migr2, emp2.1)ptab3.2x2 <- prop.table(ptab3.1.2x2)mosaicplot(t(ptab3.2x2), las=1,           col=TRUE,           main="",           xlab="Employment",           ylab="attitude toward immigrants")d <- ptab3.2x2[1,2] - ptab3.2x2[1,1]round(d, digits=2)ptab4.1.2x2 <- table(migr2, edu2.1)ptab4.2x2 <- prop.table(ptab4.1.2x2)mosaicplot(t(ptab4.2x2), las=1,           col=TRUE,           main="",           xlab="Education",           ylab="attitude toward immigrants")d <- ptab4.2x2[1,2] - ptab4.2x2[1,1]round(d, digits=2)###Protection###ptab5.1.2x2 <- table(prot2, emp2.1)ptab5.2x2 <- prop.table(ptab5.1.2x2)mosaicplot(t(ptab5.2x2), las=1,           col=TRUE,           main="",           xlab="Employment",           ylab="attitude toward foreign protection")d <- ptab5.2x2[1,2] - ptab5.2x2[1,1]round(d, digits=2)ptab6.1.2x2 <- table(prot2, edu2.1)ptab6.2x2 <- prop.table(ptab6.1.2x2)mosaicplot(t(ptab6.2x2), las=1,           col=TRUE,           main="",           xlab="Education",           ylab="attitude toward foreign protection")d <- ptab6.2x2[1,2] - ptab6.2x2[1,1]round(d, digits=2)###Restrictions###ptab7.1.2x2 <- table(rest2, emp2.1)ptab7.2x2 <- prop.table(ptab7.1.2x2)mosaicplot(t(ptab7.2x2), las=1,           col=TRUE,           main="",           xlab="Employment",           ylab="attitude toward border restrictions")d <- ptab7.2x2[1,2] - ptab7.2x2[1,1]round(d, digits=2)ptab8.1.2x2 <- table(rest2, edu2.1)ptab8.2x2 <- prop.table(ptab8.1.2x2)mosaicplot(t(ptab8.2x2), las=1,           col=TRUE,           main="",           xlab="Education",           ylab="attitude toward border restrictions")d <- ptab8.2x2[1,2] - ptab8.2x2[1,1]round(d, digits=2)###Way of life###ptab9.1.2x2 <- table(rest2, emp2.1)ptab9.2x2 <- prop.table(ptab9.1.2x2)mosaicplot(t(ptab9.2x2), las=1,           col=TRUE,           main="South Africa, 2007",           xlab="Employment",           ylab="attitude toward border restrictions")d <- ptab9.2x2[1,2] - ptab9.2x2[1,1]round(d, digits=2)ptab10.1.2x2 <- table(rest2, edu2.1)ptab10.2x2 <- prop.table(ptab10.1.2x2)mosaicplot(t(ptab8.2x2), las=1,           col=TRUE,           main="South Africa, 2007",           xlab="Education",           ylab="attitude toward border restrictions")d <- ptab10.2x2[1,2] - ptab10.2x2[1,1]round(d, digits=2)###Conflict###ptab11.1.2x2 <- table(conf2, emp2.1)ptab11.2x2 <- prop.table(ptab11.1.2x2)mosaicplot(t(ptab11.2x2), las=1,           col=TRUE,           main="",           xlab="Employment",           ylab="attitude toward conflict")d <- ptab11.2x2[1,2] - ptab11.2x2[1,1]round(d, digits=2)ptab12.1.2x2 <- table(conf2, edu2.1)ptab12.2x2 <- prop.table(ptab12.1.2x2)mosaicplot(t(ptab12.2x2), las=1,           col=TRUE,           main="",           xlab="Education",           ylab="attitude toward conflict ")d <- ptab12.2x2[1,2] - ptab12.2x2[1,1]round(d, digits=2)#########################Education levels########################### Dichotomous approaches#Every value >= cutpoint becomes 0recode.abs <- function(x, cutpoint, missing=c(98, 99)) {    new <- x    new[new %in% missing] <- NA    new[new < cutpoint] <- 0    new[new >= cutpoint] <- 1    return(new)}#Every value above median becomes 1recode.rel <- function(x, missing=c(98,99)) {    new <- x    new[new %in% missing] <- NA    med <- median(new, na.rm=TRUE)    new[new < med] <- 1    new[new >=  med] <- 0    return(new)}newgap$edu.gen.a <- NAnewgap$edu.gen.a[newgap$country == "Argentina"] <- recode.abs(newgap$q118arg[newgap$country=="Argentina"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Bangladesh"] <- recode.abs(newgap$q118ban[newgap$country=="Bangladesh"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Bolivia"] <- recode.abs(newgap$q118bol[newgap$country=="Bolivia"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Brazil"] <- recode.abs(newgap$q118bra[newgap$country=="Brazil"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Bulgaria"] <- recode.abs(newgap$q118bul[newgap$country=="Bulgaria"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Canada"] <- recode.abs(newgap$q118can[newgap$country=="Canada"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Chile"] <- recode.abs(newgap$q118chil[newgap$country=="Chile"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "China"] <- recode.abs(newgap$q118chin[newgap$country=="China"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Czech Republic"] <- recode.abs(newgap$q118cze[newgap$country=="Czech Republic"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Egypt"] <- recode.abs(newgap$q118egy[newgap$country=="Egypt"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Ethiopia"] <- recode.abs(newgap$q118eth[newgap$country=="Ethiopia"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "France"] <- recode.abs(newgap$q118fra[newgap$country=="France"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Germany"] <- recode.abs(newgap$q118ger[newgap$country=="Germany"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Ghana"] <- recode.abs(newgap$q118gha[newgap$country=="Ghana"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Great Britain"] <- recode.abs(newgap$q118gbr[newgap$country=="Great Britain"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "India"] <- recode.abs(newgap$q118inda[newgap$country=="India"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Indonesia"] <- recode.abs(newgap$q118indo[newgap$country=="Indonesia"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Israel"] <- recode.abs(newgap$q118isr[newgap$country=="Israel"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Italy"] <- recode.abs(newgap$q118ita[newgap$country=="Italy"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Ivory Coast"] <- recode.abs(newgap$q118ivo[newgap$country=="Ivory Coast"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Japan"] <- recode.abs(newgap$q118jap[newgap$country=="Japan"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Jordan"] <- recode.abs(newgap$q118jor[newgap$country=="Jordan"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Kenya"] <- recode.abs(newgap$q118ken[newgap$country=="Kenya"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Kuwait"] <- recode.abs(newgap$q118kuw[newgap$country=="Kuwait"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Lebanon"] <- recode.abs(newgap$q118leb[newgap$country=="Lebanon"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Malaysia"] <- recode.abs(newgap$q118mala[newgap$country=="Malaysia"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Mali"] <- recode.abs(newgap$q118mali[newgap$country=="Mali"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Mexico"] <- recode.abs(newgap$q118mex[newgap$country=="Mexico"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Morocco"] <- recode.abs(newgap$q118mor[newgap$country=="Morocco"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Nigeria"] <- recode.abs(newgap$q118nig[newgap$country=="Nigeria"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Pakistan"] <- recode.abs(newgap$q118pak[newgap$country=="Pakistan"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Palestinian Territories"] <- recode.abs(newgap$q118pal[newgap$country=="Palestinian Territories"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Peru"] <- recode.abs(newgap$q118per[newgap$country=="Peru"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Poland"] <- recode.abs(newgap$q118pol[newgap$country=="Poland"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Russia"] <- recode.abs(newgap$q118rus[newgap$country=="Russia"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Senegal"] <- recode.abs(newgap$q118sen[newgap$country=="Senegal"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Slovakia"] <- recode.abs(newgap$q118slo[newgap$country=="Slovakia"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "South Africa"] <- recode.abs(newgap$q118safr[newgap$country=="South Africa"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "South Korea"] <- recode.abs(newgap$q118skor[newgap$country=="South Korea"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Spain"] <- recode.abs(newgap$q118spa[newgap$country=="Spain"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Sweden"] <- recode.abs(newgap$q118swe[newgap$country=="Sweden"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Tanzania"] <- recode.abs(newgap$q118tan[newgap$country=="Tanzania"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Turkey"] <- recode.abs(newgap$q118tur[newgap$country=="Turkey"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "United States"] <- recode.abs(newgap$q118us[newgap$country=="United States"], cutpoint=2, missing=9) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Uganda"] <- recode.abs(newgap$q118uga[newgap$country=="Uganda"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Ukraine"] <- recode.abs(newgap$q118ukr[newgap$country=="Ukraine"], cutpoint=4) #absolute cutpointnewgap$edu.gen.a[newgap$country == "Venezuela"] <- recode.abs(newgap$q118ven[newgap$country=="Venezuela"], cutpoint=4) #absolute cutpointtable(newgap$country, newgap$edu.gen.a)edu1 <- tapply(newgap$edu.gen.a, newgap$country, mean,na.rm=TRUE)dotchart(edu1[order(edu1)], color=c("red","blue","darkgreen"), main="Mean of absolute levels of education ", cex=0.8)plot(density(edu1, na.rm=TRUE))###Relative cutpoint###newgap$edu.gen.r <- NAnewgap$edu.gen.r[newgap$country == "Argentina"] <- recode.rel(newgap$q118arg[newgap$country=="Argentina"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Bangladesh"] <- recode.rel(newgap$q118ban[newgap$country=="Bangladesh"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Bolivia"] <- recode.rel(newgap$q118bol[newgap$country=="Bolivia"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Brazil"] <- recode.rel(newgap$q118bra[newgap$country=="Brazil"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Bulgaria"] <- recode.rel(newgap$q118bul[newgap$country=="Bulgaria"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Canada"] <- recode.rel(newgap$q118can[newgap$country=="Canada"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Chile"] <- recode.rel(newgap$q118chil[newgap$country=="Chile"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "China"] <- recode.rel(newgap$q118chin[newgap$country=="China"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Czech Republic"] <- recode.rel(newgap$q118cze[newgap$country=="Czech Republic"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Egypt"] <- recode.rel(newgap$q118egy[newgap$country=="Egypt"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Ethiopia"] <- recode.rel(newgap$q118eth[newgap$country=="Ethiopia"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "France"] <- recode.rel(newgap$q118fra[newgap$country=="France"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Germany"] <- recode.rel(newgap$q118ger[newgap$country=="Germany"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Ghana"] <- recode.rel(newgap$q118gha[newgap$country=="Ghana"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Great Britain"] <- recode.rel(newgap$q118gbr[newgap$country=="Great Britain"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "India"] <- recode.rel(newgap$q118inda[newgap$country=="India"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Indonesia"] <- recode.rel(newgap$q118indo[newgap$country=="Indonesia"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Israel"] <- recode.rel(newgap$q118isr[newgap$country=="Israel"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Italy"] <- recode.rel(newgap$q118ita[newgap$country=="Italy"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Ivory Coast"] <- recode.rel(newgap$q118ivo[newgap$country=="Ivory Coast"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Japan"] <- recode.rel(newgap$q118jap[newgap$country=="Japan"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Jordan"] <- recode.rel(newgap$q118jor[newgap$country=="Jordan"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Kenya"] <- recode.rel(newgap$q118ken[newgap$country=="Kenya"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Kuwait"] <- recode.rel(newgap$q118kuw[newgap$country=="Kuwait"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Lebanon"] <- recode.rel(newgap$q118leb[newgap$country=="Lebanon"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Malaysia"] <- recode.rel(newgap$q118mala[newgap$country=="Malaysia"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Mali"] <- recode.rel(newgap$q118mali[newgap$country=="Mali"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Mexico"] <- recode.rel(newgap$q118mex[newgap$country=="Mexico"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Morocco"] <- recode.rel(newgap$q118mor[newgap$country=="Morocco"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Nigeria"] <- recode.rel(newgap$q118nig[newgap$country=="Nigeria"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Pakistan"] <- recode.rel(newgap$q118pak[newgap$country=="Pakistan"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Palestinian Territories"] <- recode.rel(newgap$q118pal[newgap$country=="Palestinian Territories"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Peru"] <- recode.rel(newgap$q118per[newgap$country=="Peru"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Poland"] <- recode.rel(newgap$q118pol[newgap$country=="Poland"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Russia"] <- recode.rel(newgap$q118rus[newgap$country=="Russia"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Senegal"] <- recode.rel(newgap$q118sen[newgap$country=="Senegal"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Slovakia"] <- recode.rel(newgap$q118slo[newgap$country=="Slovakia"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "South Africa"] <- recode.rel(newgap$q118safr[newgap$country=="South Africa"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "South Korea"] <- recode.rel(newgap$q118skor[newgap$country=="South Korea"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Spain"] <- recode.rel(newgap$q118spa[newgap$country=="Spain"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Sweden"] <- recode.rel(newgap$q118swe[newgap$country=="Sweden"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Tanzania"] <- recode.rel(newgap$q118tan[newgap$country=="Tanzania"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Turkey"] <- recode.rel(newgap$q118tur[newgap$country=="Turkey"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "United States"] <- recode.rel(newgap$q118us[newgap$country=="United States"],missing=9)  #relative cutpointnewgap$edu.gen.r[newgap$country == "Uganda"] <- recode.rel(newgap$q118uga[newgap$country=="Uganda"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Ukraine"] <- recode.rel(newgap$q118ukr[newgap$country=="Ukraine"])  #relative cutpointnewgap$edu.gen.r[newgap$country == "Venezuela"] <- recode.rel(newgap$q118ven[newgap$country=="Venezuela"])  #relative cutpointtable(newgap$country, newgap$edu.gen.r)edu2 <- tapply(newgap$edu.gen.r, newgap$country, mean,na.rm=TRUE)dotchart(edu2[order(edu2)], color=c("red","blue","darkgreen"), main="Mean of relative levels of education ", cex=0.8)##########################Employment levles##########################table(newgap$country,newgap$q120)#Every value >1 cutpoint becomes 1recode.abs2 <- function(x, cutpoint, missing=c(98, 99)) {    new <- x    new[new %in% missing] <- NA    new[new <= cutpoint] <- 1    new[new > cutpoint] <- 0    return(new)}table(newgap$country, newgap$q120)newgap$emp.gen.a <- NAnewgap$emp.gen.a[newgap$country == "Argentina"] <- recode.abs2(newgap$q120[newgap$country=="Argentina"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Bangladesh"] <- recode.abs2(newgap$q120[newgap$country=="Bangladesh"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Bolivia"] <- recode.abs2(newgap$q120[newgap$country=="Bolivia"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Brazil"] <- recode.abs2(newgap$q120[newgap$country=="Brazil"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Bulgaria"] <- recode.abs2(newgap$q120[newgap$country=="Bulgaria"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Canada"] <- recode.abs2(newgap$q120[newgap$country=="Canada"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Chile"] <- recode.abs2(newgap$q120[newgap$country=="Chile"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "China"] <- recode.abs2(newgap$q120[newgap$country=="China"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Czech Republic"] <- recode.abs2(newgap$q120[newgap$country=="Czech Republic"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Egypt"] <- recode.abs2(newgap$q120[newgap$country=="Egypt"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Ethiopia"] <- recode.abs2(newgap$q120[newgap$country=="Ethiopia"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "France"] <- recode.abs2(newgap$q120[newgap$country=="France"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Germany"] <- recode.abs2(newgap$q120[newgap$country=="Germany"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Ghana"] <- recode.abs2(newgap$q120[newgap$country=="Ghana"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Great Britain"] <- recode.abs2(newgap$q120[newgap$country=="Great Britain"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "India"] <- recode.abs2(newgap$q120inda[newgap$country=="India"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Indonesia"] <- recode.abs2(newgap$q120[newgap$country=="Indonesia"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Israel"] <- recode.abs2(newgap$q120[newgap$country=="Israel"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Italy"] <- recode.abs2(newgap$q120[newgap$country=="Italy"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Ivory Coast"] <- recode.abs2(newgap$q120[newgap$country=="Ivory Coast"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Japan"] <- recode.abs2(newgap$q120[newgap$country=="Japan"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Jordan"] <- recode.abs2(newgap$q120[newgap$country=="Jordan"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Kenya"] <- recode.abs2(newgap$q120[newgap$country=="Kenya"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Kuwait"] <- recode.abs2(newgap$q120[newgap$country=="Kuwait"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Lebanon"] <- recode.abs2(newgap$q120[newgap$country=="Lebanon"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Malaysia"] <- recode.abs2(newgap$q120[newgap$country=="Malaysia"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Mali"] <- recode.abs2(newgap$q120[newgap$country=="Mali"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Mexico"] <- recode.abs2(newgap$q120[newgap$country=="Mexico"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Morocco"] <- recode.abs2(newgap$q120[newgap$country=="Morocco"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Nigeria"] <- recode.abs2(newgap$q120[newgap$country=="Nigeria"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Pakistan"] <- recode.abs2(newgap$q120[newgap$country=="Pakistan"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Palestinian Territories"] <- recode.abs2(newgap$q120[newgap$country=="Palestinian Territories"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Peru"] <- recode.abs2(newgap$q120[newgap$country=="Peru"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Poland"] <- recode.abs2(newgap$q120[newgap$country=="Poland"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Russia"] <- recode.abs2(newgap$q120[newgap$country=="Russia"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Senegal"] <- recode.abs2(newgap$q120[newgap$country=="Senegal"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Slovakia"] <- recode.abs2(newgap$q120[newgap$country=="Slovakia"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "South Africa"] <- recode.abs2(newgap$q120[newgap$country=="South Africa"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "South Korea"] <- recode.abs2(newgap$q120[newgap$country=="South Korea"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Spain"] <- recode.abs2(newgap$q120spa[newgap$country=="Spain"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Sweden"] <- recode.abs2(newgap$q120[newgap$country=="Sweden"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Tanzania"] <- recode.abs2(newgap$q120[newgap$country=="Tanzania"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Turkey"] <- recode.abs2(newgap$q120[newgap$country=="Turkey"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "United States"] <- recode.abs2(newgap$q120us[newgap$country=="United States"], cutpoint=2, missing=c(8,9)) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Uganda"] <- recode.abs2(newgap$q120[newgap$country=="Uganda"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Ukraine"] <- recode.abs2(newgap$q120[newgap$country=="Ukraine"], cutpoint=2) #absolute cutpointnewgap$emp.gen.a[newgap$country == "Venezuela"] <- recode.abs2(newgap$q120[newgap$country=="Venezuela"], cutpoint=2) #absolute cutpointtable(newgap$country, newgap$emp.gen.a)emp1 <- tapply(newgap$emp.gen.a, newgap$country, mean,na.rm=TRUE)dotchart(emp1[order(emp1)], color=c("red","blue","darkgreen"), main="Mean of absolute levels of employment ", cex=0.8)plot(density(emp1, na.rm=TRUE))####EDU.R###edu vs migr###par(mfrow=c(2,3))newgap$edu.gen.r[newgap$edu.gen.r==0] <- "Low education level"newgap$edu.gen.r[newgap$edu.gen.r==1] <- "High education level"table(newgap$edu.gen.r)migr.x2.labs <- c("Good","Bad")migr.x2 <- numeric()migr.x2[newgap$q23f==1] <- 1migr.x2[newgap$q23f==2] <- 1migr.x2[newgap$q23f==3] <- 2migr.x2[newgap$q23f==4] <- 2migr.x2[newgap$q23f==8] <- NAmigr.x2[newgap$q23f==9] <- NAtable(newgap$country, migr.x2)migr.di <- factor(migr.x2, levels=1:2, labels=migr.x2.labs)table(migr.di)migrvsedu <- table(migr.di, newgap$edu.gen.r)prop.table(migrvsedu, margin=2)migrvsedu.d <- prop.table(migrvsedu, margin=2)d <- migrvsedu.d[1,1] - migrvsedu.d[1,2]dmosaicplot(t(migrvsedu.d), main="Relative Education", ylab="Influence of immigrants")table(migr.di, newgap$edu.gen.r)prop.test(x=c(11568, 6293), n=c(28789,16379))###edu vs immi###newgap$edu.gen.r[newgap$edu.gen.r==0] <- "Low education level"newgap$edu.gen.r[newgap$edu.gen.r==1] <- "High education level"table(newgap$edu.gen.r)immi.x2.labs <- c("Problem","Not a Problem")immi.x2 <- numeric()immi.x2[newgap$q14h==1] <- 1immi.x2[newgap$q14h==2] <- 1immi.x2[newgap$q14h==3] <- 2immi.x2[newgap$q14h==4] <- 2immi.x2[newgap$q14h==8] <- NAimmi.x2[newgap$q14h==9] <- NAtable(newgap$country, immi.x2)immi.di <- factor(immi.x2, levels=1:2, labels=immi.x2.labs)table(immi.di)immivsedu <- table(immi.di, newgap$edu.gen.r)prop.table(immivsedu, margin=2)immivsedu.d <- prop.table(immivsedu, margin=2)d <- immivsedu.d[1,1] - immivsedu.d[1,2]dmosaicplot(t(immivsedu.d), main="Relative Education", ylab="Immigration is a problem")table(immi.di, newgap$edu.gen.r)prop.test(x=c(14148, 7784), n=c(24910,13531))###edu vs prot###newgap$edu.gen.r[newgap$edu.gen.r==0] <- "Low education level"newgap$edu.gen.r[newgap$edu.gen.r==1] <- "High education level"table(newgap$edu.gen.r)prot.x2.labs <- c("Agree","Disagree")prot.x2 <- numeric()prot.x2[newgap$q24e==1] <- 1prot.x2[newgap$q24e==2] <- 1prot.x2[newgap$q24e==3] <- 2prot.x2[newgap$q24e==4] <- 2prot.x2[newgap$q24e==8] <- NAprot.x2[newgap$q24e==9] <- NAtable(newgap$country, prot.x2)prot.di <- factor(prot.x2, levels=1:2, labels=prot.x2.labs)table(prot.di)protvsedu <- table(prot.di, newgap$edu.gen.r)prop.table(protvsedu, margin=2)protvsedu.d <- prop.table(protvsedu, margin=2)d <- protvsedu.d[1,1] - protvsedu.d[1,2]dmosaicplot(t(protvsedu.d), main="Relative Education", ylab="This country needs protection from foreign influence")table(prot.di, newgap$edu.gen.r)prop.test(x=c(19464, 11290), n=c(25320,13983))###edu vs life###newgap$edu.gen.r[newgap$edu.gen.r==0] <- "Low education level"newgap$edu.gen.r[newgap$edu.gen.r==1] <- "High education level"table(newgap$edu.gen.r)life.x2.labs <- c("Getting Lost","Remains Strong")life.x2 <- numeric()life.x2[newgap$q20==1] <- 1life.x2[newgap$q20==2] <- 2life.x2[newgap$q20==8] <- NAlife.x2[newgap$q20==9] <- NAtable(newgap$country, life.x2)life.di <- factor(life.x2, levels=1:2, labels=life.x2.labs)table(life.di)lifevsedu <- table(life.di, newgap$edu.gen.r)prop.table(lifevsedu, margin=2)lifevsedu.d <- prop.table(lifevsedu, margin=2)d <- lifevsedu.d[1,1] - lifevsedu.d[1,2]dmosaicplot(t(lifevsedu.d), main="Relative Education", ylab="Traditional way of life is getting lost")table(life.di, newgap$edu.gen.r)prop.test(x=c(19446, 10570), n=c(25219,14226))###edu vs rest###newgap$edu.gen.r[newgap$edu.gen.r==0] <- "Low education level"newgap$edu.gen.r[newgap$edu.gen.r==1] <- "High education level"table(newgap$edu.gen.r)rest.x2.labs <- c("Agree","Disagree")rest.x2 <- numeric()rest.x2[newgap$q24f==1] <- 1rest.x2[newgap$q24f==2] <- 1rest.x2[newgap$q24f==3] <- 2rest.x2[newgap$q24f==4] <- 2rest.x2[newgap$q24f==8] <- NArest.x2[newgap$q24f==9] <- NAtable(newgap$country, rest.x2)rest.di <- factor(rest.x2, levels=1:2, labels=rest.x2.labs)table(rest.di)restvsedu <- table(rest.di, newgap$edu.gen.r)prop.table(restvsedu, margin=2)restvsedu.d <- prop.table(restvsedu, margin=2)d <- restvsedu.d[1,1] - restvsedu.d[1,2]dmosaicplot(t(restvsedu.d), main="Relative Education", ylab="Country needs more restrictions and control of entry")table(rest.di, newgap$edu.gen.r)prop.test(x=c(18208, 10432), n=c(25143,18208))###edu vs conf###newgap$edu.gen.r[newgap$edu.gen.r==0] <- "Low education level"newgap$edu.gen.r[newgap$edu.gen.r==1] <- "High education level"table(newgap$edu.gen.r)conf.x2.labs <- c("Problem", "Not a Problem")conf.x2 <- numeric()conf.x2[newgap$q14b==1] <- 1conf.x2[newgap$q14b==2] <- 1conf.x2[newgap$q14b==3] <- 2conf.x2[newgap$q14b==4] <- 2conf.x2[newgap$q14b==8] <- NAconf.x2[newgap$q14b==9] <- NAtable(newgap$country, conf.x2)conf.di <- factor(conf.x2, levels=1:2, labels=conf.x2.labs)table(conf.di)confvsedu <- table(conf.di, newgap$edu.gen.r)prop.table(confvsedu, margin=2)confvsedu.d <- prop.table(confvsedu, margin=2)d <- confvsedu.d[1,1] - confvsedu.d[1,2]dmosaicplot(t(confvsedu.d), main="Relative Education", ylab="Conflict is not a problem")table(conf.di, newgap$edu.gen.r)prop.test(x=c(16668, 9032), n=c(23885,12711))dev.off()###EDU.A###par(mfrow=c(2,3))newgap$edu.gen.a[newgap$edu.gen.a==0] <- "Low education level"newgap$edu.gen.a[newgap$edu.gen.a==1] <- "High education level"table(newgap$edu.gen.a)migr.x2.labs <- c("Good","Bad")migr.x2 <- numeric()migr.x2[newgap$q23f==1] <- 1migr.x2[newgap$q23f==2] <- 1migr.x2[newgap$q23f==3] <- 2migr.x2[newgap$q23f==4] <- 2migr.x2[newgap$q23f==8] <- NAmigr.x2[newgap$q23f==9] <- NAtable(newgap$country, migr.x2)migr.di <- factor(migr.x2, levels=1:2, labels=migr.x2.labs)table(migr.di)migrvsedu <- table(migr.di, newgap$edu.gen.a)prop.table(migrvsedu, margin=2)migrvsedu.d <- prop.table(migrvsedu, margin=2)d <- migrvsedu.d[1,1] - migrvsedu.d[1,2]dmosaicplot(t(migrvsedu.d), main="Absolute Education", ylab="Immigrants are a bad influence")###edu vs immi###newgap$edu.gen.a[newgap$edu.gen.a==0] <- "Low education level"newgap$edu.gen.a[newgap$edu.gen.a==1] <- "High education level"table(newgap$edu.gen.a)immi.x2.labs <- c("Problem","Not a Problem")immi.x2 <- numeric()immi.x2[newgap$q14h==1] <- 1immi.x2[newgap$q14h==2] <- 1immi.x2[newgap$q14h==3] <- 2immi.x2[newgap$q14h==4] <- 2immi.x2[newgap$q14h==8] <- NAimmi.x2[newgap$q14h==9] <- NAtable(newgap$country, immi.x2)immi.di <- factor(immi.x2, levels=1:2, labels=immi.x2.labs)table(immi.di)immivsedu <- table(immi.di, newgap$edu.gen.a)prop.table(immivsedu, margin=2)immivsedu.d <- prop.table(immivsedu, margin=2)d <- immivsedu.d[1,1] - immivsedu.d[1,2]dmosaicplot(t(immivsedu.d), main="Absolute Education", ylab="Immigration is a problem")###edu vs prot###newgap$edu.gen.a[newgap$edu.gen.a==0] <- "Low education level"newgap$edu.gen.a[newgap$edu.gen.a==1] <- "High education level"table(newgap$edu.gen.a)prot.x2.labs <- c("Agree","Disagree")prot.x2 <- numeric()prot.x2[newgap$q24e==1] <- 1prot.x2[newgap$q24e==2] <- 1prot.x2[newgap$q24e==3] <- 2prot.x2[newgap$q24e==4] <- 2prot.x2[newgap$q24e==8] <- NAprot.x2[newgap$q24e==9] <- NAtable(newgap$country, prot.x2)prot.di <- factor(prot.x2, levels=1:2, labels=prot.x2.labs)table(prot.di)protvsedu <- table(prot.di, newgap$edu.gen.a)prop.table(protvsedu, margin=2)protvsedu.d <- prop.table(protvsedu, margin=2)d <- protvsedu.d[1,1] - protvsedu.d[1,2]dmosaicplot(t(protvsedu.d), main="Absolute Education", ylab="This country needs protection from foreign influence")###edu vs life###newgap$edu.gen.a[newgap$edu.gen.a==0] <- "Low education level"newgap$edu.gen.a[newgap$edu.gen.a==1] <- "High education level"table(newgap$edu.gen.a)life.x2.labs <- c("Getting Lost","Remains Strong")life.x2 <- numeric()life.x2[newgap$q20==1] <- 1life.x2[newgap$q20==2] <- 2life.x2[newgap$q20==8] <- NAlife.x2[newgap$q20==9] <- NAtable(newgap$country, life.x2)life.di <- factor(life.x2, levels=1:2, labels=life.x2.labs)table(life.di)lifevsedu <- table(life.di, newgap$edu.gen.a)prop.table(lifevsedu, margin=2)lifevsedu.d <- prop.table(lifevsedu, margin=2)d <- lifevsedu.d[1,1] - lifevsedu.d[1,2]dmosaicplot(t(lifevsedu.d), main="Absolute Education", ylab="Traditional way of life is getting lost")###edu vs rest###newgap$edu.gen.a[newgap$edu.gen.a==0] <- "Low education level"newgap$edu.gen.a[newgap$edu.gen.a==1] <- "High education level"table(newgap$edu.gen.a)rest.x2.labs <- c("Agree","Disagree")rest.x2 <- numeric()rest.x2[newgap$q24f==1] <- 1rest.x2[newgap$q24f==2] <- 1rest.x2[newgap$q24f==3] <- 2rest.x2[newgap$q24f==4] <- 2rest.x2[newgap$q24f==8] <- NArest.x2[newgap$q24f==9] <- NAtable(newgap$country, rest.x2)rest.di <- factor(rest.x2, levels=1:2, labels=rest.x2.labs)table(rest.di)restvsedu <- table(rest.di, newgap$edu.gen.a)prop.table(restvsedu, margin=2)restvsedu.d <- prop.table(restvsedu, margin=2)d <- restvsedu.d[1,1] - restvsedu.d[1,2]dmosaicplot(t(restvsedu.d),main="Absolute Education", ylab="Country needs more restrictions and control of entry")###edu vs conf###newgap$edu.gen.a[newgap$edu.gen.a==0] <- "Low education level"newgap$edu.gen.a[newgap$edu.gen.a==1] <- "High education level"table(newgap$edu.gen.a)conf.x2.labs <- c("Problem", "Not a Problem")conf.x2 <- numeric()conf.x2[newgap$q14b==1] <- 1conf.x2[newgap$q14b==2] <- 1conf.x2[newgap$q14b==3] <- 2conf.x2[newgap$q14b==4] <- 2conf.x2[newgap$q14b==8] <- NAconf.x2[newgap$q14b==9] <- NAtable(newgap$country, conf.x2)conf.di <- factor(conf.x2, levels=1:2, labels=conf.x2.labs)table(conf.di)confvsedu <- table(conf.di, newgap$edu.gen.a)prop.table(confvsedu, margin=2)confvsedu.d <- prop.table(confvsedu, margin=2)d <- confvsedu.d[1,1] - confvsedu.d[1,2]dmosaicplot(t(confvsedu.d),main="Absolute Education", ylab="Conflict is not a problem")dev.off()####EMP######emp vs migr ###par(mfrow=c(2,3))newgap$emp.gen.a[newgap$emp.gen.a==0] <- "Unemployed"newgap$emp.gen.a[newgap$emp.gen.a==1] <- "Employed"table(newgap$emp.gen.a)migr.x2.labs <- c("Good","Bad")migr.x2 <- numeric()migr.x2[newgap$q23f==1] <- 1migr.x2[newgap$q23f==2] <- 1migr.x2[newgap$q23f==3] <- 2migr.x2[newgap$q23f==4] <- 2migr.x2[newgap$q23f==8] <- NAmigr.x2[newgap$q23f==9] <- NAtable(newgap$country, migr.x2)migr.di <- factor(migr.x2, levels=1:2, labels=migr.x2.labs)table(migr.di)migrvsemp <- table(migr.di, newgap$emp.gen.a)prop.table(migrvsemp, margin=2)migrvsemp.d <- prop.table(migrvsemp, margin=2)d <- migrvsemp.d[1,1] - migrvsemp.d[1,2]dmosaicplot(t(migrvsemp.d), main="Employment", ylab="Immigrants are a bad influence in this country")table(migr.di, newgap$emp.gen.a)prop.test(x=c(9607, 6866), n=c(18795,13630))###emp vs immi ###newgap$emp.gen.a[newgap$emp.gen.a==0] <- "Unemployed"newgap$emp.gen.a[newgap$emp.gen.a==1] <- "Employed"table(newgap$emp.gen.a)immi.x2.labs <- c("Problem","Not a Problem")immi.x2 <- numeric()immi.x2[newgap$q14h==1] <- 1immi.x2[newgap$q14h==2] <- 1immi.x2[newgap$q14h==3] <- 2immi.x2[newgap$q14h==4] <- 2immi.x2[newgap$q14h==8] <- NAimmi.x2[newgap$q14h==9] <- NAtable(newgap$country, immi.x2)immi.di <- factor(immi.x2, levels=1:2, labels=immi.x2.labs)table(immi.di)immivsemp <- table(immi.di, newgap$emp.gen.a)prop.table(immivsemp, margin=2)immivsemp.d <- prop.table(immivsemp, margin=2)d <- immivsemp.d[1,1] - immivsemp.d[1,2]dmosaicplot(t(immivsemp.d),main="Employment",ylab="Immigration is a problem in this country")table(immi.di, newgap$emp.gen.a)prop.test(x=c(12325, 8772), n=c(20735,14677))###emp vs prot ###newgap$emp.gen.a[newgap$emp.gen.a==0] <- "Unemployed"newgap$emp.gen.a[newgap$emp.gen.a==1] <- "Employed"table(newgap$emp.gen.a)prot.x2.labs <- c("Agree","Disagree")prot.x2 <- numeric()prot.x2[newgap$q24e==1] <- 1prot.x2[newgap$q24e==2] <- 1prot.x2[newgap$q24e==3] <- 2prot.x2[newgap$q24e==4] <- 2prot.x2[newgap$q24e==8] <- NAprot.x2[newgap$q24e==9] <- NAtable(newgap$country, prot.x2)prot.di <- factor(prot.x2, levels=1:2, labels=prot.x2.labs)table(prot.di)protvsemp <- table(prot.di, newgap$emp.gen.a)prop.table(protvsemp, margin=2)protvsemp.d <- prop.table(protvsemp, margin=2)d <- protvsemp.d[1,1] - protvsemp.d[1,2]dmosaicplot(t(protvsemp.d),main="Emplyment",ylab="Protection from foreign influence")table(prot.di, newgap$emp.gen.a)prop.test(x=c(17089, 11262), n=c(21266,14813))###emp vs rest ###newgap$emp.gen.a[newgap$emp.gen.a==0] <- "Unemployed"newgap$emp.gen.a[newgap$emp.gen.a==1] <- "Employed"table(newgap$emp.gen.a)rest.x2.labs <- c("Agree","Disagree")rest.x2 <- numeric()rest.x2[newgap$q24f==1] <- 1rest.x2[newgap$q24f==2] <- 1rest.x2[newgap$q24f==3] <- 2rest.x2[newgap$q24f==4] <- 2rest.x2[newgap$q24f==8] <- NArest.x2[newgap$q24f==9] <- NAtable(newgap$country, rest.x2)rest.di <- factor(rest.x2, levels=1:2, labels=rest.x2.labs)table(rest.di)restvsemp <- table(rest.di, newgap$emp.gen.a)prop.table(restvsemp, margin=2)restvsemp.d <- prop.table(restvsemp, margin=2)d <- restvsemp.d[1,1] - restvsemp.d[1,2]dmosaicplot(t(restvsemp.d),main="Employment",ylab="Country needs more restrictions")table(rest.di, newgap$emp.gen.a)prop.test(x=c(16054, 10784), n=c(21039,14774))###emp vs life ###newgap$emp.gen.a[newgap$emp.gen.a==0] <- "Unemployed"newgap$emp.gen.a[newgap$emp.gen.a==1] <- "Employed"table(newgap$emp.gen.a)life.x2.labs <- c("Getting Lost", "Remains Strong")life.x2 <- numeric()life.x2[newgap$q20==1] <- 1life.x2[newgap$q20==2] <- 2life.x2[newgap$q20==8] <- NAlife.x2[newgap$q20==9] <- NAtable(newgap$country, life.x2)life.di <- factor(life.x2, levels=1:2, labels=life.x2.labs)table(life.di)lifevsemp <- table(life.di, newgap$emp.gen.a)prop.table(lifevsemp, margin=2)lifevsemp.d <- prop.table(lifevsemp, margin=2)d <- lifevsemp.d[1,1] - lifevsemp.d[1,2]dmosaicplot(t(lifevsemp.d),main="Employment",ylab="Traditional way of life is changing")prop.test(lifevsemp)###emp vs conf ###newgap$emp.gen.a[newgap$emp.gen.a==0] <- "Unemployed"newgap$emp.gen.a[newgap$emp.gen.a==1] <- "Employed"table(newgap$emp.gen.a)conf.x2.labs <- c("Problem", "Not a Problem")conf.x2 <- numeric()conf.x2[newgap$q14b==1] <- 1conf.x2[newgap$q14b==2] <- 1conf.x2[newgap$q14b==3] <- 2conf.x2[newgap$q14b==4] <- 2conf.x2[newgap$q14b==8] <- NAconf.x2[newgap$q14b==9] <- NAtable(newgap$country, conf.x2)conf.di <- factor(conf.x2, levels=1:2, labels=conf.x2.labs)table(conf.di)confvsemp <- table(conf.di, newgap$emp.gen.a)prop.table(confvsemp, margin=2)confvsemp.d <- prop.table(confvsemp, margin=2)d <- confvsemp.d[1,1] - confvsemp.d[1,2]dmosaicplot(t(confvsemp.d),main="Employment",ylab="conflict is not a problem")prop.test(confvsemp)dev.off()############################ PROP TEST FUNCTION WITH CONFIDENCE INTERVAL, FOR MANY SAMPLESa## x must be a dichotomous ("outcome") vector## group must be a dichotomous ("grouping") vector## by must be a vector identifying the "samples" (e.g., countries)propDandCI <- function(x, group, by, conf.level=0.95) {  df <- na.omit(data.frame(x=x, group=group, by=by))  df1 <- df[as.numeric(as.factor(df$group))==1, ]  df2 <- df[as.numeric(as.factor(df$group))==2, ]  dtable1 <- addmargins(table(df1$by, df1$x), margin=2)  dtable2 <- addmargins(table(df2$by, df2$x), margin=2)  dtable <- cbind(dtable1, dtable2)  gnames <- rownames(dtable)  get.d <- function(x) {    pt.out <- prop.test(x=x[c(2,5)], n=x[c(3,6)], conf.level=conf.level)    return(diff(pt.out$estimate))  }  get.ciLow <- function(x) {    pt.out <- prop.test(x=x[c(2,5)], n=x[c(3,6)], conf.level=conf.level)    return(-pt.out$conf.int[2])  }  get.ciHigh <- function(x) {    pt.out <- prop.test(x=x[c(2,5)], n=x[c(3,6)], conf.level=conf.level)    return(-pt.out$conf.int[1])  }  d <- apply(dtable, 1, get.d)  ciLow <- apply(dtable, 1, get.ciLow)  ciHigh <- apply(dtable, 1, get.ciHigh)  output <- data.frame(by=gnames, d=d, ciLow=ciLow, ciHigh=ciHigh)  return(output)}#Difference of proportions analysis#Put existing dichotomous variable on immigration into dataframenewgap$immi.di <- immi.di#"Relative" education is already in the data frame#Use new function to run the tabledCompTab1 <- propDandCI(x=newgap$immi.di,                       group=newgap$edu.gen.r,                       by=newgap$country,                       conf.level=.9)round(dCompTab1[ , -1], 3)dCompTab1 <- dCompTab1[order(dCompTab1$d), ]png("newgraph.png", width=480, height=720)dotchart(dCompTab1$d, labels=dCompTab1$by,         xlim=c(min(dCompTab1$ciLow), max(dCompTab1$ciHigh)),         xlab="proportion difference in anti-immigrant sentiment\np(low educ) - p(high educ)",         pch=16)abline(v=0)segments(x0=dCompTab1$ciLow,         x1=dCompTab1$ciHigh,         y0=1:nrow(dCompTab1),         y1=1:nrow(dCompTab1))dev.off()###Immigrationnewgap$migr.di <- migr.didCompTab2 <- propDandCI(x=newgap$migr.di,                       group=newgap$edu.gen.r,                       by=newgap$country,                       conf.level=.9)round(dCompTab2[ , -1], 3)dCompTab2 <- dCompTab2[order(dCompTab2$d), ]png("newgraph2.png", width=480, height=720)dotchart(dCompTab2$d, labels=dCompTab2$by,         xlim=c(min(dCompTab2$ciLow), max(dCompTab2$ciHigh)),         xlab="proportion difference in anti-immigration sentiment\np(low educ) - p(high educ)",         pch=16)abline(v=0)segments(x0=dCompTab2$ciLow,         x1=dCompTab2$ciHigh,         y0=1:nrow(dCompTab2),         y1=1:nrow(dCompTab2))dev.off()###Restrictionnewgap$rest.di <- rest.didCompTab3 <- propDandCI(x=newgap$rest.di,                       group=newgap$edu.gen.r,                       by=newgap$country,                       conf.level=.9)round(dCompTab3[ , -1], 3)dCompTab3 <- dCompTab3[order(dCompTab3$d), ]png("newgraph3.png", width=480, height=720)dotchart(dCompTab3$d, labels=dCompTab3$by,         xlim=c(min(dCompTab3$ciLow), max(dCompTab3$ciHigh)),         xlab="proportion difference in restriction of entry\np(low educ) - p(high educ)",         pch=16)abline(v=0)segments(x0=dCompTab3$ciLow,         x1=dCompTab3$ciHigh,         y0=1:nrow(dCompTab3),         y1=1:nrow(dCompTab3))dev.off()###Protectionnewgap$prot.di <- prot.didCompTab4 <- propDandCI(x=newgap$prot.di,                       group=newgap$edu.gen.r,                       by=newgap$country,                       conf.level=.9)round(dCompTab4[ , -1], 3)dCompTab4 <- dCompTab4[order(dCompTab4$d), ]png("newgraph4.png", width=480, height=720)dotchart(dCompTab4$d, labels=dCompTab4$by,         xlim=c(min(dCompTab4$ciLow), max(dCompTab4$ciHigh)),         xlab="proportion difference in protection from foreign influence\np(low educ) - p(high educ)",         pch=16)abline(v=0)segments(x0=dCompTab4$ciLow,         x1=dCompTab4$ciHigh,         y0=1:nrow(dCompTab4),         y1=1:nrow(dCompTab4))dev.off()###Way of Lifenewgap$life.di <- life.didCompTab5 <- propDandCI(x=newgap$life.di,                       group=newgap$edu.gen.r,                       by=newgap$country,                       conf.level=.9)round(dCompTab5[ , -1], 3)dCompTab5 <- dCompTab5[order(dCompTab5$d), ]png("newgraph5.png", width=480, height=720)dotchart(dCompTab5$d, labels=dCompTab5$by,         xlim=c(min(dCompTab5$ciLow), max(dCompTab5$ciHigh)),         xlab="proportion difference in attitude towards way of life remaining strong\np(low educ) - p(high educ)",         pch=16)abline(v=0)segments(x0=dCompTab5$ciLow,         x1=dCompTab5$ciHigh,         y0=1:nrow(dCompTab5),         y1=1:nrow(dCompTab5))dev.off()###Conflictnewgap$conf.di <- conf.didCompTab6 <- propDandCI(x=newgap$conf.di,                       group=newgap$edu.gen.r,                       by=newgap$country,                       conf.level=.9)round(dCompTab6[ , -1], 3)dCompTab6 <- dCompTab6[order(dCompTab6$d), ]png("newgraph6.png", width=480, height=720)dotchart(dCompTab6$d, labels=dCompTab6$by,         xlim=c(min(dCompTab6$ciLow), max(dCompTab6$ciHigh)),         xlab="proportion difference in attitude towards conflict \np(low educ) - p(high educ)",         pch=16)abline(v=0)segments(x0=dCompTab6$ciLow,         x1=dCompTab6$ciHigh,         y0=1:nrow(dCompTab6),         y1=1:nrow(dCompTab6))dev.off()###Employmentnochina <- newgap[newgap$country != "China", ]nochina$country <- factor(nochina$country)#####dCompTab7 <- propDandCI(x=nochina$immi.di,                       group=nochina$emp.gen.a,                       by=nochina$country,                       conf.level=.9)round(dCompTab7[ , -1], 3)dCompTab7 <- dCompTab7[order(dCompTab7$d), ]png("newgraph7.png", width=480, height=720)dotchart(dCompTab7$d, labels=dCompTab7$by,         xlim=c(min(dCompTab7$ciLow), max(dCompTab7$ciHigh)),         xlab="proportion difference in anti-immigrant sentiment\np(low emp) - p(high emp)",         pch=16)abline(v=0)segments(x0=dCompTab7$ciLow,         x1=dCompTab7$ciHigh,         y0=1:nrow(dCompTab7),         y1=1:nrow(dCompTab7))dev.off()###Immigration#####################################dCompTab8 <- propDandCI(x=nochina$migr.di,                       group=nochina$emp.gen.a,                       by=nochina$country,                       conf.level=.9)round(dCompTab8[ , -1], 3)dCompTab8 <- dCompTab8[order(dCompTab8$d), ]png("newgraph8.png", width=480, height=720)dotchart(dCompTab8$d, labels=dCompTab8$by,         xlim=c(min(dCompTab8$ciLow), max(dCompTab8$ciHigh)),         xlab="proportion difference in anti-immigration sentiment\np(low emp) - p(high emp)",         pch=16)abline(v=0)segments(x0=dCompTab8$ciLow,         x1=dCompTab8$ciHigh,         y0=1:nrow(dCompTab8),         y1=1:nrow(dCompTab8))dev.off()###RestrictiondCompTab9 <- propDandCI(x=nochina$rest.di,                       group=nochina$emp.gen.a,                       by=nochina$country,                       conf.level=.9)round(dCompTab9[ , -1], 3)dCompTab9 <- dCompTab9[order(dCompTab9$d), ]png("newgraph9.png", width=480, height=720)dotchart(dCompTab9$d, labels=dCompTab9$by,         xlim=c(min(dCompTab9$ciLow), max(dCompTab9$ciHigh)),         xlab="proportion difference in restriction of entry\np(low emp) - p(high emp)",         pch=16)abline(v=0)segments(x0=dCompTab9$ciLow,         x1=dCompTab9$ciHigh,         y0=1:nrow(dCompTab9),         y1=1:nrow(dCompTab9))dev.off()###ProtectiondCompTab10 <- propDandCI(x=nochina$prot.di,                       group=nochina$emp.gen.a,                       by=nochina$country,                       conf.level=.9)round(dCompTab10[ , -1], 3)dCompTab10 <- dCompTab10[order(dCompTab10$d), ]png("newgraph10.png", width=500, height=800)dotchart(dCompTab10$d, labels=dCompTab10$by,         xlim=c(min(dCompTab10$ciLow), max(dCompTab10$ciHigh)),         xlab="proportion difference in protection\np(low emp) - p(high emp)",         pch=16)abline(v=0)segments(x0=dCompTab10$ciLow,         x1=dCompTab10$ciHigh,         y0=1:nrow(dCompTab10),         y1=1:nrow(dCompTab10))dev.off()###Way of Life##########################################dCompTab11 <- propDandCI(x=nochina$life.di,                       group=nochina$emp.gen.a,                       by=nochina$country,                       conf.level=.9)round(dCompTab11[ , -1], 3)dCompTab11 <- dCompTab11[order(dCompTab11$d), ]png("newgraph11.png", width=480, height=720)dotchart(dCompTab11$d, labels=dCompTab11$by,         xlim=c(min(dCompTab11$ciLow), max(dCompTab11$ciHigh)),         xlab="proportion difference in attitude towards way of life remaining strong\np(low emp) - p(high emp)",         pch=16)abline(v=0)segments(x0=dCompTab11$ciLow,         x1=dCompTab11$ciHigh,         y0=1:nrow(dCompTab11),         y1=1:nrow(dCompTab11))dev.off()###Conflict################################3dCompTab12 <- propDandCI(x=nochina$conf.di,                       group=nochina$emp.gen.a,                       by=nochina$country,                       conf.level=.9)round(dCompTab12[ , -1], 3)dCompTab12 <- dCompTab12[order(dCompTab12$d), ]png("newgraph12.png", width=480, height=720)dotchart(dCompTab12$d, labels=dCompTab12$by,         xlim=c(min(dCompTab12$ciLow), max(dCompTab12$ciHigh)),         xlab="proportion difference in attitude towards conflict \np(low emp) - p(high emp)",         pch=16)abline(v=0)segments(x0=dCompTab12$ciLow,         x1=dCompTab12$ciHigh,         y0=1:nrow(dCompTab12),         y1=1:nrow(dCompTab12))dev.off()#######################Stastistics Assignment######Load a foreign spss data - Country specific (South Africa) Round 4 Afrobarometer data)library(foreign)data <- read.spss(file="saf.sav",to.data.frame=FALSE,                        use.value.labels=TRUE)objects()names(data)summary(data)###Chang names to lower case###names(data) <- tolower(names(data))names(data)###Subseting Columns###sa <- data[c("q94", "q89", "q1", "q84d_saf", "q76a_saf",  "q76b_saf",  "q57t_saf", "q77c_saf", "q77a_saf", "q101", "q102", "region", "urbrur")]summary(sa)###Recoding data###############################################Trust towards immigrants/foreigners############################################sa$q84d_saf <- sa$q84duncoded <- table(sa$q84d_saf)uncodedbarplot((uncoded), main="Trust towards immigrants")atti.x <- numeric() ### Initialize vectoratti.x[sa$q84d== "Not at all"] <- 1atti.x[sa$q84d== "Just a little"] <- 0.75atti.x[sa$q84d== "Somewhat"] <- 0.5atti.x[sa$q84d== "A lot"] <- 0.25atti.x[sa$q84d== "Don't know"] <- 0atti.x[sa$q84d== "Refused"] <- NAatti.x[sa$q84d== "Missing"] <-NAtable(atti.x)###Make labels###atti.labs <- c("Not at all","Just a little", "Somewhat", "A lot", "Don't know")atti.labs###Make factor###q84 <- factor(atti.x, levels=c(1,0.75,0.5,0.25,0), labels=atti.labs)###Check the coding###table(q84, sa$q84d, exclude=NULL)###Add to the data frame###sa <- data.frame(sa, q84)###Graphing###coded <- table(q84)coded <- prop.table(coded)barplot((coded), main="Trust towards immigrants", ylab="Proportions")###Dichotomous###atti.x2 <- numeric() ### Initialize vectoratti.x2[sa$q84d== "Not at all"] <- 1atti.x2[sa$q84d== "Just a little"] <- 1atti.x2[sa$q84d== "Somewhat"] <- 0atti.x2[sa$q84d== "A lot"] <- 0atti.x2[sa$q84d== "Don't know"] <- NAatti.x2[sa$q84d== "Refused"] <- NAatti.x2[sa$q84d== "Missing"] <-NAtable(atti.x2)###Make labels###atti.labs2 <- c("No","Yes")atti.labs2###Make factor###atti <- factor(atti.x2, levels=c(1,0), labels=atti.labs2)table(atti)######################################Tolerance towards immigration######################################sa$q76a_saf <- sa$q76auncodedtol <- table(sa$q76a)uncodedtolbarplot((uncodedtol), main="Tolerance towards immigration")tol.x <- numeric() ### Initialize vectortol.x[sa$q76a== "Prohibit people entering from other countries"]  <- 1tol.x[sa$q76a== "Place strict restrictions on the number of foreigners who ca"] <- 0.75tol.x[sa$q76a== "Let people in as long as there are jobs available"]  <- 0.5tol.x[sa$q76a== "Let anyone in who wants to enter"] <- 0.25tol.x[sa$q76a== "Don't know"] <- 0tol.x[sa$q76a== "Refused to answer"] <- NAtol.x[sa$q76a== "Missing"]  <- NAtol.x[sa$q76a== "Other"] <- NAtol.x[sa$q76a== "Refused"] <- NAtable(tol.x)###Make labels###tol.labs <- c("Prohibit people", "Place strict restrictions", "Let people in if there are jobs", "Let anyone in", "Don't know")###Make factor###q76a <- factor(tol.x, levels=c(1,0.75,0.5,0.25,0), labels=tol.labs)###Check coding###table(q76a, sa$q76a, exclude=NULL)###Add to data frame###sa <- data.frame(sa, q76a)###Graphing###codedtol <- table(q76a)codedtol <- prop.table(codedtol)barplot((codedtol), main="Tolerance towards immigration", ylab="Proportions")###Dichotomous###tol.x2 <- numeric() ### Initialize vectortol.x2[sa$q76a== "Prohibit people"]  <- 1tol.x2[sa$q76a== "Place strict restrictions"] <- 1tol.x2[sa$q76a== "Let people in as long as there are jobs"]  <- 0tol.x2[sa$q76a== "Let anyone in"] <- 0tol.x2[sa$q76a== "Don't know"] <- NAtol.x2[sa$q76a== "Refused to answer"] <- NAtol.x2[sa$q76a== "Missing data"]  <- NAtable(tol.x2)###Make labels###tol.labs2 <- c("Prohibit people",  "Let people in")###Make factor###tol <- factor(tol.x2, levels=c(1, 0), labels=tol.labs2)table(tol)######################################Tolerance towards deportation######################################sa$q76b_saf <- sa$q76buncodeddep <- table(sa$q76b)uncodeddepbarplot((uncodeddep), main="Tolerance towards deportation")dep.x <- numeric() ### Initialize vectordep.x[sa$q76b== "All of these people"] <- 1dep.x[sa$q76b== "Only those who are not contributing to the economy"] <- 0.75dep.x[sa$q76b== "Only those who are here without the permission of the SAF"] <- 0.5dep.x[sa$q76b== "Only those who have committed crimes"] <- 0.25dep.x[sa$q76b== "The govt. should not send back people to their own countries"] <- 0dep.x[sa$q76b== "Other"] <- 0dep.x[sa$q76b== "Don't know"] <- NAdep.x[sa$q76b== "Missing"]  <- NAtable(dep.x)table(sa$q76b, dep.x, exclude=NULL)###Make labels -- in the right order###dep.labs <- c("Every foreigner", " Those not contributing to the economy", " Those without permits", " Criminal offenders", " None")###Make factor###q76b <- factor(dep.x, levels=c(1,0.75,0.5,0.25,0), labels=dep.labs)###Check coding###table(q76b, sa$q76b, exclude=NULL)###Add to data frame)sa <- data.frame(sa, q76b)###Graphing###codeddep <- table(q76b)codeddep <- prop.table(codeddep)barplot((codeddep), main="Tolerance towards deportation", ylab="Proportions")####################################################################################Attitudes towards the management of immigration by the Mbeki administration####################################################################################sa$q57t_saf <- sa$q57tuncodedman <- table(sa$q57t)uncodedmanbarplot((uncodedman), main="Management of immigration by Mbeki admin")man.x <- numeric() ### Initialize vectorman.x[sa$q57t== "Very Badly"] <- 1man.x[sa$q57t== "Fairly Badly"] <- 0.75man.x[sa$q57t== "Fairly Well"] <- 0.5man.x[sa$q57t== "Very Well"] <- 0.25man.x[sa$q57t== "Don't know/Haven't heard enough"] <- 0man.x[sa$q57t== "Refused"] <- NAman.x[sa$q57t== "Missing"]  <- NAtable(man.x)###Make labels -- in the right order###man.labs <- c("Very Badly", "Fairly Badly", "Fairly Well", "Very Well", "Don't Know/Haven't heard enough ")###Make factor###q57t <- factor(man.x, levels=c(1,0.75,0.5,0.25,0), labels=man.labs)###Check coding###table(q57t, sa$q57t, exclude=NULL)###Add to data frame)sa <- data.frame(sa, q57t)###Graphing###codedman <- table(q57t)codedman <- prop.table(codedman)barplot((codedman), main=" Management of immigration by Mbeki admin", ylab="Proportions")###Dichotomous###man.x2 <- numeric() ### Initialize vectorman.x2[sa$q57t== "Very Badly"] <- 1man.x2[sa$q57t== "Fairly Badly"] <- 1man.x2[sa$q57t== "Fairly Well"] <- 0man.x2[sa$q57t== "Very Well"] <- 0man.x2[sa$q57t== "Don't know/Haven't heard enough"] <- NAman.x2[sa$q57t== "Refused"] <- NAman.x2[sa$q57t== "Missing"]  <- NAtable(man.x2)###Make labels -- in the right order###man.labs2 <- c("Badly","Well")###Make factor###man <- factor(man.x2, levels=c(1,0), labels=man.labs2)table(man)###############################################################################Likelihood of action against foreigners to move into your neighborhood###############################################################################sa$q77a_saf <- sa$q77auncodedact <- table(sa$q77a)uncodedactbarplot((uncodedact), main=" Likelihood of action against foreigners to move into your neighborhood")act.x <- numeric() ### Initialize vectoract.x[sa$q77a=="Very likely"] <- 1act.x[sa$q77a=="Likely"] <- 0.75act.x[sa$q77a=="Not very likely"] <- 0.5act.x[sa$q77a=="Not likely at all"] <- 0.25act.x[sa$q77a=="Don't know"] <- 0act.x[sa$q77a=="Refused"] <- NAact.x[sa$q77a=="Missing"]  <- NAtable(act.x)###Make labels###act.labs <- c("Very likely", "likely", "Not very likely", "Not likely at all", "Don't Know")###Make factor###q77a <- factor(act.x, levels=c(1,0.75,0.5,0.25,0), labels=act.labs)###Check coding###table(q77a, sa$q77a, exclude=NULL)###Add to data frame###sa <- data.frame(sa, q77a)###Graphing###codedact <- table(q77a)codedact <- prop.table(codedact)barplot((codedact), main="Likely of action against foreigners moving into your neighborhood", ylab="Proportions")###Dichotomous###act.x2 <- numeric() ### Initialize vectoract.x2[sa$q77a=="Very likely"] <- 1act.x2[sa$q77a=="Likely"] <- 1act.x2[sa$q77a=="Not very likely"] <- 0act.x2[sa$q77a=="Not likely at all"] <- 0act.x2[sa$q77a=="Don't know"] <- NAact.x2[sa$q77a=="Refused"] <- NAact.x2[sa$q77a=="Missing"]  <- NAtable(act.x2)###Make labels###act.labs2 <- c("Likely", "Not likely")###Make factor###act <- factor(act.x2, levels=c(1,0), labels=act.labs2)table(act)###Recoding education###table(sa$q89)educ.x <- numeric()educ.x[sa$q89=="No formal schooling"] <- 0educ.x[sa$q89=="Informal schooling only"] <- 0educ.x[sa$q89=="Some primary schooling"] <- 0.25educ.x[sa$q89=="Primary school completed"] <- 0.25educ.x[sa$q89=="Some secondary school/high school"] <- 0.5educ.x[sa$q89=="Secondary school completed/high school"] <- 0.5educ.x[sa$q89=="Post-secondary qualifications, not university"] <- 0.75educ.x[sa$q89=="Some university"] <- 0.75educ.x[sa$q89=="University completed"] <- 1educ.x[sa$q89=="Post-graduate"] <- 1educ.x[sa$q89=="Don't know"] <- NAeduc.x[sa$q89=="Refused"] <- NAeduc.x[sa$q89=="Missing"] <- NAtable(educ.x)###Make labels -- in the right order###edu.labs <- c("No formal education, Incomplete primary school","Complete primary education, Incomplete secondary school","Complete secondary school, Incomplete technical school","Complete technical school, Some University-level, without a degree", "University-level education, with degree")###Make factor###q89 <- factor(educ.x, levels=c(1,0.75,0.5,0.25,0), labels=edu.labs)###Check coding###table(q89, sa$q89, exclude=NULL)###Add to data frame###sa <- data.frame(sa, q89)###Graphing###codedemp<- table(q89)codedemp <- prop.table(codedemp)barplot((codedemp), main="Levels of education")###Dichotomous###educ2.x <- numeric()educ2.x[sa$q89=="No formal schooling"] <- 0educ2.x[sa$q89=="Informal schooling only"] <- 0educ2.x[sa$q89=="Some primary schooling"] <- 0educ2.x[sa$q89=="Primary school completed"] <- 0educ2.x[sa$q89=="Some secondary school/high school"] <- 0educ2.x[sa$q89=="Secondary school completed/high school"] <- 1educ2.x[sa$q89=="Post-secondary qualifications, not university"] <- 1educ2.x[sa$q89=="Some university"] <- 1educ2.x[sa$q89=="University completed"] <- 1educ2.x[sa$q89=="Post-graduate"] <- 1educ2.x[sa$q89=="Don't know"] <- NAeduc2.x[sa$q89=="Refused"] <- NAeduc2.x[sa$q89=="Missing"] <- NAtable(educ2.x)###Make labels -- in the right order###educ2.labs <- c("Low level of education", "High level of education")###Make factor###educ2 <- factor(educ2.x, levels=c(0:1), labels=educ2.labs)###Check coding###table(educ2, sa$q89, exclude=NULL)###Add to data frame###sa <- data.frame(sa, educ2)###Graphing###codededuc2<- table(educ2)codededuc2 <- prop.table(codededuc2)barplot((codededuc2), main="Levels of education")dotchart(rev(codededuc2), main=" Levels of education", xlab="Relative Propotions",cex=0.8, color=c("red", "blue"))###Recoding employment###table(sa$q94)empl.x <- numeric()empl.x[sa$q94=="Missing"] <- NAempl.x[sa$q94=="No (not looking)"] <- 1empl.x[sa$q94=="No (looking) Yes"] <- 2empl.x[sa$q94=="part time (not looking)"] <- 3empl.x[sa$q94=="Yes, part time (looking)"] <- 4empl.x[sa$q94=="Yes, full time (not looking)"] <- 5empl.x[sa$q94=="Yes, full time (looking)"] <- 6empl.x[sa$q94=="Don't know"] <- NAempl.x[sa$q94=="Refused"] <- NAtable(empl.x)###Making labels -- in the right order###empl.labs <- c("Not employed (not looking)","Not employed (looking)", "Yes,part time (not looking)","Yes, part time (looking)","Yes, full time (not looking)","Yes, full time (looking)")###Make factor###q94 <- factor(empl.x, levels=1:6, labels=empl.labs)table(q94, sa$q94, exclude=NULL)###Add to data frame###sa <- data.frame(sa, q94)codedempl <- table(q94)codedemplbarplot((codedempl), main="Levels of employment")dotchart(rev(codedempl), main="Levels of employment", xlab="Relative Propotions",cex=0.8, color=c("red", "blue"))table(sa$q94)empl2.x <- numeric()empl2.x[sa$q94=="Missing"] <- NAempl2.x[sa$q94=="No (not looking)"] <- 0empl2.x[sa$q94=="No (looking) Yes"] <- 0empl2.x[sa$q94=="part time (not looking)"] <- 0empl2.x[sa$q94=="Yes, part time (looking)"] <- 0empl2.x[sa$q94=="Yes, full time (not looking)"] <- 1empl2.x[sa$q94=="Yes, full time (looking)"] <- 1empl2.x[sa$q94=="Don't know"] <- NAempl2.x[sa$q94=="Refused"] <- NAtable(empl2.x)###Making labels -- in the right order###empl2.labs <- c("Not employed","Employed(full time)")###Make factor###empl2 <- factor(empl2.x, levels=0:1, labels=empl2.labs)table(empl2, sa$q94, exclude=NULL)###Add to data frame###sa <- data.frame(sa, empl2)###Graphing###codedempl2 <- table(empl2)barplot((codedempl2), main="Levels of employment")dotchart(rev(codedempl2), main=" Levels of employment", xlab="Relative Propotions",cex=0.8, color=c("red", "blue"))###Prop diff###par(mfrow=c(1,2))attivsedu <- table(atti, educ2)attivsedu.d <- prop.table(attivsedu, margin=2)attivsedu.dd <- attivsedu.d[2,1]-attivsedu.d[1,2]dmosaicplot(t(attivsedu.d), main="Education", ylab="Trust towards immigrants", xlab="")prop.test(attivsedu)###attivsemp <- table(atti,empl2)attivsemp.d <- prop.table(attivsemp, margin=2)attivsemp.dd <- attivsemp.d[1,1]-attivsemp.d[1,2]dmosaicplot(t(attivsemp), main="Employment", ylab="Trust towards immigrants", xlab="")prop.test(attivsemp)dev.off()####par(mfrow=c(1,2))tolvsedu <- table(tol, educ2)tolvsedu.d <- prop.table(tolvsedu, margin=2)tolvsedu.dd <- tolvsedu.d[1,1]-tolvsedu.d[1,2]dmosaicplot(t(tolvsedu.d), main="Education", ylab="Tolerance towards immigration", xlab="")prop.test(tolvsedu)###tolvsemp <- table(tol,empl2)tolvsemp.d <- prop.table(tolvsemp, margin=2)tolvsemp.dd <- tolvsemp.d[1,1]-tolvsemp.d[1,2]dmosaicplot(t(tolvsemp), main="Employment", ylab="Tolerance towards immigration", xlab="")prop.test(tolvsemp)dev.off()###par(mfrow=c(1,2))manvsedu <- table(man, educ2)manvsedu.d <- prop.table(manvsedu, margin=2)manvsedu.dd <- manvsedu.d[1,1]-manvsedu.d[1,2]dmosaicplot(t(manvsedu.d), main="Education", ylab="Immigration management by Mbeki Admin", xlab="")###manvsemp <- table(man,empl2)manvsemp.d <- prop.table(manvsemp, margin=2)manvsemp.dd <- manvsemp.d[1,1]-manvsemp.d[1,2]dmosaicplot(t(manvsemp), main="Employment", ylab="Immigration management by Mbeki Admin", xlab="")dev.off()###par(mfrow=c(1,2))actvsedu <- table(act, educ2)actvsedu.d <- prop.table(actvsedu, margin=2)actvsedu.dd <- actvsedu.d[1,1]-actvsedu.d[1,2]dmosaicplot(t(actvsedu.d), main="Education", ylab="Likelihood of action against foreign neighbours", xlab="")###actvsemp <- table(act,empl2)actvsemp.d <- prop.table(actvsemp, margin=2)actvsemp.dd <- actvsemp.d[1,1]-actvsemp.d[1,2]dmosaicplot(t(actvsemp), main="Employment", ylab="Likelihood of action against foreign neighbours", xlab="")dev.off()