These notes build on the prior chapters’ R Code and Instructions, where we discuss some operational aspects of R and detail the use of many functions and how to install packages. Here, we skip details that have already been discussed in the prior R Code and Instructions.
Load the AMCP package.
library(AMCP)
data(chapter_10_table_5)
chapter_10_table_5
## A B ACT
## 1 1 1 28
## 2 1 1 26
## 3 1 1 26
## 4 1 1 23
## 5 1 1 17
## 6 1 2 26
## 7 1 2 26
## 8 1 2 25
## 9 1 2 21
## 10 1 2 17
## 11 1 3 24
## 12 1 3 24
## 13 1 3 19
## 14 1 3 14
## 15 1 3 14
## 16 1 4 20
## 17 1 4 20
## 18 1 4 20
## 19 1 4 18
## 20 1 4 12
## 21 2 1 30
## 22 2 1 28
## 23 2 1 28
## 24 2 1 25
## 25 2 1 19
## 26 2 2 34
## 27 2 2 31
## 28 2 2 29
## 29 2 2 27
## 30 2 2 24
## 31 2 3 31
## 32 2 3 29
## 33 2 3 25
## 34 2 3 21
## 35 2 3 19
## 36 2 4 33
## 37 2 4 30
## 38 2 4 28
## 39 2 4 26
## 40 2 4 23
str(chapter_10_table_5)
## 'data.frame': 40 obs. of 3 variables:
## $ A : int 1 1 1 1 1 1 1 1 1 1 ...
## $ B : int 1 1 1 1 1 2 2 2 2 2 ...
## $ ACT: int 28 26 26 23 17 26 26 25 21 17 ...
# Prepare data.
chapter_10_table_5$Program[chapter_10_table_5$A==1] <- "Computer"
chapter_10_table_5$Program[chapter_10_table_5$A==2] <- "Standard"
chapter_10_table_5$Program <- as.factor(chapter_10_table_5$Program)
chapter_10_table_5$School <- factor(chapter_10_table_5$B)
chapter_10_table_5
## A B ACT Program School
## 1 1 1 28 Computer 1
## 2 1 1 26 Computer 1
## 3 1 1 26 Computer 1
## 4 1 1 23 Computer 1
## 5 1 1 17 Computer 1
## 6 1 2 26 Computer 2
## 7 1 2 26 Computer 2
## 8 1 2 25 Computer 2
## 9 1 2 21 Computer 2
## 10 1 2 17 Computer 2
## 11 1 3 24 Computer 3
## 12 1 3 24 Computer 3
## 13 1 3 19 Computer 3
## 14 1 3 14 Computer 3
## 15 1 3 14 Computer 3
## 16 1 4 20 Computer 4
## 17 1 4 20 Computer 4
## 18 1 4 20 Computer 4
## 19 1 4 18 Computer 4
## 20 1 4 12 Computer 4
## 21 2 1 30 Standard 1
## 22 2 1 28 Standard 1
## 23 2 1 28 Standard 1
## 24 2 1 25 Standard 1
## 25 2 1 19 Standard 1
## 26 2 2 34 Standard 2
## 27 2 2 31 Standard 2
## 28 2 2 29 Standard 2
## 29 2 2 27 Standard 2
## 30 2 2 24 Standard 2
## 31 2 3 31 Standard 3
## 32 2 3 29 Standard 3
## 33 2 3 25 Standard 3
## 34 2 3 21 Standard 3
## 35 2 3 19 Standard 3
## 36 2 4 33 Standard 4
## 37 2 4 30 Standard 4
## 38 2 4 28 Standard 4
## 39 2 4 26 Standard 4
## 40 2 4 23 Standard 4
str(chapter_10_table_5)
## 'data.frame': 40 obs. of 5 variables:
## $ A : int 1 1 1 1 1 1 1 1 1 1 ...
## $ B : int 1 1 1 1 1 2 2 2 2 2 ...
## $ ACT : int 28 26 26 23 17 26 26 25 21 17 ...
## $ Program: Factor w/ 2 levels "Computer","Standard": 1 1 1 1 1 1 1 1 1 1 ...
## $ School : Factor w/ 4 levels "1","2","3","4": 1 1 1 1 1 2 2 2 2 2 ...
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
group_by(chapter_10_table_5, Program, School) %>%
summarise(mean=mean(ACT), sd=sd(ACT), n=n())
## Source: local data frame [8 x 5]
## Groups: Program [?]
##
## Program School mean sd n
## <fctr> <fctr> <dbl> <dbl> <int>
## 1 Computer 1 24 4.301163 5
## 2 Computer 2 23 3.937004 5
## 3 Computer 3 19 5.000000 5
## 4 Computer 4 18 3.464102 5
## 5 Standard 1 26 4.301163 5
## 6 Standard 2 29 3.807887 5
## 7 Standard 3 25 5.099020 5
## 8 Standard 4 28 3.807887 5
Here we fit an ANOVA in which Program is nested within School, which uses a “/” seperator to denote the nesting. In this design, Program is fixed but School is random. Note that for the nesting, A/B denotes that B is nested within A (here, Program nested within School). Unfortunately, it is difficult in R to get the full ANOVA source table easily. One essentially needs to do the calculations manually from the information output provided below. Notice that what is missing is the \(F\) and \(p\)-value.
Mixed.Model.ANOVA <- aov(ACT ~ Program + School + Program*School + Error(School/Program), data=chapter_10_table_5)
summary(Mixed.Model.ANOVA)
##
## Error: School
## Df Sum Sq Mean Sq
## School 3 100 33.33
##
## Error: School:Program
## Df Sum Sq Mean Sq
## Program 1 360 360.0
## Program:School 3 80 26.7
##
## Error: Within
## Df Sum Sq Mean Sq F value Pr(>F)
## Residuals 32 578 18.06