#### Quant 1 #### Week 3 Code require(foreign) dail2002 <- read.dta("../02 Univariate/dail2002.dta") dail2002$incumbf <- factor(dail2002$incumb, labels=c("Challenger", "Incumbent")) ## Candidates by party table(dail2002$party) options("digits"=3) # set limit on digits to print prop.table(table(dail2002$party)) # proportions temptable <- cbind(table(dail2002$party), prop.table(table(dail2002$party))*100) # make into single table colnames(temptable) <- c("N", "%") temptable ## two-way tables partyinctable <- table(dail2002$party, dail2002$incumbf) addmargins(partyinctable) # both row and column marginals addmargins(partyinctable, 2) # row marginals addmargins(partyinctable, 1) # column marginals options("digits"=1) prop.table(partyinctable) # proportion of all cells prop.table(partyinctable, 1) # row proportions prop.table(partyinctable, 2) # column proportions addmargins(prop.table(partyinctable)) # total proportion marginals addmargins(prop.table(partyinctable, 1), 2) # row proportions and sum addmargins(prop.table(partyinctable, 2), 1) # column proportions and sum ## three-way tables (partyincgendertable <- table(dail2002$party, dail2002$incumbf, dail2002$gender)) ftable(partyincgendertable) ## barplots to compare 2 variables barplot(partyinctable) barplot(table(dail2002$party, dail2002$incumbf), beside=T, legend.text=T) barplot(table(dail2002$incumbf, dail2002$party), beside=T, legend.text=T) ## comparative boxplot boxplot(dail2002$votes1st ~ dail2002$party, ylab="First Preference Votes") ## compare two density plots plot(density(subset(dail2002$votes1st, dail2002$incumbf=="Challenger"), na.rm=T), main="", xlab="First Preference Votes") lines(density(subset(dail2002$votes1st, dail2002$incumbf=="Incumbent"), na.rm=T)) text(3000,.00020,"Challengers") text(10000,.00015,"Incumbents") ## scatterplot plot(dail2002$votes1st ~ dail2002$spend_total, xlab="Spending in Euros", ylab="First Preference Votes") ## scatterplot distinguishing challengers and incumbents plot(subset(dail2002$votes1st, dail2002$incumbf=="Challenger") ~ subset(dail2002$spend_total, dail2002$incumbf=="Challenger"), col="red", xlab="Spending in Euros", ylab="First Preference Votes") points(subset(dail2002$votes1st, dail2002$incumbf=="Incumbent") ~ subset(dail2002$spend_total, dail2002$incumbf=="Incumbent"), col="blue") legend(0, 9000, c("Challenger", "Incumbent"), col=c("red","blue"), pch="o") sum((x-mean(x))*(y-mean(y))) / sqrt(sum((x-mean(x))^2)*sum((y-mean(y))^2))