Designing Experiments and Analyzing Data: A Model Comparison Perspective (3rd edition) by Maxwell, Delaney, & Kelley

Information about the book is available at https://designingexperiments.com

R Code and Instructions to Accompany Chapter 7

library(AMCP)
data(chapter_7_table_1)
chapter_7_table_1$Group <- factor(chapter_7_table_1$Group)

chapter_7_table_1$Biofeedback <- as.numeric(chapter_7_table_1$Group %in% c(1, 2))

chapter_7_table_1$DrugTherapy <- as.numeric(chapter_7_table_1$Group %in% c(1, 3))
chapter_7_table_1
##    Group Score Biofeedback DrugTherapy
## 1      1   158           1           1
## 2      1   163           1           1
## 3      1   173           1           1
## 4      1   178           1           1
## 5      1   168           1           1
## 6      2   188           1           0
## 7      2   183           1           0
## 8      2   198           1           0
## 9      2   178           1           0
## 10     2   193           1           0
## 11     3   186           0           1
## 12     3   191           0           1
## 13     3   196           0           1
## 14     3   181           0           1
## 15     3   176           0           1
## 16     4   185           0           0
## 17     4   190           0           0
## 18     4   195           0           0
## 19     4   200           0           0
## 20     4   180           0           0

This corresponds to Table 7.2. An ANOVA using the four Groups separately.

ANOVA.Object <- aov(Score~Group, data=chapter_7_table_1)

# Note the following produce the same summaries.
anova(ANOVA.Object)
## Analysis of Variance Table
## 
## Response: Score
##           Df Sum Sq Mean Sq F value   Pr(>F)   
## Group      3   1540  513.33  8.2133 0.001553 **
## Residuals 16   1000   62.50                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(ANOVA.Object)
##             Df Sum Sq Mean Sq F value  Pr(>F)   
## Group        3   1540   513.3   8.213 0.00155 **
## Residuals   16   1000    62.5                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Table 7.3

aggregate(Score ~ Biofeedback+DrugTherapy, mean, data=chapter_7_table_1)
##   Biofeedback DrugTherapy Score
## 1           0           0   190
## 2           1           0   188
## 3           0           1   186
## 4           1           1   168

Alternatively, for more than a single descriptive statistics, dplyr can be used.

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_7_table_1, Group) %>%
summarise(mean=mean(Score), sd=sd(Score))
## # A tibble: 4 × 3
##    Group  mean       sd
##   <fctr> <dbl>    <dbl>
## 1      1   168 7.905694
## 2      2   188 7.905694
## 3      3   186 7.905694
## 4      4   190 7.905694

Interaction plot that compares to Figure 7.1, A. Note that the labels are in the opposite direction as compared to the book.

# Default names
interaction.plot(chapter_7_table_1$Biofeedback, chapter_7_table_1$DrugTherapy, chapter_7_table_1$Score)

# Using better names. 
interaction.plot(chapter_7_table_1$Biofeedback, chapter_7_table_1$DrugTherapy, chapter_7_table_1$Score, xlab="Biofeedback", ylab="Score", trace.label="Drug Therepy", main="Interaction Plot")

Interaction plot that compares to Figure 7.1, B. Again, note that the labels are in the opposite direction as compared to the book.

# Default names
interaction.plot(chapter_7_table_1$DrugTherapy, chapter_7_table_1$Biofeedback, chapter_7_table_1$Score)

#Using better names. 
interaction.plot(chapter_7_table_1$Biofeedback, chapter_7_table_1$DrugTherapy, chapter_7_table_1$Score, xlab="Drug Therepy", ylab="Score", trace.label="Biofeedback", main="Interaction Plot")