#### Quant 1 #### Week 4 Code require(foreign) dail2002 <- read.dta("../02 Univariate/dail2002.dta") dail2002$incumbf <- factor(dail2002$incumb, labels=c("Challenger", "Incumbent")) dail2002$wonseatf <- factor(dail2002$wonseat, labels=c("Lost", "Won")) ## incumbency by outcome table(dail2002$incumbf, dail2002$wonseatf) ## incumbency by outcome by gender levels(dail2002$gender) <- c("Male", "Female") table(dail2002$incumbf, dail2002$wonseatf, dail2002$gender) ftable(table(dail2002$incumbf, dail2002$wonseatf, dail2002$gender), col.vars=c(2,3)) ftable(table(dail2002$incumbf, dail2002$wonseatf, dail2002$gender), col.vars=c(3,2)) ## 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) ## boxplots to compare a continuous variable by categories of a factor variable attach(dail2002) temp.medians <- aggregate(spend_total, by=list(party), median, na.rm=TRUE) temp.partyf <- factor(party, levels=temp.medians[order(temp.medians$x), 1]) boxplot(spend_total ~ temp.partyf, ylab="Total Spending in Euros") levels(temp.partyf) <- toupper(levels(temp.partyf)) boxplot(spend_total ~ temp.partyf, varwidth=T, ylab="Total Spending in Euros") detach(dail2002) ## illustration of data frame indexing, and problems with using attach() is.na(dail2002$votes1st) sum(is.na(dail2002$votes1st)) dail2002$wholename[is.na(dail2002$votes1st)] dail2002[is.na(dail2002$votes1st), c("wholename","votes1st")] attach(dail2002) votes1st[is.na(votes1st)] <- -99 # change this missing value code cbind(wholename, votes1st)[379,] # look at local copy cbind(dail2002$wholename, dail2002$votes1st)[379,] # compare to dframe detach(dail2002)