### Class examples from Week 2: Data ### Ken Benoit Jan 2010 ## FIRST YOU NEED TO DOWNLOAD THE FILE dail2002.Rdata ## FROM http://www.kenbenoit.net/courses/iqrm/dail2002.Rdata ## ## let's say you download it to your Desktop ## ## then you need to do this (may only work for Mac OS) setwd("~/Desktop") ## or change the working directory from a pull-down menu load("http://www.kenbenoit.net/courses/iqrm/dail2002.Rdata") class(dail2002) attach(dail2002) # frequency distribution of candidates by party table(party) # frequency distribution of candidates by party as proportion prop.table(table(party)) # put them together in a single table using cbind() cbind(table(party), prop.table(table(party))*100) # two-way contingency table of party by incumbency status table(party, incumbf) # sum the columns using apply() apply(table(party, incumbf), 2, sum) # produces too many values! table(spend_total) # this cuts spending into 10 ranges table(cut(spend_total, breaks=10, dig.lab=6)) # this cuts spending into ranges that we define t1 <- table(cut(spend_total, breaks=c(0,5000,10000,20000,30000,40000), dig.lab=6)) t1 # this tabulates spending intervals by candidate status t2 <- table(incumbf, cut(spend_total, breaks=c(0,5000,10000,20000,30000,40000), dig.lab=6)) t2 # we can "transpose" the table using t() t(t2) # pie chart of candidates by party pie(table(party)) # pie chart of spending by party tapply(spend_total, party, sum) pie(tapply(spend_total, party, sum)) pie(tapply(spend_total, party, sum), main="Spending Share by Party") # barplot of candidates by party barplot(table(party)) # barplot of spending intervals from above example barplot(t1) # barplot of spending intervals by cand status from above barplot(t2) barplot(t2, beside=T) barplot(t2, beside=T, legend=T) # box plots of spending by incumbency, then by party plot(incumbf, spend_total) plot(party, spend_total) # histogram of spending hist(spend_total, xlab="Candidate Spending", main="") # density plots to show skew plot(density(spend_total)) plot(density(votes1st, na.rm=T)) # overlay histogram with density plot hist(spend_total, xlab="Candidate Spending", main="", freq=F) lines(density(spend_total)) # scatterplot! plot(spend_total, votes1st)