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 15

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 to use the data from Table 1, Chapter 15.

library(AMCP)
data(chapter_15_table_1)
chapter_15_table_1
##    Months30 Months36 Months42 Months48
## 1       108       96      110      122
## 2       103      117      127      133
## 3        96      107      106      107
## 4        84       85       92       99
## 5       118      125      125      116
## 6       110      107       96       91
## 7       129      128      123      128
## 8        90       84      101      113
## 9        84      104      100       88
## 10       96      100      103      105
## 11      105      114      105      112
## 12      113      117      132      130

Margin means

# Means by row (individual here)
apply(chapter_15_table_1, MARGIN=1, FUN=mean)
##  [1] 109 120 104  90 121 101 127  97  94 101 109 123
# Means by column (measurement occasion here)
apply(chapter_15_table_1, MARGIN=2, FUN=mean)
## Months30 Months36 Months42 Months48 
##      103      107      110      112

First, we need to convert the structure of the data from “wide” or “multivariate” to “long” or “univariate.” We find the easiest way to do this is using the tidyr and dplyr packages.

library(tidyr)
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
# Add an ID variable (by row number)
chapter_15_table_1$ID <- 1:nrow(chapter_15_table_1)

# Note that the "sep=-3" partitions between the 3rd and 2nd value from the end.
# The "-ID" here means to leave ID as it is (i.e., without "gathering" it)
data = chapter_15_table_1 %>%
gather(key=Unit.Month, value=Values, -ID) %>%
separate(col=Unit.Month, into=c("Unit", "Month"), sep=-3) %>%
arrange(ID, Month)

# Display the data
data
##    ID   Unit Month Values
## 1   1 Months    30    108
## 2   1 Months    36     96
## 3   1 Months    42    110
## 4   1 Months    48    122
## 5   2 Months    30    103
## 6   2 Months    36    117
## 7   2 Months    42    127
## 8   2 Months    48    133
## 9   3 Months    30     96
## 10  3 Months    36    107
## 11  3 Months    42    106
## 12  3 Months    48    107
## 13  4 Months    30     84
## 14  4 Months    36     85
## 15  4 Months    42     92
## 16  4 Months    48     99
## 17  5 Months    30    118
## 18  5 Months    36    125
## 19  5 Months    42    125
## 20  5 Months    48    116
## 21  6 Months    30    110
## 22  6 Months    36    107
## 23  6 Months    42     96
## 24  6 Months    48     91
## 25  7 Months    30    129
## 26  7 Months    36    128
## 27  7 Months    42    123
## 28  7 Months    48    128
## 29  8 Months    30     90
## 30  8 Months    36     84
## 31  8 Months    42    101
## 32  8 Months    48    113
## 33  9 Months    30     84
## 34  9 Months    36    104
## 35  9 Months    42    100
## 36  9 Months    48     88
## 37 10 Months    30     96
## 38 10 Months    36    100
## 39 10 Months    42    103
## 40 10 Months    48    105
## 41 11 Months    30    105
## 42 11 Months    36    114
## 43 11 Months    42    105
## 44 11 Months    48    112
## 45 12 Months    30    113
## 46 12 Months    36    117
## 47 12 Months    42    132
## 48 12 Months    48    130
#Note that we do not need "Unit" variable, so we drop it.  
data$Unit <- NULL

# Examining the structure of the data.
str(data)
## 'data.frame':    48 obs. of  3 variables:
##  $ ID    : int  1 1 1 1 2 2 2 2 3 3 ...
##  $ Month : chr  "30" "36" "42" "48" ...
##  $ Values: int  108 96 110 122 103 117 127 133 96 107 ...
# Examining the structure of the data we saw that "Month" is being treated as a character variable,
# yet we want it to be numeric:
data$Month <- as.numeric(data$Month)

str(data)
## 'data.frame':    48 obs. of  3 variables:
##  $ ID    : int  1 1 1 1 2 2 2 2 3 3 ...
##  $ Month : num  30 36 42 48 30 36 42 48 30 36 ...
##  $ Values: int  108 96 110 122 103 117 127 133 96 107 ...

There are multiple ways of producing trajectory plots. One convenient way is using the xyplot() function from the lattice. The lattice package provides a great deal of flexibility.

library(lattice)
# (each ID has it's own plot)
xyplot(Values ~ Month | ID, data=data, type="l", main="Trajectory Plot of McCarthy Scores", ylab="ID", xlab="Time in Months") 

Another way of plotting indivivdual trajectories is with ggplot2. The ggplot2 package provides a great deal of flexibility.

library(ggplot2)
Base.Plot <- ggplot(data = data, aes(x = Month, y = Values, group = ID))
Base.Plot + geom_line()