One Factor Parametric Tests - R#

Independent-Samples t-test#

  • Samples: 1

  • Levels: 2

  • Between or Within Subjects: Between

  • Reporting: “The mean of ‘a’ was 14.63 (SD = 2.13) and of ‘b’ was 11.01 (SD = 1.75). This difference was statistically significant according to an independent-samples t-test (t(58) = 7.18, p < .0001).”

# Example data
# df has subjects (S), one between-Ss factor (X) w/levels (a,b), and continuous response (Y)
df <- read.csv("data/1F2LBs.csv")
head(df, 20)
A data.frame: 20 × 3
SXY
<int><chr><dbl>
1 1a13.211290
2 2b 9.376966
3 3a 9.832110
4 4b13.241823
5 5a15.290763
6 6b11.926719
7 7a16.513046
8 8b11.817017
9 9a14.405615
1010b 7.186540
1111a14.620504
1212b 9.200230
1313a13.619696
1414b 8.989190
1515a10.055274
1616b12.827598
1717a16.136434
1818b11.604144
1919a12.451788
2020b11.255572
df$S = factor(df$S) # Subject id is nominal (unused)
df$X = factor(df$X) # X is a 2-level factor
t.test(Y ~ X, data=df, var.equal=TRUE) # use var.equal=FALSE if heteroscedastistic
	Two Sample t-test

data:  Y by X
t = 7.1775, df = 58, p-value = 1.475e-09
alternative hypothesis: true difference in means between group a and group b is not equal to 0
95 percent confidence interval:
 2.609128 4.627288
sample estimates:
mean in group a mean in group b 
       14.62769        11.00948 

Paired-Samples t-test#

  • Samples: 1

  • Levels: 2

  • Between or Within Subjects: Within

  • Reporting: “The mean of ‘a’ was 13.15 (SD = 2.53) and of ‘b’ was 14.37 (SD = 2.16). This difference was statistically significant according to a paired-samples t-test (t(29) = -2.14, p < .05).”

# Example data
# df has subjects (S), one within-Ss factor (X) w/levels (a,b), and continuous response (Y)
df <- read.csv("data/1F2LWs.csv")
head(df, 20)
A data.frame: 20 × 3
SXY
<int><chr><dbl>
1 1a 9.348176
2 1b16.280812
3 2a12.797245
4 2b14.638421
5 3a11.757036
6 3b14.221410
7 4a13.037475
8 4b15.172092
9 5a14.934822
10 5b16.080187
11 6a10.762074
12 6b11.558915
13 7a14.699569
14 7b15.364241
15 8a12.476783
16 8b13.468385
17 9a12.761169
18 9b17.316534
1910a16.532902
2010b16.405975
library(reshape2) # for dcast
df$S = factor(df$S) # Subject id is nominal
df$X = factor(df$X) # X is a 2-level factor
df2 <- dcast(df, S ~ X, value.var="Y") # make wide-format table
t.test(df2$a, df2$b, paired=TRUE) # homoscedasticity is irrelevant for a paired-samples t-test
	Paired t-test

data:  df2$a and df2$b
t = -2.1363, df = 29, p-value = 0.04123
alternative hypothesis: true mean difference is not equal to 0
95 percent confidence interval:
 -2.39877224 -0.05222605
sample estimates:
mean difference 
      -1.225499 

One-Way ANOVA#

  • Samples: 1

  • Levels: ≥2

  • Between or Within Subjects: Between

  • Reporting: “The mean of ‘a’ was 13.74 (SD = 2.84), of ‘b’ was 14.15 (SD = 2.65), and of ‘c’ was 9.08 (SD = 4.29). These differences were statistically significant according to a one-way ANOVA (F(2, 57) = 14.18, p < .0001).”

# Example data
# df has subjects (S), one between-Ss factor (X) w/levels (a,b,c), and continuous response (Y)
df <- read.csv("data/1F3LBs.csv")
head(df, 20)
A data.frame: 20 × 3
SXY
<int><chr><dbl>
1 1a14.310439
2 2b17.390453
3 3c12.501365
4 4a17.943734
5 5b10.597671
6 6c 9.652177
7 7a10.095838
8 8b15.324131
9 9c 7.649627
1010a13.517695
1111b13.702848
1212c19.033070
1313a11.871676
1414b12.177908
1515c 7.713374
1616a11.698955
1717b13.716288
1818c 9.492661
1919a17.384638
2020b13.506336
df$S = factor(df$S) # Subject id is nominal (unused)
df$X = factor(df$X) # X is a 3-level factor
m = aov(Y ~ X, data=df) # fit model
anova(m)
A anova: 2 × 5
DfSum SqMean SqF valuePr(>F)
<int><dbl><dbl><dbl><dbl>
X 2316.8775158.4387314.180671.00343e-05
Residuals57636.8532 11.17286 NA NA

One-Way Repeated Measures ANOVA#

  • Samples: 1

  • Levels: ≥2

  • Between or Within Subjects: Within

  • Reporting: “The mean of ‘a’ was 14.04 (SD = 2.98), of ‘b’ was 11.95 (SD = 1.98), and of ‘c’ was 11.40 (SD = 2.75). Mauchly’s test of sphericity indicated no sphericity violation (W = .926, p = .499), allowing for an uncorrected repeated measures ANOVA, which showed statistically significant differences (F(2, 38) = 6.57, p < .01).”

# Example data
# df has subjects (S), one within-Ss factor (X) w/levels (a,b,c), and continuous response (Y)
df <- read.csv("data/1F3LWs.csv")
head(df, 20)
A data.frame: 20 × 3
SXY
<int><chr><dbl>
11a 8.833569
21b11.849632
31c 7.909041
42a15.361964
52b10.177478
62c13.245308
73a10.820555
83b 9.240199
93c14.307353
104a13.089309
114b13.276904
124c11.683161
135a12.229721
145b10.442200
155c 7.425539
166a15.471203
176b10.401901
186c14.725702
197a14.398177
207b10.755235
library(ez) # for ezANOVA

df$S = factor(df$S) # Subject id is nominal
df$X = factor(df$X) # X is a 3-level factor

m = ezANOVA(dv=Y, within=c(X), wid=S, type=3, data=df) # use c() for >1 factors

m$Mauchly # p<.05 indicates a sphericity violation
A data.frame: 1 × 4
EffectWpp<.05
<chr><dbl><dbl><chr>
2X0.92562720.4987981
m$ANOVA # use if no violation
A data.frame: 1 × 7
EffectDFnDFdFpp<.05ges
<chr><dbl><dbl><dbl><dbl><chr><dbl>
2X2386.5699370.003543683*0.1682495
# if there is a sphericity violation, report the Greenhouse-Geisser or Huynh-Feldt correction
p = match(m$Sphericity$Effect, m$ANOVA$Effect) # positions of within-Ss effects in m$ANOVA
m$Sphericity$GGe.DFn = m$Sphericity$GGe * m$ANOVA$DFn[p] # Greenhouse-Geisser DFs
m$Sphericity$GGe.DFd = m$Sphericity$GGe * m$ANOVA$DFd[p]
m$Sphericity$HFe.DFn = m$Sphericity$HFe * m$ANOVA$DFn[p] # Huynh-Feldt DFs
m$Sphericity$HFe.DFd = m$Sphericity$HFe * m$ANOVA$DFd[p]
m$Sphericity # show results
A data.frame: 1 × 11
EffectGGep[GG]p[GG]<.05HFep[HF]p[HF]<.05GGe.DFnGGe.DFdHFe.DFnHFe.DFd
<chr><dbl><dbl><chr><dbl><dbl><chr><dbl><dbl><dbl><dbl>
2X0.93077560.00446249*1.0278360.003543683*1.86155135.369472.05567239.05776
# the following also performs the equivalent repeated measures ANOVA, but does not address sphericity
m = aov(Y ~ X + Error(S/X), data=df)
summary(m)
Error: S
          Df Sum Sq Mean Sq F value Pr(>F)
Residuals 19  160.3   8.436               

Error: S:X
          Df Sum Sq Mean Sq F value  Pr(>F)   
X          2  78.12   39.06    6.57 0.00354 **
Residuals 38 225.93    5.95                   
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1