[R] How to run Shapiro-Wilk test for each grouped variable?

David Winsemius dwinsemius at comcast.net
Fri Apr 9 16:09:40 CEST 2010


On Apr 9, 2010, at 9:39 AM, Ivan Calandra wrote:

> Hi,
>
> Maybe you should change the "3" in the loop with "r" like:
> for (r in 3:18) {
> by(eval(parse(text=(paste("data",data.n[r],sep="$")))), data 
> $groupFactor, shapiro.test)
> }
>
> I think it should work, if not, I have already a similar script for  
> that.
> HTH,
> Ivan

Good catch, Ivan. There's a phrase ... "if it's not one thing, it's  
another", and it applies here although it needs to be modified to ...   
"if it's not one thing, it's two things". If one uses the example in  
the by help page with the warpbreaks data:

attach(warpbreaks)
for (i in 1:2) { by(warpbreaks[, 1], list(wool = wool, tension =  
tension), summary) }
for (i in 1:2) { print(by(warpbreaks[, 1], list(wool = wool, tension =  
tension), summary)) }

The first for-loop produces no output, the second one fills the screen.

-- 
David.
>
> Le 4/9/2010 15:17, David Winsemius a écrit :
>>
>> On Apr 9, 2010, at 8:16 AM, Iurie Malai wrote:
>>
>>> I want to run Shapiro-Wilk test for each variable in my dataset,  
>>> each
>>> grouped by variable groupFactor.
>>> I have these working commands:
>>>
>>>> data.n<-names(data) # put names into a vector called data.n
>>>> by(eval(parse(text=(paste("data",data.n[3],sep="$")))), data 
>>>> $factor, shapiro.test) #run shapiro.test
>>>
>>> but I must to change the variable number manualy. How to automate  
>>> this?
>>>
>>> I tried this:
>>>
>>>> for (r in 3:18) {
>>>> by(eval(parse(text=(paste("data",data.n[3],sep="$")))), data 
>>>> $groupFactor, shapiro.test)
>>>> }
>>
>> Not able to test since you have provided code that works with data  
>> that is not available. Inside for loops one needs either to make an  
>> assignment or print the results. Had the data been available I  
>> would have wrapped print() around the full by expression to see if  
>> my hypothesis could be tested.
>>
>
> -- 
> Ivan CALANDRA
> PhD Student
> University of Hamburg
> Biozentrum Grindel und Zoologisches Museum
> Abt. Säugetiere
> Martin-Luther-King-Platz 3
> D-20146 Hamburg, GERMANY
> +49(0)40 42838 6231
> ivan.calandra at uni-hamburg.de
>
> **********
> http://www.for771.uni-bonn.de
> http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list