One Between-Subjects Variable and One Within-Subjects Variable

Often psycologists would test participants in an experimental design consisting of at least one between-subjects variable and one within-subjects variable. For example, King (1986) wanted to know whether the acquired tolerance could be explained on the basis of a conditioned tolerance related to the physical context in which the drug was administered. Three groups of rats were tested. The control group was injected with physiological saline. The “Same” group was injected with the drug in the same environment. The “Different” group was injected with the drug in a different environment. Because the drug is known to be metabolized over a period of approximately 1 hour, King recored his data in 5-min intervals. The below shows the data. Here “Con” is the between-subjects factor and “Int” is the within-subjects factor.

dta<-data.frame(s=factor(c(rep(1:8,6),rep(9:16,6),rep(17:24,6))),
                Int=gl(6,8,144),
                Con=gl(3,48,144),
                y=c(150,335,149,159,159,292,297,170,
                    44,270,52,31,0,125,187,37,
                    71,156,91,127,35,184,66,42,
                    59,160,115,212,75,246,96,66,
                    132,118,43,71,71,225,209,114,
                    74,230,154,224,34,170,74,81,
                    346,426,359,272,200,366,371,497,
                    175,329,238,60,271,291,364,402,
                    177,236,183,82,263,263,270,294,
                    192,76,123,85,216,144,308,216,
                    239,102,183,101,241,220,219,284,
                    140,232,30,98,227,180,267,255,
                    282,317,362,338,263,138,329,292,
                    186,31,104,132,94,38,62,139,
                    225,85,144,91,141,16,62,104,
                    134,120,114,77,142,95,6,184,
                    189,131,115,108,120,39,93,193,
                    169,205,127,169,195,55,67,122))

Let’s make a plot for checking the pattern of group means. The visual inspection suggests that both Control group and Different group have a similar pattern through the experiment. This result seems to be consistent with King’s hypothesis.

means<-with(dta,tapply(y,list(Con,Int),mean))
ses<-with(dta,tapply(y,list(Con,Int),sd))/sqrt(8)
fg.dta<-data.frame(mean=c(means),ses=c(ses),Interval=rep(1:6,each=3),
                   Condition=c("Control","Same","Different"))
library(ggplot2)
ggplot(fg.dta,aes(Interval,mean,group=Condition))+
  geom_point(aes(color=Condition))+
  geom_line(aes(color=Condition))+
  geom_errorbar(aes(ymin=mean-ses,ymax=mean+ses,color=Condition),width=0.2,
                position=position_dodge(width=0.05))

As now we have one between-subjects variable (“Con”) and one within-subjects variable (“Int”), we should tell R which variable is which. By setting up the error term as Error(s/Int), the interaction of subject and injection interval is included in the error term for testing any effect involving “Int”.

overall.aov<-aov(y~Con*Int+Error(s/Int),dta)
summary(overall.aov)
## 
## Error: s
##           Df Sum Sq Mean Sq F value  Pr(>F)   
## Con        2 285815  142908   7.801 0.00293 **
## Residuals 21 384722   18320                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Error: s:Int
##            Df Sum Sq Mean Sq F value  Pr(>F)    
## Int         5 399737   79947  29.852 < 2e-16 ***
## Con:Int    10  80820    8082   3.018 0.00216 ** 
## Residuals 105 281199    2678                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

The results of ANOVA are divided into two parts; one for between-subjects variable and the other for the rest. For testing the between-subjects effect of “Con”, the error term is the within-cell error and the degrees of freedom of the error term is \(p(n-1)\). Here, there are 8 subjects in each cell and 3 groups, hence \(3\times (8-1)=21\). For testing the two within-subjects effects, the error term includes the interaction of subject and the degrees of freedom is \(p(q-1)(n-1)\). Thus, the degrees of freedom is \(3\times5\times7=105\).

Partition of The Total Variations

In this experimental design, the total variations first are partitioned to the between-subjects part and the within-subjects part. The between-subjects part includes the variations accounted for by the between-subjects variable and what are regarded as the within-cell error. The within-subjects part includes the variations accounted for by the within-subjects variable and the interaction effect between the within- and between-subjects variables. The rest are the residuals.

\(SST=SS_{between}+SS_{within}=SS_{A}+SS_{within}+SS_{B}+SS_{A\times B}+SS_{residuals}\)

\(df_{total}=df_{A}+df_{within}+df_{B}+df_{A\times B}+df_{residual}\)

\(pqn-1=p-1+p(n-1)+q-1+(p-1)(q-1)+p(q-1)(n-1)\)

Simple Effects

As the interaction effect is significant, we can test the simple effects of the two factors. First, we focus on the simple effect of “Int” on different groups. This can be done by first separating the data set to three smaller data sets according to the between-subjects variable “Con”. Thereafter, we can run a one-way within-subjects ANOVA for each of these data sets. Note we always tread the subject variable as a random-effect variable. The results reveal that in no matter which group, the drug tolerance is significant.

# Simple effect of Int at Control group
s.int1.aov<-aov(y~Int+Error(s/Int),subset(dta,Con==1))
summary(s.int1.aov)
## 
## Error: s
##           Df Sum Sq Mean Sq F value Pr(>F)
## Residuals  7 134616   19231               
## 
## Error: s:Int
##           Df Sum Sq Mean Sq F value   Pr(>F)    
## Int        5  76447   15289   5.693 0.000606 ***
## Residuals 35  93998    2686                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Simple effect of Int at Same group
s.int2.aov<-aov(y~Int+Error(s/Int),subset(dta,Con==2))
summary(s.int2.aov)
## 
## Error: s
##           Df Sum Sq Mean Sq F value Pr(>F)
## Residuals  7 175600   25086               
## 
## Error: s:Int
##           Df Sum Sq Mean Sq F value   Pr(>F)    
## Int        5 193091   38618    11.1 1.85e-06 ***
## Residuals 35 121715    3478                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Simple effect of Int at Different group
s.int3.aov<-aov(y~Int+Error(s/Int),subset(dta,Con==3))
summary(s.int3.aov)
## 
## Error: s
##           Df Sum Sq Mean Sq F value Pr(>F)
## Residuals  7  74506   10644               
## 
## Error: s:Int
##           Df Sum Sq Mean Sq F value   Pr(>F)    
## Int        5 211018   42204   22.56 4.71e-10 ***
## Residuals 35  65486    1871                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Of our great interest is whether the Different group performed like the Control group. If the drug tolerance results from the same conditioning mechanism as for morphine, these two groups should perform quite similarly. We can verify this hypothesis with contrasts.

dta$Conc1<-rep(c(1,0,-1),each=48)
dta$Conc2<-rep(c(1,-2,1),each=48)
Conc.aov<-aov(y~Conc1*Conc2*Int+Error(s/Int),dta)
summary(Conc.aov)
## 
## Error: s
##           Df Sum Sq Mean Sq F value  Pr(>F)    
## Conc1      1   4565    4565   0.249 0.62284    
## Conc2      1 281250  281250  15.352 0.00079 ***
## Residuals 21 384722   18320                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Error: s:Int
##            Df Sum Sq Mean Sq F value  Pr(>F)    
## Int         5 399737   79947  29.852 < 2e-16 ***
## Conc1:Int   5  21198    4240   1.583 0.17121    
## Conc2:Int   5  59622   11924   4.453 0.00102 ** 
## Residuals 105 281199    2678                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Apparently, across all injection intervals, the Control group has no difference from the Different group. Also, the difference between the Control group and the Different group would not differ in different intervals. The Same group significantly differs from the other two Groups acorss all intervals. The difference between the Same group and the other two groups is affected by the injection intervals.

Another Numeric Example

Let’s see another numerical example of the design with one between- and one within-subjects variables. In the below data set, factor a is a between-subjects variable with 2 levels. Factor b is a within-subjects variable with 4 levels. In each condition, there are 4 subjects.

dta<-data.frame(a=gl(2,16,32,labels=1:2),
                b=gl(4,4,32,labels=1:4),
                s=as.factor(c(rep(1:4,4),rep(5:8,4))),
                y=c(3,6,3,3, 4,5,4,3, 7,8,7,6, 7,8,9,8,
                    1,2,2,2, 2,3,4,3, 5,6,5,6, 10,10,9,11))

The pattern of group means is shown in the following figure.

means<-with(dta,tapply(y,list(a,b),mean))
ses<-with(dta,tapply(y,list(a,b),sd))/sqrt(4)
fg.dta<-data.frame(mean=c(means),ses=c(ses),
                   b=rep(1:4,each=2),a=as.factor(1:2))
ggplot(fg.dta,aes(b,mean,group=a))+
  geom_point(aes(color=a))+
  geom_line(aes(color=a))+
  geom_errorbar(aes(ymin=mean-ses,ymax=mean+ses,
                    color=a),width=0.1)

The omnibus F test shows that Factor a has no significant main effect, \(F(1,6)=2\), \(MSe=1.56\), \(p=.21\), the main effect of Factor b is significant [\(F(3,18)=127.89\), \(MSe=0.51\), \(p<.01\)], and the interaction effect between Factor a and b is also significant [\(F(3,18)=12.74\), \(MSe=0.51\), \(p<.01\)].

overall.aov<-aov(y~a*b+Error(s/b),dta)
summary(overall.aov)
## 
## Error: s
##           Df Sum Sq Mean Sq F value Pr(>F)
## a          1  3.125   3.125       2  0.207
## Residuals  6  9.375   1.562               
## 
## Error: s:b
##           Df Sum Sq Mean Sq F value   Pr(>F)    
## b          3 194.50   64.83  127.89 2.52e-12 ***
## a:b        3  19.37    6.46   12.74 0.000105 ***
## Residuals 18   9.12    0.51                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Since the interaction effect is significant, we need to figure out the reason for it. Although we can test the simple effect for both a and b at each level of the other, that would be too time consuming. Suggested by the visual inspection, the superiority of a1 over a2 is flipped at b4. Perhaps, b4 is the critical point for the interaction effect. Thus, we can test this finding with contrasts.

dta$bc1<-rep(rep(c(1,1,1,-3),each=4),2)
dta$bc2<-rep(rep(c(0,1,-1,0),each=4),2)
dta$bc3<-rep(rep(c(-2,1,1,0),each=4),2)
bc.aov<-aov(y~a*(bc1+bc2+bc3)+Error(s/(bc1+bc2+bc3)),dta)
summary(bc.aov)
## 
## Error: s
##           Df Sum Sq Mean Sq F value Pr(>F)
## a          1  3.125   3.125       2  0.207
## Residuals  6  9.375   1.562               
## 
## Error: s:bc1
##           Df Sum Sq Mean Sq F value   Pr(>F)    
## bc1        1 140.17  140.17  145.21 1.98e-05 ***
## a:bc1      1  18.38   18.38   19.04  0.00475 ** 
## Residuals  6   5.79    0.97                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Error: s:bc2
##           Df Sum Sq Mean Sq F value   Pr(>F)    
## bc2        1  30.25   30.25     121 3.36e-05 ***
## a:bc2      1   0.25    0.25       1    0.356    
## Residuals  6   1.50    0.25                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Error: s:bc3
##           Df Sum Sq Mean Sq F value   Pr(>F)    
## bc3        1 24.083  24.083  78.818 0.000114 ***
## a:bc3      1  0.750   0.750   2.455 0.168227    
## Residuals  6  1.833   0.306                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Apparently, the contrast between b4 and the average of b1, b2, and b3 is significant, suggesting that across the a levels, the score in b4 is different from the mean of the rest. More importantly, the interaction effect between this contrast and Factor a is significant. However, the other contrasts which exclude b4 do not interact with Factor a. Therefore, b4 is the keypoint of the significant interaction effect.