#### Quant 1 #### Week 1 Code # show what we can do with R packages <- c("SPSS","Excel","Stata","R") features <- c(25,50,100,150) cost <- c(100,50,90,0) barplot(features, names.arg=packages, ylab="Feature index") plot(features, -cost, xlab="Feature Index", ylab="(Inverse) Cost Index") text(features, -cost, packages, pos=c(4,4,4,2)) ####################### #####Dail example###### ####################### # load in the Dail 2002 candidate spending data load("dail2002.Rdata") # tabulate incumbency status by victory attach(dail2002) table(incumbf,wonseatf) # produce a mean for each candidate category tapply(spend_total, incumbf, mean) # perform a t-test of difference t.test(spend_total ~ incumbf) # plot spending by candidate category plot(spend_total ~ incumbf) ####################### ### Birthday example### ####################### lbdp <- function(n) { 1 - exp(lfactorial(365) - lfactorial(365-n) - n*log(365)) } x <- 1:60 plot(x,lbdp(x)) plot(x,lbdp(x), xlab="Number of people",ylab="Probability of same birthday") abline(h=.5, lty="dashed", col="red")