### Class examples from Week 3: Central Tendency ### Ken Benoit Feb 2010 ## mode example x <- c(1, 2, 3, 1, 1, 5, 5, 4, 1, 4, 4, 3) mode(x) # this means something very different in R! table(x) # do a frequency distribution # see which values are the maximum values which(table(x)==max(table(x))) ## multiple modes example x <- c(6, 6, 7, 2, 6, 1, 2, 3, 2, 4) which(table(x)==max(table(x))) ## medians x <- c(1, 5, 6, 7, 7, 8) median(x) x <- c(4, 8, 15, 16, 23, 42, 45, 2, 7, 33, 4, 46, 997) sort(x) # put the vector in order length(x) # length of vector is N length(x)%%2 # modulus division: is there are remainder? (length(x)+1)/2 # the position of the median by our formula length(x)%%2==0 # no remainder means this number is ODD median(x) # use R to compute median x <- c(x,998) # add one more number to our test vector x # look at the vector sort(x) # look at the vector sorted (length(x)+1)/2 # position of new median value length(x)%%2 (length(x)+1)/2 # the position of the median by our formula length(x)%%2==0 # should be TRUE if even median(x) ## means, means as deviations x <- c(9,8,6,5,2) xbar <- mean(x) xdev <- x - xbar cbind(x,xdev) xbar ## mean sensitivity to extreme values a <- c(5,6,6,7,8,9,10,10) b <- c(5,6,6,7,8,9,10,95) median(a) median(b) mean(a) mean(b) ## geometric mean example prod(10, 1, 1000, 1, 10) prod(10, 1, 1000, 1, 10)^(1/5) exp(1/5 * sum(10, 1, 100, 10)) exp(mean(log(c(10, 1, 1000, 1, 10)))) ## harmonic mean example x <- c(1,2,4,1) 1/mean(1/x) length(x)/sum(1/x) ## applying mean and median to real data # FIRST YOU NEED TO DOWNLOAD THE FILE dail2002.Rdata # see the code from Week 2 on how to do this load("dail2002.Rdata") attach(dail2002) summary(spend_total) mean(spend_total) plot(density(spend_total)) abline(v=mean(spend_total), col="red") abline(v=median(spend_total), col="blue", lty="dashed") detach(dail2002) ## weighted mean example -- uses a built-in dataset data(state) # load the built-in state dataset state.x77[1:5,1:2] # inspecta snippet of the data attach(data.frame(state.x77)) # coerce as data frame and attach mean(Income) weighted.mean(Income, Population) # automatically normalizes weight median(Income)