x<-c(9,3,5,5,1,7) x*2 x-3 sort(x) mean(x) median(x) mode(x) #note: this doesn't work # ---------------------------------------------------------------------------- fuel.frame<-read.table("c:/fuel-frame.txt", header=T, sep=",") names(fuel.frame)[1] "row.names" "Weight" "Disp." "Mileage" "Fuel" "Type" attach(fuel.frame) hist(Mileage) boxplot(Weight ~Type) title("Weight by Vehicle Types") h # ---------------------------------------------------------------------------- #chi squared: evenly distributed? x = c(10, 9, 5, 3, 12, 13, 7, 6, 14, 20) chisq.test(x) #males and females admitted to Berkely admit = c(22, 24) reject = c(351, 317) chisq.test(rbind(admit, reject)) # ---------------------------------------------------------------------------- #one way ANOVA (analysis of variance: what variables affect the results? # How do the variables affect each other?)) # Aircraft primer paints are applied to aluminum surfaces by two methods; # dipping and spraying. A factorial experiment was performed to investigate #the effect of paint primer type and application method on paint adhesion. # Adhesion force was measured, with three different primers and two # application methods. paint <- data.frame(adhf = c(4.0,4.5,4.3,5.6,4.9,5.4,3.8,3.7,4.0,5.4,4.9,5.6,5.8,6.1,6.3,5.5,5.0,5.0), primer = factor(rep(rep(1:3,rep(3,3)),2)),applic = factor(rep(c("D","S"),c(9,9)))) # This is a one-way anova for primer, ignoring application method; # lm() fits the linear model and anova() displayes the results in an # anova table. anova(lm(adhf~primer, data=paint)) # This is a one-way anova for application method, ignoring primer. anova(lm(adhf~applic, data=paint)) # This is a two-way anova for application method, primer and their # interaction. Note that the sums of square for primer and for applic # are the same as computed in the respective one-way analyses. anova(lm(adhf~primer*applic, data=paint)) # The interaction is not significant (P = 0.27) so we can test the main # effects, both of which are highly significant. We conclude that both the # choice of primer and the choice of application method affect the adhesive # force of the paint, and the differences between the three primers are the # same with both application methods, and the difference between the two # application methods is the same with each primer. # ---------------------------------------------------------------------------- Hypothesis testing (Student's T test) x<-c(-1, -2, 1, -1, 3, 4, -3, 6, 7,8,9,0,-8,-7,-3,1,2,3,4,5,7,4,2,7,-1,-2) t.test(x, alternative="greater", mu=0.0, conf.level=0.99) x = c(0, 1, -1, 2, -2, 3, 3, 2, -2, 0, -1, 5, 2, 1, -2, -1) t.test(x, alternative="two.sided", mu=0.0, conf.level=0.99) # ----------------------------------------------------------------------------