### Class examples from Week 4: Variability ### Ken Benoit Feb 2010 ## percentiles illustrated x <- (round(runif(100)*100)) # create 100 random values 0-100 xsorted <- sort(x) # sort the values xsorted[c(1,25,50,75,100)] # min; 25th, 50th, 75th %tile; max summary(xsorted) # ask R for same information summary(x) # sorting makes no difference quantile(x,0.25) # 25th percentile using quantile() quantile(x,0.95) # 75th percentile quantile(x,0.50) # 50th percentile (median) median(x) # median (50th percentile) # compute position of 25th %tile using formula (p <- (25/100)*(length(xsorted)+1)) x[floor(p)] # the position before p x[ceiling(p)] # the position after p ## variance and standard deviation from L&F Table 4.1 x <- c(9,8,6,4,2,1) xdev <- x - mean(x) (s2 <- sum(xdev^2)/length(x)) (s <- sqrt(s2)) ## redo figure 4.4 LFF males <- c(5,2,7,9,3,4,3,1,3,8) females <- c(3,5,7,4,5,6,7,6,5,4) boxplot(c(males,females), ylim=c(0,10)) abline(h=c(2,4,6,8), lty="dotted")