# Shapiro-Wilk test with multiple conditions in R

I have to perform a Shapiro-Wilk normality test. My dataset is build like this:

Condition0 <- c(0.9201584, 0.8386860, 0.8092635, 0.8166590, 0.8653545)
Condition1 <- c(0.9905397, 0.9498400, 1.0378111, 0.9740314, 1.0355568)
Condition2 <- c(0.9529179, 1.0919274, 1.0089470, 1.0067670, 0.9686904)
Condition3 <- c(0.7402958, 0.7890059, 0.8060471, 0.8020820, 0.7931508)
Condition4 <- c(0.7725662, 0.6916708, 0.7698080, 0.7476060, 0.7602339)
Control <- c(0.7707546, 0.7035131, 0.7268695, 0.8217838, 0.7641010)

dataset <- data.frame(Condition0, Condition1, Condition2, Condition3, Condition4, Control, row.names = c("d1", "d2", "d3", "d4", "d5"))


As you see can there are 6 conditions with 5 results for each condition.

Is it right to do the Shapiro-Wilk test in this way, dividing the dataset for every single condition?

shapiro.test(dataset$Condition0) shapiro.test(dataset$Condition1)
shapiro.test(dataset$Condition2) shapiro.test(dataset$Condition3)
shapiro.test(dataset$Condition4) shapiro.test(dataset$Control)


or, should I built my dataset in a different way and do the test in the whole dataset, regardless of the factor "condition"? Like this:

    my_data <- c(0.9201584, 0.8386860, 0.8092635, 0.8166590, 0.8653545,
0.9905397, 0.9498400, 1.0378111, 0.9740314, 1.0355568,
0.9529179, 1.0919274, 1.0089470, 1.0067670, 0.9686904,
0.7402958, 0.7890059, 0.8060471, 0.8020820, 0.7931508,
0.7725662, 0.6916708, 0.7698080, 0.7476060, 0.7602339,
0.7707546, 0.7035131, 0.7268695, 0.8217838, 0.7641010)
cycle <- rep(c("d1", "d2", "d3", "d4", "d5"), 6)
Condition <- rep(c("Condition0","Condition1", "Condition2", "Condition3", "Condition4", "Control"), each = 5)

dataset2 <- data.frame(my_data, cycle, Condition)

shapiro.test(dataset2$my_data)  After the Shapiro-Wilk test I'll run an ANOVA or a Kruskall-Wallis (depends on the result) to see if there is any difference among different conditions. Like this: my_lm <- lm(my_data ~ Condition, data= dataset2) anova(my_lm)  Which one do you think is the correct one? Thank you for the answer! • (Although asked in the context of R, this is a statistical question & is on topic here.) – gung Oct 24 '16 at 12:15 • yes, it is on topic. The question is basically: Should I run the Shapiro-Wilk using the entire dataset, or should I divide the dataset based on the different condition and therefore run the Shapiro-Wilk on 6 small datasets? – Wildebeest Oct 24 '16 at 12:23 • Those are distinctly different tests, so your question can be resolved by considering what it is you want to determine: are you trying to tell whether the collection of all data is consistent with independent draws from a single Normal distribution or do you want to know whether each group looks like independent draws from some Normal distribution, but possibly a different distribution in each group? – whuber Oct 24 '16 at 12:37 • I think that the second description @whuber give is the right one. Since each "Condition" refers to a different treatment, it is important to maintain the groups separate? Does it make sense? – Wildebeest Oct 24 '16 at 15:03 • Where does the compulsion arise ("have to perform a Shapiro Wilk")? Whoever is compelling you to run the test should probably answer this question. Who knows what their reasoning might be? If you were to ask me, I'd say the best choice is to perform it zero times (but to do something else instead, something less likely to lead you to do exactly the wrong thing later), but I'm not the one compelling you to do it -- it's hard to give advice on actions I disagree with absent a good rationale for doing them at all. – Glen_b Oct 24 '16 at 23:09 ## 1 Answer What matters for ANOVA is the normality of the residuals rather than the raw normality. The Shapiro-Wilk test is only one of the possible ways of checking normality, others including boxplots, plot(resid(model)), and z-scores of skewness and kurtosis, stat.desc(model, norm=T) (with the pastecs package). Never rely on Shapiro alone. In fact, the z-scores are possibly the go-to scores, as they are robust to sample size, and don't require you being terribly experienced with instances of non-normality. The key figures for the z-scores are 'skew.2SE' and 'kurt.2SE', and the scores are expected to lie below 0.96 if the distribution is normal. Now, if those residuals are non-normal, then consider solutions. Besides non-parametric alternatives to ANOVA, you might try replacing your dependent variable with its transformation, e.g., log(), and then checking the residuals again. Aside from that, if you want to check the normality of the variables themselves, do it per condition. Personally, I would enter the data in long format, with condition as one column, and then use subsetting, i.e., dataset[dataset$condition=='0',], dataset[dataset\$condition=='1',]...

For a more advanced discussion of normality (along the lines of @Glen_b's comments), see this question.