::p_load(tidyverse)
pacman
set.seed(1)
<- rnorm(n = 50, mean = 60, sd = 7)
A <- rnorm(n = 50, mean = 65, sd = 7)
B <- rnorm(n = 50, mean = 70, sd = 7) C
49 Sphericity: Variances and Co-Variances
In this walkthrough I’m just going to be building a few intuitions about what we mean by sphericity and how it relates to our homogeneity of variance assumptions.
49.1 Between Subjects Example
For this example, let’s assume that we have three groups of participants that we are comparing against one another. Let’s generate the data for these groups—A, B, & C.Each group has the same number of members and same variances (sds), but different means:
49.1.1 The correlation matrix
49.1.1.1 Auto-correlation
Auto correlation simply means the correlation of data with itself. In practical circumstances there is an additional wrinkle to auto-correlation like introducing a “lag” in the data, but for our purposes we are just going to correlate the original data from each group against itself. This will always result in a correlation of 1:
<- cor(A,A)
aa <- cor(B,B)
bb <- cor(C,C)
cc
c("correlation A" = aa,
"correlation B" = bb,
"correlation C" = cc)
correlation A correlation B correlation C
1 1 1
49.1.1.2 across groups correlations
Now we need to test for the correlations of each group to the other groups. Notice that when we generated our data in Chunk 1, each group was randomly generated, independent of one another. This captures these two critical assumptions of Between Subjects ANOVA. This also means that there should be no correlations (inter-dependencies) between our groups. While theoretically this means that any between group correlations should equal zero, in practice we should get a very low number. (Keep in mind your values may be different from mine due to random generation, unless you set the seed as I did to 1):
<- cor(A,B)
ab <- cor(A,C)
ac <- cor(B,C)
bc
c("correlation AB" = ab, "correlation AC" = ac, "correlation BC" = bc)
correlation AB correlation AC correlation BC
-0.039087178 0.026287227 0.003552006
From here, we can build our correlation matrix by first assigning the mirrors to our correlations above (e.g., the correlation of A to C is the same as C to A).
<- ab # mirrors
ba <- ac
ca <- bc cb
And building the matrix
<- matrix(c(aa, ab, ac,
correlation_matrix
ba, bb, bc,
ca, cb, cc),ncol = 3)
rownames(correlation_matrix) = c("A", "B", "C")
colnames(correlation_matrix) = c("A", "B", "C")
%>% round(3) correlation_matrix
A B C
A 1.000 -0.039 0.026
B -0.039 1.000 0.004
C 0.026 0.004 1.000
49.2 The covariance matrix
We can translate the correlation matrix to a covariance matrix by cross multiplying by our group standard deviations. (Or, since we had the original data, I suppose we could have just taken the cov()
of each column)
<- c(sd(A), sd(B), sd(C))
stdevs
# need to transpose for matrix multiplication
<- correlation_matrix * stdevs %*% t(stdevs)
covariance_matrix
covariance_matrix
A B C
A 33.8695775 -1.5427086 0.9635283
B -1.5427086 45.9927476 0.1517166
C 0.9635283 0.1517166 39.6670215
The important thing to note here, that our simulations reinforce is that for the sake of the Between Subjects ANOVAs all values off of the diagonal are effectively zero. Therefore the Between Subjects ANOVA is only concerned with the relationships (equivalence) of values along the diagonal.
Let kick this up to within subjects (repeated measures) designs.
49.3 Within Subjects example
Lets start by generating a Group-Condition, A
. We’ll create the same values as before
set.seed(1)
<- rnorm(n = 50, mean = 60, sd = 7) A
However, this time, instead of generating B
and C
independently, we will psuedo-randomly generate these scores based on A
: where B
is 5 greater than A
plus or minus some random noise, and C
is 5 greater than B
plus or minus noise.
<- A + 5 + runif(50) - runif(50)
B <- B + 5 + runif(50) - runif(50) C
Now lets run our auto-correlations and between conditions correlations:
#auto correlations all equal 1
<- cor(A,A)
aa <- cor(B,B)
bb <- cor(C,C)
cc
# cross condition correlations
<- cor(A,B)
ab <- cor(A,C)
ac <- ab # mirrors
ba <- cor(B,C)
bc <- ac
ca <- bc cb
and finally build the correlation matrix:
<- matrix(c(aa, ab, ac,
correlation_matrix
ba, bb, bc,
ca, cb, cc),ncol = 3)
rownames(correlation_matrix) = c("A", "B", "C")
colnames(correlation_matrix) = c("A", "B", "C")
%>% round(3) correlation_matrix
A B C
A 1.000 0.998 0.996
B 0.998 1.000 0.999
C 0.996 0.999 1.000
As we see in this case, scores in each condition are very strongly correlated with one another (the off-diagonal correlations). We can then take a look at the covariance matrix by cross multiplying by our group standard deviations. (Or, again, since we had the original data, I suppose we could have just taken the cov()
of each column)
<- c(sd(A), sd(B), sd(C))
stdevs
# need to transpose for matrix multiplication
<- correlation_matrix * stdevs %*% t(stdevs)
covariance_matrix
covariance_matrix
A B C
A 33.86958 34.31762 34.24638
B 34.31762 34.89534 34.84546
C 34.24638 34.84546 34.89718
The covariance matrix above represents a case on near-compound-symmetry. This would be as close to the perfect sphericity as we would expect given generated data. I really wouldn’t expect these clean of values ever from my empirical data.
49.4 What all of this means
Hopefully, the above example captures how we can use the variance / co-variance matrix to assess how strongly the data in our conditions (and groups) is correlated with one another. For example, I can easily transform back and forth between covariances and correlations using the standard deviations of my groups. For example, going from covariance to correlation:
/ stdevs %*% t(stdevs) covariance_matrix
A B C
A 1.0000000 0.9982252 0.9961268
B 0.9982252 1.0000000 0.9985442
C 0.9961268 0.9985442 1.0000000
So, our assessment of sphericity is an assement of the degree to data in each condition’s data is correlated with one another, where the ideal scenario is that data in each group is perfectly correlated with the other groups (note this does not mean that the data is indentical, just strongly correlated).
49.5 Sphericity demonstration calculator
With this in mind we can build a calculator example to better understand how devations in correlations result in deviations away from the sphericity assumption. Here we can start with our correlations, use those values to generate hypothetical data, and work our way back. In the code below, you can alter the group means mu
and standard deviations stddev
, as well as the between condition correlations.
49.5.1 Build correlation matrix
library(MASS)
Attaching package: 'MASS'
The following object is masked from 'package:dplyr':
select
library(tidyverse)
set.seed(5)
# YOU CAN CHANGE ME
<- c(4.23, 3.01, 2.91) # group means
mu <- c(2, 2, 2) # group standard devs
stddev
# correlations (variances, DO NOT CHANGE)
= 1
aa = 1
bb = 1
cc
# correlations (covariances, a to b, a to c, b to c, CHANGE)
= .3
ab = .6
ac = .1 bc
Based upon your changes you can not build the appropriate correlation matrix
# DO NOT ALTER
= ab #mirror
ba = ac
ca = bc
cb
<- matrix(c(aa, ab, ac,
corMat
ba, bb, bc,
ca, cb, cc),ncol = 3)
corMat
[,1] [,2] [,3]
[1,] 1.0 0.3 0.6
[2,] 0.3 1.0 0.1
[3,] 0.6 0.1 1.0
49.5.2 build a covariance matrix
Now to create the covariance matrix
<- stddev %*% t(stddev) * corMat
covMat covMat
[,1] [,2] [,3]
[1,] 4.0 1.2 2.4
[2,] 1.2 4.0 0.4
[3,] 2.4 0.4 4.0
49.5.3 build simulated data
you can use this code to build out simulated data based on the parameters you have entered above:
<- mvrnorm(n = 212, # number of participants
df mu = mu, # means from above
Sigma = covMat, # covariance matrix from above
empirical = TRUE) %>%
data.frame()
names(df) <- c("A","B","C")
df
A B C
1 4.277062678 3.90719459 4.45579227
2 4.641274608 1.49615783 1.42113739
3 3.827340659 3.83714702 5.24109404
4 5.851299251 5.04517587 2.13024154
5 4.246887804 0.05766786 1.90040053
6 4.979052424 2.92928818 6.02874995
7 6.093413904 5.03705424 4.14889026
8 6.957267914 6.01743282 4.43155681
9 4.127715697 3.16528570 3.57542196
10 3.995481497 2.58402796 2.89951762
11 5.159923190 -1.11939545 6.27617987
12 2.835283218 3.66516386 2.74910499
13 -0.005171068 3.08004143 0.64309191
14 1.604748598 0.93847074 2.93725316
15 0.104871288 3.46868401 0.21707385
16 2.978828416 2.71749637 2.21460297
17 3.511566669 4.64110544 1.64587304
18 0.969214756 8.69093535 -2.51418694
19 4.006297746 2.87981840 2.17936103
20 4.286101350 2.22302861 5.02910647
21 3.095643825 1.01454567 1.53614891
22 3.565675803 1.48623563 1.37257349
23 2.526985556 -0.62642339 1.32087133
24 7.546741165 2.33181844 6.23095835
25 3.903580984 -0.71824722 5.30337738
26 3.309238135 3.69308917 1.76454027
27 3.941415739 0.52761729 1.74132239
28 7.374147523 1.68879043 4.44490095
29 4.548794063 4.13414632 3.92912026
30 3.843303195 4.39002312 3.23504463
31 4.665123667 2.93900057 2.74286733
32 6.816769693 1.45988113 5.23032884
33 5.554110070 2.21077803 -0.94466206
34 3.177194088 0.67750507 1.13568517
35 7.328580371 2.64875995 3.09567325
36 4.031199119 1.31165763 2.20948641
37 2.946212473 5.23347820 1.33831893
38 5.638877551 7.53906151 4.77114360
39 2.524047840 4.99203285 3.45085161
40 5.952927868 5.31870294 2.54216798
41 2.644049929 -1.61338551 2.60930780
42 2.675571255 3.54267916 2.70994174
43 0.689786180 2.24912497 -0.38416886
44 5.546689126 2.88443793 -0.90832427
45 2.559404085 3.05165811 2.17096064
46 4.916914595 3.80676397 1.09483360
47 3.007383173 2.42163012 4.99117018
48 4.484086905 4.16310585 3.19081765
49 6.437561152 8.48952865 0.53957314
50 4.798749088 3.57288866 3.22632821
51 6.741865472 1.39823211 4.11913155
52 2.041130755 1.36884085 1.84317573
53 4.914176044 1.68598773 2.64389017
54 6.403865046 3.22546950 7.48096292
55 4.583086014 2.48682853 4.59807826
56 2.366962683 3.66978645 2.49962789
57 2.616965642 2.24802193 -0.37949205
58 3.761107399 3.77498769 1.69129705
59 5.191838186 4.55744819 2.35175825
60 3.079915252 2.82050020 1.04896171
61 5.888754446 7.73430658 2.14805398
62 7.115827043 2.22991475 5.33892244
63 7.488374891 4.71283481 6.79726047
64 5.934307128 3.74617467 2.74320888
65 1.226151281 3.35782120 0.90249027
66 3.893422498 3.12289455 3.49477790
67 4.858224567 7.13468474 4.61313986
68 4.698820470 4.20263249 2.92976471
69 4.842739506 5.80489466 3.88425063
70 4.384869661 4.74484519 1.68006882
71 0.724852026 1.38955548 1.27280809
72 5.466053126 3.31746769 4.96210063
73 5.539570315 4.22359237 2.00879115
74 1.525839944 1.47877832 1.48290147
75 6.564609727 2.60421403 5.44700856
76 7.540720669 6.49204720 3.05327059
77 7.330073078 1.69561488 6.00749110
78 3.288495378 2.82239924 1.67319019
79 2.465953025 1.26157259 2.55677739
80 4.955757395 3.83721184 4.59742601
81 7.114691053 4.17457903 3.71117308
82 4.165306104 6.08176310 4.04956514
83 2.489275635 -0.54667597 2.68746169
84 1.991995396 1.90605485 1.68026325
85 6.685800702 2.82682678 4.48201578
86 4.080930455 2.85201370 5.27139543
87 4.658162092 -1.24823958 2.18653743
88 6.366383739 5.59593453 3.73195364
89 4.408515004 1.68368996 5.38051089
90 5.935792738 5.38396806 3.60509507
91 6.361088298 0.68857606 6.27594523
92 3.502997064 4.09979322 3.83930421
93 1.742894734 1.73502994 -0.21645273
94 4.437940905 2.63980610 1.04405939
95 7.647354334 2.82269152 4.79618384
96 2.890270047 1.51172167 2.16817978
97 4.715529686 4.35682082 2.87475186
98 5.885987498 4.86234182 4.32861724
99 -1.590478407 -1.09971579 1.61207753
100 6.553873310 3.69122536 5.37320979
101 1.918659612 3.43882916 5.54923542
102 4.579194887 2.73765586 0.36680869
103 4.052586532 1.50417752 2.82379931
104 6.618348581 2.75663495 5.94125595
105 8.094050768 5.18835527 5.32161960
106 3.961341824 1.62088755 1.86097402
107 -0.100848882 1.09591631 0.65965677
108 2.669225212 4.65964963 4.74361915
109 3.068611435 3.79168696 2.73302864
110 6.736171603 1.19701224 4.96283307
111 5.687754926 5.80268835 4.02387176
112 5.513496066 0.28466870 3.54947645
113 5.572249747 0.77714155 -0.10014258
114 1.733975553 1.86674136 0.88253509
115 4.070846031 4.50797576 4.91923864
116 1.719285522 -0.57555341 2.24807024
117 3.181364809 6.75887872 1.20234405
118 3.061936122 2.06096413 1.37681613
119 3.735262445 3.98179630 0.85369327
120 2.777023228 2.28939315 2.74248268
121 3.177633288 0.01405719 2.21224057
122 3.759727726 2.04134204 1.02867964
123 5.621811340 6.16973910 4.55326269
124 3.162682761 6.22359498 2.20368518
125 1.495419998 0.22749135 0.01224626
126 2.910962828 1.11037135 3.05024568
127 7.155604423 5.96300673 4.04218792
128 5.235852596 1.29471411 2.93691950
129 -1.039288431 4.39288460 -1.69485987
130 -2.636641009 2.23463377 -0.56215565
131 2.920555500 1.61447124 -0.93195323
132 4.338234264 5.67143001 5.59065801
133 2.211628570 4.79637589 1.69012464
134 3.963014235 4.15356977 1.40229644
135 6.811097035 4.70724849 1.82883493
136 2.153369868 1.89678588 0.41912203
137 4.578847704 2.63597591 3.13992896
138 5.899353521 5.01470267 5.78889325
139 4.301508346 4.76790374 -1.34641167
140 4.891574666 2.75154904 3.36074262
141 4.607337631 3.57348446 5.22655528
142 2.861177851 2.59722616 1.88608748
143 3.228850162 -1.97992565 1.94869896
144 4.047054265 0.35846755 3.55206849
145 7.991273330 4.76406833 6.54820328
146 2.854297065 5.07641001 -0.39918021
147 4.024171978 2.36517097 3.36387222
148 3.834959195 2.68843119 3.68081879
149 3.565449325 6.14544180 2.14334857
150 10.688234597 7.23269190 6.95257620
151 6.923292220 3.28840604 3.31089347
152 7.029896676 2.88571454 4.62254573
153 4.713844901 5.27697059 2.10969860
154 5.566531794 1.34982026 4.65104416
155 4.454156578 5.35119775 1.96025240
156 5.403492577 2.10472226 2.03748430
157 5.966224188 4.64794615 4.10956116
158 6.156569509 2.93978422 1.99974788
159 2.842394834 2.37827631 3.45407983
160 6.663340690 3.66086927 4.80004140
161 0.633401685 6.95937648 0.82884060
162 5.912348389 -0.23307545 2.87259005
163 3.137054659 2.49275246 2.02008801
164 6.298191436 0.35241044 4.48701655
165 3.070904489 3.54745178 2.32606343
166 4.496609150 3.01485493 5.43954936
167 5.447971819 3.60431561 3.95908532
168 1.912932672 1.32998677 2.67801784
169 6.811280570 2.66941277 7.02987690
170 5.374025770 3.40827075 5.51909968
171 5.185190160 5.39666199 5.43621164
172 4.462388665 1.79474697 4.61470103
173 6.332813675 5.08785560 6.76456048
174 5.348235164 5.38288086 6.64319561
175 5.501143713 3.30213108 4.44019326
176 3.744667973 2.87792429 1.54938298
177 2.293072024 -0.69028542 0.77429111
178 2.117169544 3.31822499 3.10882611
179 1.979164023 -0.30049849 1.84419768
180 7.317472314 6.84005805 3.63116400
181 4.345408665 0.08494558 0.53363204
182 5.210681323 4.24477246 4.74047766
183 4.750237470 1.68813633 3.26638967
184 -0.171006222 0.64899783 -2.54210016
185 2.814608215 4.80567279 2.55537546
186 5.563067719 4.13854408 4.09863588
187 3.763718560 4.99995695 0.21172177
188 1.220724887 1.31075671 2.60446568
189 5.373438916 3.39646036 1.72730598
190 5.753128372 6.02352237 4.46976059
191 4.533720364 4.09091974 -0.14326423
192 3.916882338 3.28931152 1.09503713
193 4.681860230 3.78965022 4.14961571
194 1.843652778 0.84803951 1.49521177
195 3.115964156 2.80808528 2.42176079
196 9.287889591 2.69526989 6.08213853
197 5.318852446 3.31253505 1.53751168
198 1.749218662 3.17095635 -0.75159785
199 3.234556324 1.85368972 5.35946551
200 3.141401684 1.77327028 1.07158954
201 1.206393970 1.69343124 1.20810150
202 1.482106323 -0.01184140 4.53612541
203 6.242192992 3.18006831 2.41977491
204 5.712270352 2.83039353 7.68170170
205 5.398089473 4.26064970 1.98862815
206 2.910493463 0.71633051 3.22860748
207 5.047575310 2.63646436 2.76197619
208 2.954976966 2.49189066 -0.83984593
209 3.579038340 3.16974229 3.09029601
210 5.659629276 0.57006782 6.08220828
211 2.344912445 -1.67070231 3.31976643
212 2.434378236 1.74160205 0.47390910
49.5.4 Analyze for Homogeneity of Sums minus Covariances:
And finally check for the similarity of the Sums minus Covariances of each pairwise comparison. Note how the resulting values deviate from one another as you change the variances of each group and the correlations between groups from the start.
<- var(df$A)
varA <- var(df$B)
varB <- var(df$C)
varC
<- cov(df$A, df$B)
covAB <- cov(df$A, df$C)
covAC <- cov(df$B, df$C)
covBC
<- varA + varB - 2*covAB
AB <- varA + varC - 2*covAC
AC <- varB + varC - 2*covBC
BC
list("A to B" = AB, "A to C" = AC,"B to C" = BC)
$`A to B`
[1] 5.6
$`A to C`
[1] 3.2
$`B to C`
[1] 7.2