Two different samples with the same result in the Shapiro Wilk test

Viewed 90

Two different samples generated with the rnorm function produce the same result in the Shapiro-Wilk test. I would greatly appreciate an explanation of my error.

> set.seed(18102003)
> x=rnorm(n=6,mean=1.29,sd=1.29*0.33)
> x
[1] 2.0989973 1.5440310 0.8401053 1.3630999 1.3274229 1.9812853
> shapiro.test(x)

        Shapiro-Wilk normality test

data:  x
W = 0.94929, p-value = 0.7345

> set.seed(18102003)
> x=rnorm(6,2.97,2.97*0.49)
> x
[1] 5.735642 3.838432 1.431988 3.219900 3.097934 5.333231
> shapiro.test(x)

        Shapiro-Wilk normality test

data:  x
W = 0.94929, p-value = 0.7345
1 Answers

The tests' results are exactly the same because the vectors are copies of one another scaled and the Shapiro-Wilks test is invariant to changes in location and scale. This can be seen if we compute the differences between the scaled vectors.

First, generate the vectors again, but assigning them different names, and a new vector z.

set.seed(18102003)
x <- rnorm(n = 6, mean = 1.29, sd = 1.29*0.33)
set.seed(18102003)
y <- rnorm(6, 2.97, 2.97*0.49)
set.seed(18102003)
z <- rnorm(n = 6)

Now see that x and y are copies of z

all.equal(x, 1.29*0.33*z + 1.29)
#[1] TRUE
all.equal(y, 2.97*0.49*z + 2.97)
#[1] TRUE

scale(x) - scale(y)  # equal up to floating-point precision
scale(x) - scale(z)  # the same

Now run the tests, and compare the tests statistics with identical.

identical(shapiro.test(z)$statistic, shapiro.test(x)$statistic)
#[1] TRUE
identical(shapiro.test(z)$statistic, shapiro.test(y)$statistic)
#[1] TRUE
Related