[R] Shapiro-Wilk for levels of factor

Achim Zeileis Achim.Zeileis at uibk.ac.at
Sun Feb 14 18:00:44 CET 2010


On Sun, 14 Feb 2010, Ravi Kulkarni wrote:

> Hello,
>  I have data for an ANOVA where the between-subjects factor has three
> levels. How do I run a test of normality (using shapiro.test) on each
> of the levels of the factor for the dependent variable separately
> without creating extra datasets?

You can use tapply(y, x, shapiro.test) which will then conduct as many 
Shapiro-Wilk tests as x has levels (without adjusting for multiple 
testing).

Another approach might be to look at shapiro.test(residualslm(y ~ x))) 
which tests the null hypothesis that the residuals in all groups come from 
the same normal distribution.

A worked example for the chickwts data is included below.
Z

## data
summary(chickwts)

## linear model and ANOVA
fm <- lm(weight ~ feed, data = chickwts)
anova(fm)

## QQ plot for residuals + Shapiro-Wilk test
shapiro.test(residuals(fm))

## separate tests for all groups of observations
## (with some formatting)
do.call("rbind", with(chickwts, tapply(weight, feed,
   function(x) unlist(shapiro.test(x)[c("statistic", "p.value")]))))


>  Thanks,
>
>    Ravi
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list