R version 3.2.1 (2015-06-18) -- "World-Famous Astronaut" Copyright (C) 2015 The R Foundation for Statistical Computing Platform: x86_64-apple-darwin14.5.0 (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. [Workspace loaded from /home/1004/ma/treibergs/.RData] > # Taken from Rosenkrantz, Probability and Statistics for Science, Engineering and Finance, CRC 2008 > # p. 470. (Source Wetherill (1967) Elementary Statistical methods) > # Data lists arithmetic test scores for pupils divided randomly into five equal sized groups. > # Groups 1 and 2 were taught by the current method. Group 4,5 and 6 were taught together. > # On each day, group 3 students were praised publicly, group 4 students were criticized publicly > # and group 5 students heard the praise and criticism of the other students but were otherwise ignored. > # Is there a difference in the responses of the five groups? > # > # ENTER PUPIL TEST SCORES > # > > Score=scan(); 1: 17 14 24 20 24 23 16 15 24 10: 21 23 13 19 13 19 20 21 16 19: 28 30 29 24 27 30 28 28 23 28: 19 28 26 26 19 24 24 23 22 37: 21 14 13 19 15 15 10 18 20 46: Read 45 items > > # CHECK > > matrix(Score,ncol=9,byrow=T) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 17 14 24 20 24 23 16 15 24 [2,] 21 23 13 19 13 19 20 21 16 [3,] 28 30 29 24 27 30 28 28 23 [4,] 19 28 26 26 19 24 24 23 22 [5,] 21 14 13 19 15 15 10 18 20 > > # ENTER TEACHING METHOD. MAKE IT A FACTOR > > Meth=rep(c("Con1","Con2", "Praised","Criticized","Ignored"),each=9);Meth; [1] "Con1" "Con1" "Con1" "Con1" "Con1" "Con1" "Con1" "Con1" [9] "Con1" "Con2" "Con2" "Con2" "Con2" "Con2" "Con2" "Con2" [17] "Con2" "Con2" "Praised" "Praised" "Praised" "Praised" "Praised" "Praised" [25] "Praised" "Praised" "Praised" "Criticized" "Criticized" "Criticized" "Criticized" "Criticized" [33] "Criticized" "Criticized" "Criticized" "Criticized" "Ignored" "Ignored" "Ignored" "Ignored" [41] "Ignored" "Ignored" "Ignored" "Ignored" "Ignored" > Method=factor(Meth); > > # PRINT SUMMARY BY METHOD > > tapply(Score,Method,summary); $Con1 Min. 1st Qu. Median Mean 3rd Qu. Max. 14.00 16.00 20.00 19.67 24.00 24.00 $Con2 Min. 1st Qu. Median Mean 3rd Qu. Max. 13.00 16.00 19.00 18.33 21.00 23.00 $Criticized Min. 1st Qu. Median Mean 3rd Qu. Max. 19.00 22.00 24.00 23.44 26.00 28.00 $Ignored Min. 1st Qu. Median Mean 3rd Qu. Max. 10.00 14.00 15.00 16.11 19.00 21.00 $Praised Min. 1st Qu. Median Mean 3rd Qu. Max. 23.00 27.00 28.00 27.44 29.00 30.00 > > # SIDE-BY-SIDE BOXPLOT BY METHOD > > plot(Score~Method) > > # RUN ONE-WAY ANALYSIS OF VARIANCE. PRINT ANOVA TABLE, SUM-SQUARES AND DEGREES OF FREEDOM > > p=aov(Score~Method);summary(p) Df Sum Sq Mean Sq F value Pr(>F) Method 4 722.7 180.67 15.27 1.16e-07 *** Residuals 40 473.3 11.83 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 > print(p) Call: aov(formula = Score ~ Method) Terms: Method Residuals Sum of Squares 722.6667 473.3333 Deg. of Freedom 4 40 Residual standard error: 3.439961 Estimated effects may be unbalanced > > # ESTIMATES OF EFFECTS MU_i > > tapply(Score,Method,mean) Con1 Con2 Criticized Ignored Praised 19.66667 18.33333 23.44444 16.11111 27.44444 > > # DO ANOVA "BY HAND" > > MSTr=9*var(tapply(Score,Method,mean));MSTr [1] 180.6667 > MSE=mean(tapply(Score,Method,var));MSE [1] 11.83333 > f=MSTr/MSE;f [1] 15.26761 > pf(f,4,40,lower.tail=F) [1] 1.162741e-07 > qf(.001,4,40,lower.tail=F) [1] 5.698134 > > # PRINT ESTIMATED MU_i'S = "FITTED VALUES" > > matrix(fitted(p),ncol=9,byrow=T) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 19.66667 19.66667 19.66667 19.66667 19.66667 19.66667 19.66667 19.66667 19.66667 [2,] 18.33333 18.33333 18.33333 18.33333 18.33333 18.33333 18.33333 18.33333 18.33333 [3,] 27.44444 27.44444 27.44444 27.44444 27.44444 27.44444 27.44444 27.44444 27.44444 [4,] 23.44444 23.44444 23.44444 23.44444 23.44444 23.44444 23.44444 23.44444 23.44444 [5,] 16.11111 16.11111 16.11111 16.11111 16.11111 16.11111 16.11111 16.11111 16.11111 > > # PRINT RESIDUALS = OBSERVED - FITTED > > matrix(residuals(p),ncol=9,byrow=T) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] -2.6666667 -5.666667 4.333333 0.3333333 4.3333333 3.3333333 -3.6666667 -4.6666667 4.333333 [2,] 2.6666667 4.666667 -5.333333 0.6666667 -5.3333333 0.6666667 1.6666667 2.6666667 -2.333333 [3,] 0.5555556 2.555556 1.555556 -3.4444444 -0.4444444 2.5555556 0.5555556 0.5555556 -4.444444 [4,] -4.4444444 4.555556 2.555556 2.5555556 -4.4444444 0.5555556 0.5555556 -0.4444444 -1.444444 [5,] 4.8888889 -2.111111 -3.111111 2.8888889 -1.1111111 -1.1111111 -6.1111111 1.8888889 3.888889 > > # CHECK ON MODEL TO SEE IF RESIDUALS ARE PLAUSIBLY NORMAL > > qqnorm(residuals(p),,ylab="Residuals");qqline(residuals(p)) >