# Siegel-Tukey: a Non-parametric test for equality in variability (R code)

February 22, 2010
By

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Daniel Malter just shared on the R mailing list (link to the thread) his code for performing the Siegel-Tukey (Nonparametric) test for equality in variability.
Excited about the find, I contacted Daniel asking if I could republish his code here, and he kindly replied “yes”.
From here on I copy his note at full.

Corrections and remarks can be added in the comments bellow, or on the github code page.

* * * *

Hi, I recently ran into the problem that I needed a Siegel-Tukey test for equal variability based on ranks. Maybe there is a package that has it implemented, but I could not find it. So I programmed an R function to do it. The Siegel-Tukey test requires to recode the ranks so that they express variability rather than ascending order. This is essentially what the code further below does. After the rank transformation, a regular Mann-Whitney U test is applied. The “manual” and code are pasted below.

Description:  Non-parametric Siegel-Tukey test for equality in variability. The null hypothesis is that the variability of x is equal between two groups. A rejection of the null indicates that variability differs between
the two groups.

Usage:

 ```1 2 3 4 5 ``` ```# Loading the function source("http://www.r-statistics.com/wp-content/uploads/2012/01/source_https.r.txt") # Making sure we can source code from github source_https("https://raw.github.com/talgalili/R-code-snippets/master/siegel.tukey.r") # Using the function siegel.tukey(x,y,id.col=FALSE,adjust.median=FALSE,rnd=8, ...)```

Arguments:

x: a vector of data

y: Data of the second group (if id.col=FALSE) or group indicator (if id.col=TRUE). In the latter case, y MUST take 1 or 0 to indicate observations of group 1 and 0, respectively, and x must contain the data for both groups.

id.col: If FALSE (default), then x and y are the data vectors (columns) for group 1 and 0, respectively. If TRUE, the y is the group indicator.

adjust.median: Should between-group differences in medians be leveled before performing the test? In certain cases, the Siegel-Tukey test is susceptible to median differences and may indicate significant differences in variability that, in reality, stem from differences in medians.

rnd: Should the data be rounded and, if so, to which decimal? The default (-1) uses the data as is. Otherwise, rnd must be a non-negative integer. Typically, this option is not needed. However, occasionally, differences in
the precision with which certain functions return values cause the merging of two data frames to fail within the siegel.tukey function. Only then  rounding is necessary. This operation should not be performed if it affects
the ranks of observations.

… arguments passed on to the Wilcoxon test. See ?wilcox.test

Value: Among other output, the function returns rank sums for the two groups, the associated Wilcoxon’s W, and the p-value for a Wilcoxon test on tie-adjusted Siegel-Tukey ranks (i.e., it performs and returns a
Siegel-Tukey test). If significant, the group with the smaller rank sum has greater variability.

References: Sidney Siegel and John Wilder Tukey (1960) “A nonparametric sum of ranks procedure for relative spread in unpaired samples.” Journal of the
American Statistical Association. See also, David J. Sheskin (2004) “Handbook of parametric and nonparametric statistical procedures.” 3rd
edition. Chapman and Hall/CRC. Boca Raton, FL.

Notes: The Siegel-Tukey test has relatively low power and may, under certain conditions, indicate significance due to differences in medians rather than
differences in variabilities (consider using the argument adjust.median).

Output (in this order)

1. Group medians
2. Wilcoxon-test for between-group differences in median (after the median
3. Unique values of x and their tie-adjusted Siegel-Tukey ranks
4. Xs of group 0 and their tie-adjusted Siegel-Tukey ranks
5. Xs of group 1 and their tie-adjusted Siegel-Tukey ranks
6. Siegel-Tukey test (Wilcoxon test on tie-adjusted Siegel-Tukey ranks)

### The R code:

Update: The R function was moved to github, and corrected from a few mistakes found by some of the sharp readers of this blog. The R function can be downloaded from here

Here is an example of its usage, and output:

 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 ``` ```  ###################### # Loading the functions ######################   source("http://www.r-statistics.com/wp-content/uploads/2012/01/source_https.r.txt") # Making sure we can source code from github source_https("https://raw.github.com/talgalili/R-code-snippets/master/siegel.tukey.r")   ###################### # Examples: ######################   ### 1 x=c(4,4,5,5,6,6) y=c(0,0,1,9,10,10) siegel.tukey(x,y, F) siegel.tukey(x,y) #same as above   ### 2 # example for a non equal number of cases: x=c(4,4,5,5,6,6) y=c(0,0,1,9,10) siegel.tukey(x,y,F)   ### 3 x <- c(33, 62, 84, 85, 88, 93, 97, 4, 16, 48, 51, 66, 98) id <- c(0,0,0,0,0,0,0,1,1,1,1,1,1) siegel.tukey(x,id,T) siegel.tukey(x~id) # from now on, this also works as a function... siegel.tukey(x,id,T,adjust.median=F,exact=T)   ### 4 x<-c(177,200,227,230,232,268,272,297,47,105,126,142,158,172,197,220,225,230,262,270) id<-c(rep(0,8),rep(1,12)) siegel.tukey(x,id,T,adjust.median=T)     ### 5 x=c(33,62,84,85,88,93,97) y=c(4,16,48,51,66,98) siegel.tukey(x,y)   ### 6 x<-c(0,0,1,4,4,5,5,6,6,9,10,10) id<-c(0,0,0,1,1,1,1,1,1,0,0,0) siegel.tukey(x,id,T)   ### 7 x <- c(85,106,96, 105, 104, 108, 86) id<-c(0,0,1,1,1,1,1) siegel.tukey(x,id,T)```

Here is the code’s output:

 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 ``` ```  > > ### 1 > x=c(4,4,5,5,6,6) > y=c(0,0,1,9,10,10) > siegel.tukey(x,y, F)   Median of group 1 = 5 Median of group 2 = 5   Testing median differences...   Wilcoxon rank sum test with continuity correction   data: data\$x[data\$y == 0] and data\$x[data\$y == 1] W = 18, p-value = 1 alternative hypothesis: true location shift is not equal to 0   Performing Siegel-Tukey rank transformation...   sort.x sort.id unique.ranks 1 0 1 2.5 2 0 1 2.5 3 1 1 5.0 4 4 0 8.5 5 4 0 8.5 6 5 0 11.5 7 5 0 11.5 8 6 0 8.5 9 6 0 8.5 10 9 1 6.0 11 10 1 2.5 12 10 1 2.5   Performing Siegel-Tukey test...   Mean rank of group 0: 9.5 Mean rank of group 1: 3.5   Wilcoxon rank sum test with continuity correction   data: ranks0 and ranks1 W = 36, p-value = 0.003601 alternative hypothesis: true location shift is not equal to 0   Warning message: In wilcox.test.default(data\$x[data\$y == 0], data\$x[data\$y == 1]) : cannot compute exact p-value with ties > siegel.tukey(x,y) #same as above   Median of group 1 = 4 Median of group 2 = 5   Testing median differences...   Wilcoxon rank sum test with continuity correction   data: data\$x[data\$y == 0] and data\$x[data\$y == 1] W = 0, p-value = 0.4795 alternative hypothesis: true location shift is not equal to 0   Performing Siegel-Tukey rank transformation...   sort.x sort.id unique.ranks 1 4 0 2.5 2 4 0 2.5 3 5 1 5.5 4 5 9 5.5 5 6 10 2.5 6 6 10 2.5   Performing Siegel-Tukey test...   Mean rank of group 0: 2.5 Mean rank of group 1: 5.5   Wilcoxon rank sum test with continuity correction   data: ranks0 and ranks1 W = 0, p-value = 0.4795 alternative hypothesis: true location shift is not equal to 0   Warning message: In wilcox.test.default(data\$x[data\$y == 0], data\$x[data\$y == 1]) : cannot compute exact p-value with ties > > ### 2 > # example for a non equal number of cases: > x=c(4,4,5,5,6,6) > y=c(0,0,1,9,10) > siegel.tukey(x,y,F)   Median of group 1 = 5 Median of group 2 = 1   Testing median differences...   Wilcoxon rank sum test with continuity correction   data: data\$x[data\$y == 0] and data\$x[data\$y == 1] W = 18, p-value = 0.6451 alternative hypothesis: true location shift is not equal to 0   Performing Siegel-Tukey rank transformation...   sort.x sort.id unique.ranks 1 0 1 2.5 2 0 1 2.5 3 1 1 5.0 4 4 0 8.5 5 4 0 8.5 6 5 0 10.5 7 5 0 10.5 8 6 0 6.5 9 6 0 6.5 10 9 1 3.0 11 10 1 2.0   Performing Siegel-Tukey test...   Mean rank of group 0: 8.5 Mean rank of group 1: 3   Wilcoxon rank sum test with continuity correction   data: ranks0 and ranks1 W = 30, p-value = 0.007546 alternative hypothesis: true location shift is not equal to 0   Warning message: In wilcox.test.default(data\$x[data\$y == 0], data\$x[data\$y == 1]) : cannot compute exact p-value with ties > > ### 3 > x <- c(33, 62, 84, 85, 88, 93, 97, 4, 16, 48, 51, 66, 98) > id <- c(0,0,0,0,0,0,0,1,1,1,1,1,1) > siegel.tukey(x,id,T)   Median of group 1 = 85 Median of group 2 = 49.5   Testing median differences...   Wilcoxon rank sum test   data: data\$x[data\$y == 0] and data\$x[data\$y == 1] W = 31, p-value = 0.1807 alternative hypothesis: true location shift is not equal to 0   Performing Siegel-Tukey rank transformation...   sort.x sort.id unique.ranks 1 4 1 1 2 16 1 4 3 33 0 5 4 48 1 8 5 51 1 9 6 62 0 12 7 66 1 13 8 84 0 11 9 85 0 10 10 88 0 7 11 93 0 6 12 97 0 3 13 98 1 2   Performing Siegel-Tukey test...   Mean rank of group 0: 7.714286 Mean rank of group 1: 6.166667   Wilcoxon rank sum test with continuity correction   data: ranks0 and ranks1 W = 26, p-value = 0.5203 alternative hypothesis: true location shift is not equal to 0   > siegel.tukey(x~id) # from now on, this also works as a function...   Median of group 1 = 85 Median of group 2 = 49.5   Testing median differences...   Wilcoxon rank sum test   data: data\$x[data\$y == 0] and data\$x[data\$y == 1] W = 31, p-value = 0.1807 alternative hypothesis: true location shift is not equal to 0   Performing Siegel-Tukey rank transformation...   sort.x sort.id unique.ranks 1 4 1 1 2 16 1 4 3 33 0 5 4 48 1 8 5 51 1 9 6 62 0 12 7 66 1 13 8 84 0 11 9 85 0 10 10 88 0 7 11 93 0 6 12 97 0 3 13 98 1 2   Performing Siegel-Tukey test...   Mean rank of group 0: 7.714286 Mean rank of group 1: 6.166667   Wilcoxon rank sum test with continuity correction   data: ranks0 and ranks1 W = 26, p-value = 0.5203 alternative hypothesis: true location shift is not equal to 0   > siegel.tukey(x,id,T,adjust.median=F,exact=T)   Median of group 1 = 85 Median of group 2 = 49.5   Testing median differences...   Wilcoxon rank sum test   data: data\$x[data\$y == 0] and data\$x[data\$y == 1] W = 31, p-value = 0.1807 alternative hypothesis: true location shift is not equal to 0   Performing Siegel-Tukey rank transformation...   sort.x sort.id unique.ranks 1 4 1 1 2 16 1 4 3 33 0 5 4 48 1 8 5 51 1 9 6 62 0 12 7 66 1 13 8 84 0 11 9 85 0 10 10 88 0 7 11 93 0 6 12 97 0 3 13 98 1 2   Performing Siegel-Tukey test...   Mean rank of group 0: 7.714286 Mean rank of group 1: 6.166667   Wilcoxon rank sum test   data: ranks0 and ranks1 W = 26, p-value = 0.5338 alternative hypothesis: true location shift is not equal to 0   > > ### 4 > x<-c(177,200,227,230,232,268,272,297,47,105,126,142,158,172,197,220,225,230,262,270) > id<-c(rep(0,8),rep(1,12)) > siegel.tukey(x,id,T,adjust.median=T)   Adjusting medians...   Median of group 1 = 0 Median of group 2 = 0   Testing median differences...   Wilcoxon rank sum test   data: data\$x[data\$y == 0] and data\$x[data\$y == 1] W = 52, p-value = 0.7921 alternative hypothesis: true location shift is not equal to 0   Performing Siegel-Tukey rank transformation...   sort.x sort.id unique.ranks 1 -137.5 1 1 2 -79.5 1 4 3 -58.5 1 5 4 -54.0 0 8 5 -42.5 1 9 6 -31.0 0 12 7 -26.5 1 13 8 -12.5 1 16 9 -4.0 0 17 10 -1.0 0 20 11 1.0 0 19 12 12.5 1 18 13 35.5 1 15 14 37.0 0 14 15 40.5 1 11 16 41.0 0 10 17 45.5 1 7 18 66.0 0 6 19 77.5 1 3 20 85.5 1 2   Performing Siegel-Tukey test...   Mean rank of group 0: 13.25 Mean rank of group 1: 8.666667   Wilcoxon rank sum test with continuity correction   data: ranks0 and ranks1 W = 70, p-value = 0.09716 alternative hypothesis: true location shift is not equal to 0   > > > ### 5 > x=c(33,62,84,85,88,93,97) > y=c(4,16,48,51,66,98) > siegel.tukey(x,y) Error in data.frame(x, y) : arguments imply differing number of rows: 7, 6 > > ### 6 > x<-c(0,0,1,4,4,5,5,6,6,9,10,10) > id<-c(0,0,0,1,1,1,1,1,1,0,0,0) > siegel.tukey(x,id,T)   Median of group 1 = 5 Median of group 2 = 5   Testing median differences...   Wilcoxon rank sum test with continuity correction   data: data\$x[data\$y == 0] and data\$x[data\$y == 1] W = 18, p-value = 1 alternative hypothesis: true location shift is not equal to 0   Performing Siegel-Tukey rank transformation...   sort.x sort.id unique.ranks 1 0 0 2.5 2 0 0 2.5 3 1 0 5.0 4 4 1 8.5 5 4 1 8.5 6 5 1 11.5 7 5 1 11.5 8 6 1 8.5 9 6 1 8.5 10 9 0 6.0 11 10 0 2.5 12 10 0 2.5   Performing Siegel-Tukey test...   Mean rank of group 0: 3.5 Mean rank of group 1: 9.5   Wilcoxon rank sum test with continuity correction   data: ranks0 and ranks1 W = 0, p-value = 0.003601 alternative hypothesis: true location shift is not equal to 0   Warning message: In wilcox.test.default(data\$x[data\$y == 0], data\$x[data\$y == 1]) : cannot compute exact p-value with ties > > ### 7 > x <- c(85,106,96, 105, 104, 108, 86) > id<-c(0,0,1,1,1,1,1) > siegel.tukey(x,id,T)   Median of group 1 = 95.5 Median of group 2 = 104   Testing median differences...   Wilcoxon rank sum test   data: data\$x[data\$y == 0] and data\$x[data\$y == 1] W = 4, p-value = 0.8571 alternative hypothesis: true location shift is not equal to 0   Performing Siegel-Tukey rank transformation...   sort.x sort.id unique.ranks 1 85 0 1 2 86 1 4 3 96 1 5 4 104 1 7 5 105 1 6 6 106 0 3 7 108 1 2   Performing Siegel-Tukey test...   Mean rank of group 0: 2 Mean rank of group 1: 4.8   Wilcoxon rank sum test with continuity correction   data: ranks0 and ranks1 W = 1, p-value = 0.1752 alternative hypothesis: true location shift is not equal to 0```

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Tags: , , , , , , , , , ,