[R] Neuman-Keuls

Mark Wardle mark at wardle.org
Wed Jul 18 21:50:30 CEST 2007


Hi Elyakhlifi,

I'm not a statistics expert and so can't intuit what your code is
doing easily. I also can't just run the code as supplied, as it uses
variables and data structures you haven't specified.

>From a coding perspective, there are several issues though that will
almost certainly make a difference.

You need to read about SCOPE.

Try isolating functions, and strictly define inputs and outputs. Your
function magically uses variables E, exple, lst, lst1, E, and others.
Don't do this! Try to make your functions a little more generic and
restrict using global variables!

try defining your function  (and I would use a better name too, but
that is personal preference!) to

NK <- function(x, E, exple) {
   lst<- list()
   lst1<-list()
   lst2<-list()

  ...

  and then return(lst)
}

USE the result of the function by doing this:

returned.lst <- NK(whatever... )

and not expecting NK to magically set a variable in the global scope.

It is possible to set variables in the parent scope from a function,
but I would suggest you don't even look up how to do this!

Best wishes,

Mark



On 18/07/07, elyakhlifi mustapha <elyakhlifi_mustapha at yahoo.fr> wrote:
> hello,
> I have programmed this function to calculate the Neuman-Keuls test but I have a problem the function return an empty list and I don't know why.
>
> summary(fm1)
> E <- sqrt((summary(fm1)[[1]]["Residuals","Mean Sq"])/length(LR))
> lst <- list()
> lst1 <- list()
> lst2 <- list()
> NK <- function (x) {
>  if (length(x) == 2) {
>   Tstudent  <- t.test(subset(exple, groupe == names(x)[1])$vd,subset(exple, groupe == names(x)[2])$vd)
>   t <- as.numeric(Tstudent$statistic)
>   if (t >= Tstudent$conf.int[1:2][1] & t <= Tstudent$conf.int[1:2][2]) {
>    lst1[[1]] <- x
>    lst <- c(lst,lst1)
>   } else {
>    lst1[[1]] <- x[1]
>    lst2[[1]] <- x[2]
>    lst <- c(lst,lst1)
>    lst <- c(lst,lst2)
>   }
>  } else {
>   test <- x[c(1,length(x))]
>   Q <- (as.numeric(test[2]) - as.numeric(test[1]))/E
>   qtt <- qtukey(0.95,length(x),df=41)
>   if (Q < qtt) {
>    lst1[[1]] <- range(x)
>    lst <- c(lst,lst1)
>   } else {
>    x1 <- x[-length(x)]
>    x2 <- x[-1]
>    NK(x1)
>    NK(x2)
>   }
>  }
>  return(lst)
> }
>   with
>
> > fm1
> Call:
>    aov(formula = vd ~ groupe, data = exple)
> Terms:
>                   groupe Residuals
> Sum of Squares  300.9871   27.0000
> Deg. of Freedom       20        41
> Residual standard error: 0.8115027
> Estimated effects may be unbalanced
>
>
> Can you help me please?
> thanks.
>
>
>       _____________________________________________________________________________
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
>
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
>


-- 
Dr. Mark Wardle
Clinical research fellow and specialist registrar, Neurology
Cardiff, UK



More information about the R-help mailing list