[R] analysis of variance test

Jim Lemon drjimlemon at gmail.com
Fri May 29 13:35:27 CEST 2015


Hi Nezahat,
First, you are storing the code of the function "numeric" in x1 and
x2. You probably want to use:

x1<-numeric()
x2<-numeric()

Second, you are then storing the output of your aov summary (a list)
in x1, which requires a bit of analysis to get the information you
want (i.e. p value). The following will work for your example, but is
not a general solution.

nh_fun<-function(x) {
    pvals <-numeric()
    for(i in 2:length(x))
        pvals[i-1]<-unlist(summary(aov(x[,i] ~
factor(x[,1])))[[1]][5])[1] <= 0.05
    return(pvals)
}

nh_fun(x)

As you probably want to get the conventional <=0.05, I have changed
the criterion. If you want to understand why the mess of extractors
appears after the "summary" call, use the "str" function successively
on the return value from "summary"

Jim


On Fri, May 29, 2015 at 7:16 AM, Nezahat HUnter
<nezahathunter at yahoo.co.uk> wrote:
>
> Let's say I have 12 observation of 5 variables and my first variable is categorical (with 4 different levels). I am trying to find out statistical significance difference between these categorical levels for each variable, but my  function is not working! Please note that my data "x" are in data.frame format.
> Any suggestion would be helpful.Many thanks.
>
> function(x)
> {
>     x1 <- numeric
>     x2 <- numeric
>     for(i in 2:length(x)) {
>         x1[i] <- summary(aov(x[, i] ~ factor(x[, 1])))
>         x2[i] <- x1[i]$Pr[1]  #Pr is the probability values
>         if(x2[i] < 0.06)
>             x2[i] <- 1
>         else x2[i] <- 0
>     }
>     x2
> }
>
>
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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