[R] simulation of levene's test

R. Michael Weylandt michael.weylandt at gmail.com
Mon May 28 18:39:35 CEST 2012


On Mon, May 28, 2012 at 12:14 PM, Özgür Asar <oasar at metu.edu.tr> wrote:
> Dear Dila,
>
> Try the following:
>
> library(Rcmdr)

Or avoid the unncessary overhead of Rcmdr and use

library(car)

to provide levenTest instead.


> asim <- 1000
> pv<-NULL

It's also many orders of magnitude more efficient to preallocate "pv"
and then simply put things "into" it.

pv <- vector("real", 1000)

> for(i in 1:asim)
> {
> print(i)
> set.seed(i)

Setting the seed each loop seems excessive but I suppose it's a matter
of taste.

> g1 <- rnorm(20,0,2)
> g2 <- rnorm(20,0,2)
> g3 <- rnorm(20,0,2)
> x <- c(g1,g2,g3)

Is there any reason not to do this as x <- rnorm(60, 0, 2)

> group<-as.factor(c(rep(1,20),rep(2,20),rep(3,20)))

and this as as.factor(rep(1:3, each = 20))


> pv<-c(pv,leveneTest(x,group)$"Pr(>F)"[1])

Once you preallocate pv change this to

pv[i] <- leveneTest(x, group)$"Pr(>F)"[1]

But it's even better not to use the dollar sign shortcut here
(defensive programming and all that -- particularly with nonstandard
names which I'm pretty sure won't give a big error here but will
elsewhere)

pv[i] <- leveneTest(x, group)[["Pr(>F)"]][1]


And even better would be to do this all using the "replicate"
function, but I'll leave that as an exercise to the reader.

Michael

> }
>
> Best
> Ozgur
>
> -----
> ************************************
> Ozgur ASAR
>
> Research Assistant
> Middle East Technical University
> Department of Statistics
> 06531, Ankara Turkey
> Ph: 90-312-2105309
> http://www.stat.metu.edu.tr/people/assistants/ozgur/
> --
> View this message in context: http://r.789695.n4.nabble.com/simulation-of-levene-s-test-tp4631578p4631600.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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