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")

Moving to the data from Chapter 7, Table 5.

data("chapter_7_table_5")
chapter_7_table_5
##    Score Feedback Drug
## 1    170        1    1
## 2    175        1    1
## 3    165        1    1
## 4    180        1    1
## 5    160        1    1
## 6    186        1    2
## 7    194        1    2
## 8    201        1    2
## 9    215        1    2
## 10   219        1    2
## 11   180        1    3
## 12   187        1    3
## 13   199        1    3
## 14   170        1    3
## 15   204        1    3
## 16   173        2    1
## 17   194        2    1
## 18   197        2    1
## 19   190        2    1
## 20   176        2    1
## 21   189        2    2
## 22   194        2    2
## 23   217        2    2
## 24   206        2    2
## 25   199        2    2
## 26   202        2    3
## 27   228        2    3
## 28   190        2    3
## 29   206        2    3
## 30   224        2    3
aggregate(Score ~ Feedback+Drug, mean, data=chapter_7_table_5)
##   Feedback Drug Score
## 1        1    1   170
## 2        2    1   186
## 3        1    2   203
## 4        2    2   201
## 5        1    3   188
## 6        2    3   210

Here we perform a two-way ANOVA. Because the feedback and Drug data are numeric, we need to instruct R to treat it as a factor.

ANOVA.TwoWay_a <- aov(Score~as.factor(Feedback)*as.factor(Drug), data=chapter_7_table_5)

# Note the following produce the same summaries.
anova(ANOVA.TwoWay_a)
## Analysis of Variance Table
## 
## Response: Score
##                                     Df Sum Sq Mean Sq F value    Pr(>F)
## as.factor(Feedback)                  1   1080 1080.00  6.9342 0.0145635
## as.factor(Drug)                      2   3420 1710.00 10.9791 0.0004113
## as.factor(Feedback):as.factor(Drug)  2    780  390.00  2.5040 0.1028767
## Residuals                           24   3738  155.75                  
##                                        
## as.factor(Feedback)                 *  
## as.factor(Drug)                     ***
## as.factor(Feedback):as.factor(Drug)    
## Residuals                              
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(ANOVA.TwoWay_a)
##                                     Df Sum Sq Mean Sq F value   Pr(>F)    
## as.factor(Feedback)                  1   1080  1080.0   6.934 0.014563 *  
## as.factor(Drug)                      2   3420  1710.0  10.979 0.000411 ***
## as.factor(Feedback):as.factor(Drug)  2    780   390.0   2.504 0.102877    
## Residuals                           24   3738   155.8                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

It is often better to define factors as factors in the dataset itself. Here we do that before replicating the analysis above.

chapter_7_table_5$Drug <- as.factor(chapter_7_table_5$Drug)
chapter_7_table_5$Biofeedback <- as.factor(chapter_7_table_5$Feedback)

# Although it appears nothing changes:
chapter_7_table_5
##    Score Feedback Drug Biofeedback
## 1    170        1    1           1
## 2    175        1    1           1
## 3    165        1    1           1
## 4    180        1    1           1
## 5    160        1    1           1
## 6    186        1    2           1
## 7    194        1    2           1
## 8    201        1    2           1
## 9    215        1    2           1
## 10   219        1    2           1
## 11   180        1    3           1
## 12   187        1    3           1
## 13   199        1    3           1
## 14   170        1    3           1
## 15   204        1    3           1
## 16   173        2    1           2
## 17   194        2    1           2
## 18   197        2    1           2
## 19   190        2    1           2
## 20   176        2    1           2
## 21   189        2    2           2
## 22   194        2    2           2
## 23   217        2    2           2
## 24   206        2    2           2
## 25   199        2    2           2
## 26   202        2    3           2
## 27   228        2    3           2
## 28   190        2    3           2
## 29   206        2    3           2
## 30   224        2    3           2
# notice here that the structure of the data frame has changed (in that Drug and Biofeedback are Factors)
str(chapter_7_table_5)
## 'data.frame':    30 obs. of  4 variables:
##  $ Score      : int  170 175 165 180 160 186 194 201 215 219 ...
##  $ Feedback   : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ Drug       : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 2 2 2 2 2 ...
##  $ Biofeedback: Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...
# Replicate two-way ANOVA
ANOVA.TwoWay <- aov(Score~Biofeedback*Drug, data=chapter_7_table_5)

# Note the following produce the same summaries.
Summary_ANOVA.TwoWay <- summary(ANOVA.TwoWay)
Summary_ANOVA.TwoWay
##                  Df Sum Sq Mean Sq F value   Pr(>F)    
## Biofeedback       1   1080  1080.0   6.934 0.014563 *  
## Drug              2   3420  1710.0  10.979 0.000411 ***
## Biofeedback:Drug  2    780   390.0   2.504 0.102877    
## Residuals        24   3738   155.8                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

As in the code for Chapters 3 the relevant parts of the ANOVA table can be extracted for purposes of calculating the various effect sizes and follow-up tests (like in the Chapter 6 code), as discussed in the book (which can be used for further calculations).

# To see what the object contains.  
str(Summary_ANOVA.TwoWay)
## List of 1
##  $ :Classes 'anova' and 'data.frame':    4 obs. of  5 variables:
##   ..$ Df     : num [1:4] 1 2 2 24
##   ..$ Sum Sq : num [1:4] 1080 3420 780 3738
##   ..$ Mean Sq: num [1:4] 1080 1710 390 156
##   ..$ F value: num [1:4] 6.93 10.98 2.5 NA
##   ..$ Pr(>F) : num [1:4] 0.014563 0.000411 0.102877 NA
##  - attr(*, "class")= chr [1:2] "summary.aov" "listof"
# Extract the sums of squares. 
Summary_ANOVA.TwoWay[[1]]["Sum Sq"]
##                  Sum Sq
## Biofeedback        1080
## Drug               3420
## Biofeedback:Drug    780
## Residuals          3738
# Extract the sums of degrees of freedom.
Summary_ANOVA.TwoWay[[1]]["Df"]
##                  Df
## Biofeedback       1
## Drug              2
## Biofeedback:Drug  2
## Residuals        24
# Extract the mean squares.
Summary_ANOVA.TwoWay[[1]]["Mean Sq"]
##                  Mean Sq
## Biofeedback      1080.00
## Drug             1710.00
## Biofeedback:Drug  390.00
## Residuals         155.75

Form summary values of interest (which can be used for further calculations).

library(dplyr)
# First for Biofeedback
Summary_Score.Biofeedback <- chapter_7_table_5 %>% 
group_by(Biofeedback) %>%
summarize(
mean.Score=mean(Score),
sd.Score=sd(Score),
n.Score=n()
)
Summary_Score.Biofeedback
## # A tibble: 2 × 4
##   Biofeedback mean.Score sd.Score n.Score
##        <fctr>      <dbl>    <dbl>   <int>
## 1           1        187 17.96823      15
## 2           2        199 15.62507      15
# Now for Drug
Summary_Score.Drug <- chapter_7_table_5 %>% 
group_by(Drug) %>%
summarize(
mean.Score=mean(Score),
sd.Score=sd(Score),
n.Score=n()
)
Summary_Score.Drug
## # A tibble: 3 × 4
##     Drug mean.Score sd.Score n.Score
##   <fctr>      <dbl>    <dbl>   <int>
## 1      1        178 12.29273      10
## 2      2        202 11.84155      10
## 3      3        199 18.18424      10

Moving to the data from Table 15, Chapter 7.

data(chapter_7_table_15)
chapter_7_table_15
##    Sex Education Salary
## 1    1         1     24
## 2    1         1     26
## 3    1         1     25
## 4    1         1     24
## 5    1         1     27
## 6    1         1     24
## 7    1         1     27
## 8    1         1     23
## 9    1         2     15
## 10   1         2     17
## 11   1         2     20
## 12   1         2     16
## 13   2         1     25
## 14   2         1     29
## 15   2         1     27
## 16   2         2     19
## 17   2         2     18
## 18   2         2     21
## 19   2         2     20
## 20   2         2     21
## 21   2         2     22
## 22   2         2     19
str(chapter_7_table_15)
## 'data.frame':    22 obs. of  3 variables:
##  $ Sex      : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ Education: int  1 1 1 1 1 1 1 1 2 2 ...
##  $ Salary   : int  24 26 25 24 27 24 27 23 15 17 ...
chapter_7_table_15$Degree <- as.factor(as.numeric(chapter_7_table_15$Education==1))
chapter_7_table_15$Female <- as.factor(as.numeric(chapter_7_table_15$Sex==1))

Unbalanced ANOVA designs are not straightforward, as discussed in the book. There are multiple ways in which the sums of squares can be calculated, and not everyone agrees with the best approach. The book generally recomends what is termed ``Type III’’ sums of squares. Whereas Type III sums of squares is the default in IBM SPSS Statistics and SAS, it is not in R. R uses what the book refers to as Type I sums of squares. Thus, the analysis of the same data in SAS or IBM SPSS vs. R, for example, will differ for unbalanced designs (e.g., when the sample sizes in the cells differ). To demonstrate, consider the “base” ANOVA in R, where Degree is entered first versus where Female is entered first. You will notice that the interaction and residual terms are the same, but that the main effects for the Degree and Female differ, based on the order of entry of the terms. To better understand some of the logic behind R’s appraoch, consider this presentation http://www.stats.ox.ac.uk/pub/MASS3/Exegeses.pdf by W. N. Venables.

ANOVA_C7T15.Degree.1st <- aov(Salary~Degree*Female, data=chapter_7_table_15)
anova(ANOVA_C7T15.Degree.1st)
## Analysis of Variance Table
## 
## Response: Salary
##               Df  Sum Sq Mean Sq F value    Pr(>F)    
## Degree         1 242.227 242.227 87.2018 2.534e-08 ***
## Female         1  30.462  30.462 10.9662  0.003881 ** 
## Degree:Female  1   1.175   1.175  0.4229  0.523690    
## Residuals     18  50.000   2.778                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
ANOVA_C7T15.Female.1st <- aov(Salary~Female*Degree, data=chapter_7_table_15)
anova(ANOVA_C7T15.Female.1st)
## Analysis of Variance Table
## 
## Response: Salary
##               Df  Sum Sq Mean Sq F value    Pr(>F)    
## Female         1   0.297   0.297  0.1069    0.7475    
## Degree         1 272.392 272.392 98.0611 1.038e-08 ***
## Female:Degree  1   1.175   1.175  0.4229    0.5237    
## Residuals     18  50.000   2.778                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

One appraoch to obtaining the Type III sums of squares is to use the drop1() function for a fitted model (either order of entry is fine). See http://www.statmethods.net/stats/anova.html.

ANOVA_T3SS <- drop1(ANOVA_C7T15.Degree.1st, ~., test="F")
ANOVA_T3SS
## Single term deletions
## 
## Model:
## Salary ~ Degree * Female
##               Df Sum of Sq     RSS    AIC F value    Pr(>F)    
## <none>                      50.000 26.062                      
## Degree         1   102.900 152.900 48.652 37.0440 9.447e-06 ***
## Female         1    22.909  72.909 32.360  8.2473   0.01014 *  
## Degree:Female  1     1.175  51.175 24.573  0.4229   0.52369    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# The above is the same as the following. 
drop1(ANOVA_C7T15.Female.1st, ~., test="F")
## Single term deletions
## 
## Model:
## Salary ~ Female * Degree
##               Df Sum of Sq     RSS    AIC F value    Pr(>F)    
## <none>                      50.000 26.062                      
## Female         1    22.909  72.909 32.360  8.2473   0.01014 *  
## Degree         1   102.900 152.900 48.652 37.0440 9.447e-06 ***
## Female:Degree  1     1.175  51.175 24.573  0.4229   0.52369    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

An alternative appraoch is to use the car package, which uses as it’s default Type II sums of squares. However, it is more straight-forward on switching between different types of sums of squares using its Anova() function (note the capital “A”) with the type parameter. Note that either of the previous (Type I) ANOVA objects can be used with the Anova() function to produce the Type III sums of squres. Also, an lm() object can be used, which we recomenmd (see below).

# install.packages("car")
library(car)
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
Anova(ANOVA_C7T15.Degree.1st, type="III")
## Anova Table (Type III tests)
## 
## Response: Salary
##                Sum Sq Df   F value    Pr(>F)    
## (Intercept)   2800.00  1 1008.0000 < 2.2e-16 ***
## Degree         102.90  1   37.0440 9.447e-06 ***
## Female          22.91  1    8.2473   0.01014 *  
## Degree:Female    1.17  1    0.4229   0.52369    
## Residuals       50.00 18                        
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Anova(ANOVA_C7T15.Degree.1st, type="III")
## Anova Table (Type III tests)
## 
## Response: Salary
##                Sum Sq Df   F value    Pr(>F)    
## (Intercept)   2800.00  1 1008.0000 < 2.2e-16 ***
## Degree         102.90  1   37.0440 9.447e-06 ***
## Female          22.91  1    8.2473   0.01014 *  
## Degree:Female    1.17  1    0.4229   0.52369    
## Residuals       50.00 18                        
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Type III sums of squares using Anova() from the car package.

C7T15_lm <- lm(Salary~Female*Degree, data=chapter_7_table_15)
Anova(C7T15_lm, type="III")
## Anova Table (Type III tests)
## 
## Response: Salary
##                Sum Sq Df   F value    Pr(>F)    
## (Intercept)   2800.00  1 1008.0000 < 2.2e-16 ***
## Female          22.91  1    8.2473   0.01014 *  
## Degree         102.90  1   37.0440 9.447e-06 ***
## Female:Degree    1.17  1    0.4229   0.52369    
## Residuals       50.00 18                        
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Analysis for MMPI Depression data (Table 7.23).

data(chapter_7_table_23)
chapter_7_table_23
##    Therapy Severity Score
## 1        1        1    41
## 2        1        1    43
## 3        1        1    50
## 4        1        2    51
## 5        1        2    43
## 6        1        2    53
## 7        1        2    54
## 8        1        2    46
## 9        1        3    45
## 10       1        3    55
## 11       1        3    56
## 12       1        3    60
## 13       1        3    58
## 14       1        3    62
## 15       1        3    62
## 16       2        1    56
## 17       2        1    47
## 18       2        1    45
## 19       2        1    46
## 20       2        1    49
## 21       2        2    58
## 22       2        2    54
## 23       2        2    49
## 24       2        2    61
## 25       2        2    52
## 26       2        2    62
## 27       2        3    59
## 28       2        3    55
## 29       2        3    68
## 30       2        3    63
## 31       3        1    43
## 32       3        1    56
## 33       3        1    48
## 34       3        1    46
## 35       3        1    47
## 36       3        2    59
## 37       3        2    46
## 38       3        2    58
## 39       3        2    54
## 40       3        3    55
## 41       3        3    69
## 42       3        3    63
## 43       3        3    56
## 44       3        3    62
## 45       3        3    67
str(chapter_7_table_23)
## 'data.frame':    45 obs. of  3 variables:
##  $ Therapy : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ Severity: int  1 1 1 2 2 2 2 2 3 3 ...
##  $ Score   : int  41 43 50 51 43 53 54 46 45 55 ...
chapter_7_table_23$Therapy <- chapter_7_table_23$Therapy
chapter_7_table_23$Therapy[chapter_7_table_23$Therapy==1] <- "Cognitive_Behavioral"
chapter_7_table_23$Therapy[chapter_7_table_23$Therapy==2] <- "Rogerian"
chapter_7_table_23$Therapy[chapter_7_table_23$Therapy==3] <- "Assertiveness_Training"

chapter_7_table_23$Severity <- chapter_7_table_23$Severity
chapter_7_table_23$Severity[chapter_7_table_23$Severity==1] <- "Mild"
chapter_7_table_23$Severity[chapter_7_table_23$Severity==2] <- "Moderate"
chapter_7_table_23$Severity[chapter_7_table_23$Severity==3] <- "Severe"

chapter_7_table_23
##                   Therapy Severity Score
## 1    Cognitive_Behavioral     Mild    41
## 2    Cognitive_Behavioral     Mild    43
## 3    Cognitive_Behavioral     Mild    50
## 4    Cognitive_Behavioral Moderate    51
## 5    Cognitive_Behavioral Moderate    43
## 6    Cognitive_Behavioral Moderate    53
## 7    Cognitive_Behavioral Moderate    54
## 8    Cognitive_Behavioral Moderate    46
## 9    Cognitive_Behavioral   Severe    45
## 10   Cognitive_Behavioral   Severe    55
## 11   Cognitive_Behavioral   Severe    56
## 12   Cognitive_Behavioral   Severe    60
## 13   Cognitive_Behavioral   Severe    58
## 14   Cognitive_Behavioral   Severe    62
## 15   Cognitive_Behavioral   Severe    62
## 16               Rogerian     Mild    56
## 17               Rogerian     Mild    47
## 18               Rogerian     Mild    45
## 19               Rogerian     Mild    46
## 20               Rogerian     Mild    49
## 21               Rogerian Moderate    58
## 22               Rogerian Moderate    54
## 23               Rogerian Moderate    49
## 24               Rogerian Moderate    61
## 25               Rogerian Moderate    52
## 26               Rogerian Moderate    62
## 27               Rogerian   Severe    59
## 28               Rogerian   Severe    55
## 29               Rogerian   Severe    68
## 30               Rogerian   Severe    63
## 31 Assertiveness_Training     Mild    43
## 32 Assertiveness_Training     Mild    56
## 33 Assertiveness_Training     Mild    48
## 34 Assertiveness_Training     Mild    46
## 35 Assertiveness_Training     Mild    47
## 36 Assertiveness_Training Moderate    59
## 37 Assertiveness_Training Moderate    46
## 38 Assertiveness_Training Moderate    58
## 39 Assertiveness_Training Moderate    54
## 40 Assertiveness_Training   Severe    55
## 41 Assertiveness_Training   Severe    69
## 42 Assertiveness_Training   Severe    63
## 43 Assertiveness_Training   Severe    56
## 44 Assertiveness_Training   Severe    62
## 45 Assertiveness_Training   Severe    67
str(chapter_7_table_23)
## 'data.frame':    45 obs. of  3 variables:
##  $ Therapy : chr  "Cognitive_Behavioral" "Cognitive_Behavioral" "Cognitive_Behavioral" "Cognitive_Behavioral" ...
##  $ Severity: chr  "Mild" "Mild" "Mild" "Moderate" ...
##  $ Score   : int  41 43 50 51 43 53 54 46 45 55 ...

Analysis of data from chapter_7_table_23

Descriptives of the cells usingdplyr can be used.

group_by(chapter_7_table_23, Therapy, Severity) %>%
summarise(mean=mean(Score), sd=sd(Score), n=n())
## Source: local data frame [9 x 5]
## Groups: Therapy [?]
## 
##                  Therapy Severity     mean       sd     n
##                    <chr>    <chr>    <dbl>    <dbl> <int>
## 1 Assertiveness_Training     Mild 48.00000 4.847680     5
## 2 Assertiveness_Training Moderate 54.25000 5.909033     4
## 3 Assertiveness_Training   Severe 62.00000 5.656854     6
## 4   Cognitive_Behavioral     Mild 44.66667 4.725816     3
## 5   Cognitive_Behavioral Moderate 49.40000 4.722288     5
## 6   Cognitive_Behavioral   Severe 56.85714 5.899960     7
## 7               Rogerian     Mild 48.60000 4.393177     5
## 8               Rogerian Moderate 56.00000 5.176872     6
## 9               Rogerian   Severe 61.25000 5.560276     4

Descriptives of the marginal means for Therapy and Degree usingdplyr can be used.

group_by(chapter_7_table_23, Therapy) %>%
summarise(mean=mean(Score), sd=sd(Score), n=n())
## # A tibble: 3 × 4
##                  Therapy     mean       sd     n
##                    <chr>    <dbl>    <dbl> <int>
## 1 Assertiveness_Training 55.26667 8.013085    15
## 2   Cognitive_Behavioral 51.93333 7.085868    15
## 3               Rogerian 54.93333 6.922702    15
group_by(chapter_7_table_23, Severity) %>%
summarise(mean=mean(Score), sd=sd(Score), n=n())
## # A tibble: 3 × 4
##   Severity     mean       sd     n
##      <chr>    <dbl>    <dbl> <int>
## 1     Mild 47.46154 4.539005    13
## 2 Moderate 53.33333 5.677860    15
## 3   Severe 59.70588 5.913594    17

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_23$Therapy, chapter_7_table_23$Severity, chapter_7_table_23$Score, xlab="Type of Therepy", ylab="Score", trace.label="Degree of Severity", main="Interaction Plot for MMPI Depression Scores")

Type I sums of squares.

C7T23_SS1a <- aov(Score~Therapy*Severity, data=chapter_7_table_23)
anova(C7T23_SS1a)
## Analysis of Variance Table
## 
## Response: Score
##                  Df  Sum Sq Mean Sq F value    Pr(>F)    
## Therapy           2  101.11   50.56  1.8102    0.1782    
## Severity          2 1253.19  626.59 22.4357 4.711e-07 ***
## Therapy:Severity  4   14.19    3.55  0.1270    0.9717    
## Residuals        36 1005.42   27.93                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
C7T23_SS1b <- aov(Score~Severity*Therapy, data=chapter_7_table_23)
anova(C7T23_SS1b)
## Analysis of Variance Table
## 
## Response: Score
##                  Df  Sum Sq Mean Sq F value    Pr(>F)    
## Severity          2 1115.82  557.91 19.9764 1.458e-06 ***
## Therapy           2  238.48  119.24  4.2695   0.02168 *  
## Severity:Therapy  4   14.19    3.55  0.1270   0.97170    
## Residuals        36 1005.42   27.93                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Type II sums of squares.

C7T23_lm <- lm(Score~Therapy*Severity, data=chapter_7_table_23)
Anova(C7T23_lm, type="II")
## Anova Table (Type II tests)
## 
## Response: Score
##                   Sum Sq Df F value    Pr(>F)    
## Therapy           238.48  2  4.2695   0.02168 *  
## Severity         1253.19  2 22.4357 4.711e-07 ***
## Therapy:Severity   14.19  4  0.1270   0.97170    
## Residuals        1005.42 36                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Type III sums of squares.

Anova(C7T23_lm, type="III")
## Anova Table (Type III tests)
## 
## Response: Score
##                   Sum Sq Df  F value    Pr(>F)    
## (Intercept)      11520.0  1 412.4828 < 2.2e-16 ***
## Therapy             31.4  2   0.5615  0.575263    
## Severity           540.2  2   9.6708  0.000435 ***
## Therapy:Severity    14.2  4   0.1270  0.971701    
## Residuals         1005.4 36                       
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
C7T23_lm <- lm(Score~Therapy*Severity, data=chapter_7_table_23) # Repeated from above

For an of the ANOVA models of interest, we can perform Tukey HSD tests with the TukeyHSD() function.

C7T23_ANOVA <- aov(Score~Therapy*Severity, data=chapter_7_table_23) # Repeated from above
TukeyHSD(C7T23_ANOVA)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = Score ~ Therapy * Severity, data = chapter_7_table_23)
## 
## $Therapy
##                                                   diff       lwr      upr
## Cognitive_Behavioral-Assertiveness_Training -3.3333333 -8.050125 1.383458
## Rogerian-Assertiveness_Training             -0.3333333 -5.050125 4.383458
## Rogerian-Cognitive_Behavioral                3.0000000 -1.716792 7.716792
##                                                 p adj
## Cognitive_Behavioral-Assertiveness_Training 0.2090188
## Rogerian-Assertiveness_Training             0.9836924
## Rogerian-Cognitive_Behavioral               0.2781897
## 
## $Severity
##                      diff      lwr      upr     p adj
## Moderate-Mild    6.218803 1.323957 11.11365 0.0100799
## Severe-Mild     12.797888 8.038602 17.55717 0.0000004
## Severe-Moderate  6.579085 2.003125 11.15505 0.0033855
## 
## $`Therapy:Severity`
##                                                                     diff
## Cognitive_Behavioral:Mild-Assertiveness_Training:Mild         -3.3333333
## Rogerian:Mild-Assertiveness_Training:Mild                      0.6000000
## Assertiveness_Training:Moderate-Assertiveness_Training:Mild    6.2500000
## Cognitive_Behavioral:Moderate-Assertiveness_Training:Mild      1.4000000
## Rogerian:Moderate-Assertiveness_Training:Mild                  8.0000000
## Assertiveness_Training:Severe-Assertiveness_Training:Mild     14.0000000
## Cognitive_Behavioral:Severe-Assertiveness_Training:Mild        8.8571429
## Rogerian:Severe-Assertiveness_Training:Mild                   13.2500000
## Rogerian:Mild-Cognitive_Behavioral:Mild                        3.9333333
## Assertiveness_Training:Moderate-Cognitive_Behavioral:Mild      9.5833333
## Cognitive_Behavioral:Moderate-Cognitive_Behavioral:Mild        4.7333333
## Rogerian:Moderate-Cognitive_Behavioral:Mild                   11.3333333
## Assertiveness_Training:Severe-Cognitive_Behavioral:Mild       17.3333333
## Cognitive_Behavioral:Severe-Cognitive_Behavioral:Mild         12.1904762
## Rogerian:Severe-Cognitive_Behavioral:Mild                     16.5833333
## Assertiveness_Training:Moderate-Rogerian:Mild                  5.6500000
## Cognitive_Behavioral:Moderate-Rogerian:Mild                    0.8000000
## Rogerian:Moderate-Rogerian:Mild                                7.4000000
## Assertiveness_Training:Severe-Rogerian:Mild                   13.4000000
## Cognitive_Behavioral:Severe-Rogerian:Mild                      8.2571429
## Rogerian:Severe-Rogerian:Mild                                 12.6500000
## Cognitive_Behavioral:Moderate-Assertiveness_Training:Moderate -4.8500000
## Rogerian:Moderate-Assertiveness_Training:Moderate              1.7500000
## Assertiveness_Training:Severe-Assertiveness_Training:Moderate  7.7500000
## Cognitive_Behavioral:Severe-Assertiveness_Training:Moderate    2.6071429
## Rogerian:Severe-Assertiveness_Training:Moderate                7.0000000
## Rogerian:Moderate-Cognitive_Behavioral:Moderate                6.6000000
## Assertiveness_Training:Severe-Cognitive_Behavioral:Moderate   12.6000000
## Cognitive_Behavioral:Severe-Cognitive_Behavioral:Moderate      7.4571429
## Rogerian:Severe-Cognitive_Behavioral:Moderate                 11.8500000
## Assertiveness_Training:Severe-Rogerian:Moderate                6.0000000
## Cognitive_Behavioral:Severe-Rogerian:Moderate                  0.8571429
## Rogerian:Severe-Rogerian:Moderate                              5.2500000
## Cognitive_Behavioral:Severe-Assertiveness_Training:Severe     -5.1428571
## Rogerian:Severe-Assertiveness_Training:Severe                 -0.7500000
## Rogerian:Severe-Cognitive_Behavioral:Severe                    4.3928571
##                                                                       lwr
## Cognitive_Behavioral:Mild-Assertiveness_Training:Mild         -16.0582074
## Rogerian:Mild-Assertiveness_Training:Mild                     -10.4200642
## Assertiveness_Training:Moderate-Assertiveness_Training:Mild    -5.4385432
## Cognitive_Behavioral:Moderate-Assertiveness_Training:Mild      -9.6200642
## Rogerian:Moderate-Assertiveness_Training:Mild                  -2.5509082
## Assertiveness_Training:Severe-Assertiveness_Training:Mild       3.4490918
## Cognitive_Behavioral:Severe-Assertiveness_Training:Mild        -1.3454541
## Rogerian:Severe-Assertiveness_Training:Mild                     1.5614568
## Rogerian:Mild-Cognitive_Behavioral:Mild                        -8.7915407
## Assertiveness_Training:Moderate-Cognitive_Behavioral:Mild      -3.7246585
## Cognitive_Behavioral:Moderate-Cognitive_Behavioral:Mild        -7.9915407
## Rogerian:Moderate-Cognitive_Behavioral:Mild                    -0.9874730
## Assertiveness_Training:Severe-Cognitive_Behavioral:Mild         5.0125270
## Cognitive_Behavioral:Severe-Cognitive_Behavioral:Mild           0.1666004
## Rogerian:Severe-Cognitive_Behavioral:Mild                       3.2753415
## Assertiveness_Training:Moderate-Rogerian:Mild                  -6.0385432
## Cognitive_Behavioral:Moderate-Rogerian:Mild                   -10.2200642
## Rogerian:Moderate-Rogerian:Mild                                -3.1509082
## Assertiveness_Training:Severe-Rogerian:Mild                     2.8490918
## Cognitive_Behavioral:Severe-Rogerian:Mild                      -1.9454541
## Rogerian:Severe-Rogerian:Mild                                   0.9614568
## Cognitive_Behavioral:Moderate-Assertiveness_Training:Moderate -16.5385432
## Rogerian:Moderate-Assertiveness_Training:Moderate              -9.4973059
## Assertiveness_Training:Severe-Assertiveness_Training:Moderate  -3.4973059
## Cognitive_Behavioral:Severe-Assertiveness_Training:Moderate    -8.3140847
## Rogerian:Severe-Assertiveness_Training:Moderate                -5.3208063
## Rogerian:Moderate-Cognitive_Behavioral:Moderate                -3.9509082
## Assertiveness_Training:Severe-Cognitive_Behavioral:Moderate     2.0490918
## Cognitive_Behavioral:Severe-Cognitive_Behavioral:Moderate      -2.7454541
## Rogerian:Severe-Cognitive_Behavioral:Moderate                   0.1614568
## Assertiveness_Training:Severe-Rogerian:Moderate                -4.0598962
## Cognitive_Behavioral:Severe-Rogerian:Moderate                  -8.8368157
## Rogerian:Severe-Rogerian:Moderate                              -5.9973059
## Cognitive_Behavioral:Severe-Assertiveness_Training:Severe     -14.8368157
## Rogerian:Severe-Assertiveness_Training:Severe                 -11.9973059
## Rogerian:Severe-Cognitive_Behavioral:Severe                    -6.5283704
##                                                                     upr
## Cognitive_Behavioral:Mild-Assertiveness_Training:Mild          9.391541
## Rogerian:Mild-Assertiveness_Training:Mild                     11.620064
## Assertiveness_Training:Moderate-Assertiveness_Training:Mild   17.938543
## Cognitive_Behavioral:Moderate-Assertiveness_Training:Mild     12.420064
## Rogerian:Moderate-Assertiveness_Training:Mild                 18.550908
## Assertiveness_Training:Severe-Assertiveness_Training:Mild     24.550908
## Cognitive_Behavioral:Severe-Assertiveness_Training:Mild       19.059740
## Rogerian:Severe-Assertiveness_Training:Mild                   24.938543
## Rogerian:Mild-Cognitive_Behavioral:Mild                       16.658207
## Assertiveness_Training:Moderate-Cognitive_Behavioral:Mild     22.891325
## Cognitive_Behavioral:Moderate-Cognitive_Behavioral:Mild       17.458207
## Rogerian:Moderate-Cognitive_Behavioral:Mild                   23.654140
## Assertiveness_Training:Severe-Cognitive_Behavioral:Mild       29.654140
## Cognitive_Behavioral:Severe-Cognitive_Behavioral:Mild         24.214352
## Rogerian:Severe-Cognitive_Behavioral:Mild                     29.891325
## Assertiveness_Training:Moderate-Rogerian:Mild                 17.338543
## Cognitive_Behavioral:Moderate-Rogerian:Mild                   11.820064
## Rogerian:Moderate-Rogerian:Mild                               17.950908
## Assertiveness_Training:Severe-Rogerian:Mild                   23.950908
## Cognitive_Behavioral:Severe-Rogerian:Mild                     18.459740
## Rogerian:Severe-Rogerian:Mild                                 24.338543
## Cognitive_Behavioral:Moderate-Assertiveness_Training:Moderate  6.838543
## Rogerian:Moderate-Assertiveness_Training:Moderate             12.997306
## Assertiveness_Training:Severe-Assertiveness_Training:Moderate 18.997306
## Cognitive_Behavioral:Severe-Assertiveness_Training:Moderate   13.528370
## Rogerian:Severe-Assertiveness_Training:Moderate               19.320806
## Rogerian:Moderate-Cognitive_Behavioral:Moderate               17.150908
## Assertiveness_Training:Severe-Cognitive_Behavioral:Moderate   23.150908
## Cognitive_Behavioral:Severe-Cognitive_Behavioral:Moderate     17.659740
## Rogerian:Severe-Cognitive_Behavioral:Moderate                 23.538543
## Assertiveness_Training:Severe-Rogerian:Moderate               16.059896
## Cognitive_Behavioral:Severe-Rogerian:Moderate                 10.551101
## Rogerian:Severe-Rogerian:Moderate                             16.497306
## Cognitive_Behavioral:Severe-Assertiveness_Training:Severe      4.551101
## Rogerian:Severe-Assertiveness_Training:Severe                 10.497306
## Rogerian:Severe-Cognitive_Behavioral:Severe                   15.314085
##                                                                   p adj
## Cognitive_Behavioral:Mild-Assertiveness_Training:Mild         0.9935796
## Rogerian:Mild-Assertiveness_Training:Mild                     1.0000000
## Assertiveness_Training:Moderate-Assertiveness_Training:Mild   0.7047196
## Cognitive_Behavioral:Moderate-Assertiveness_Training:Mild     0.9999664
## Rogerian:Moderate-Assertiveness_Training:Mild                 0.2656175
## Assertiveness_Training:Severe-Assertiveness_Training:Mild     0.0028675
## Cognitive_Behavioral:Severe-Assertiveness_Training:Mild       0.1325902
## Rogerian:Severe-Assertiveness_Training:Mild                   0.0165147
## Rogerian:Mild-Cognitive_Behavioral:Mild                       0.9814135
## Assertiveness_Training:Moderate-Cognitive_Behavioral:Mild     0.3279936
## Cognitive_Behavioral:Moderate-Cognitive_Behavioral:Mild       0.9450576
## Rogerian:Moderate-Cognitive_Behavioral:Mild                   0.0919372
## Assertiveness_Training:Severe-Cognitive_Behavioral:Mild       0.0013422
## Cognitive_Behavioral:Severe-Cognitive_Behavioral:Mild         0.0447956
## Rogerian:Severe-Cognitive_Behavioral:Mild                     0.0060621
## Assertiveness_Training:Moderate-Rogerian:Mild                 0.8018175
## Cognitive_Behavioral:Moderate-Rogerian:Mild                   0.9999996
## Rogerian:Moderate-Rogerian:Mild                               0.3616114
## Assertiveness_Training:Severe-Rogerian:Mild                   0.0048679
## Cognitive_Behavioral:Severe-Rogerian:Mild                     0.1952294
## Rogerian:Severe-Rogerian:Mild                                 0.0255829
## Cognitive_Behavioral:Moderate-Assertiveness_Training:Moderate 0.9023328
## Rogerian:Moderate-Assertiveness_Training:Moderate             0.9998438
## Assertiveness_Training:Severe-Assertiveness_Training:Moderate 0.3846270
## Cognitive_Behavioral:Severe-Assertiveness_Training:Moderate   0.9965712
## Rogerian:Severe-Assertiveness_Training:Moderate               0.6353203
## Rogerian:Moderate-Cognitive_Behavioral:Moderate               0.5129491
## Assertiveness_Training:Severe-Cognitive_Behavioral:Moderate   0.0096887
## Cognitive_Behavioral:Severe-Cognitive_Behavioral:Moderate     0.3095077
## Rogerian:Severe-Cognitive_Behavioral:Moderate                 0.0448108
## Assertiveness_Training:Severe-Rogerian:Moderate               0.5749751
## Cognitive_Behavioral:Severe-Rogerian:Moderate                 0.9999980
## Rogerian:Severe-Rogerian:Moderate                             0.8296447
## Cognitive_Behavioral:Severe-Assertiveness_Training:Severe     0.7131529
## Rogerian:Severe-Assertiveness_Training:Severe                 0.9999998
## Rogerian:Severe-Cognitive_Behavioral:Severe                   0.9166266