views:

278

answers:

1

For this project you are required to use an R script to simulate the effectiveness of the t-test. A for loop will be used to carry out the following 2000 times:

Would the loop look something like this

i <- 1
for (i <= 2001) { 
    x <-rf(5,df1=5,df2=10)
    b <- df2
    p.value <-t.test(x,mu=(b/(b-2))$p.value
    i <- i+1
}
+4  A: 

In the way you wrote it, it would be a "while" loop.

For loops in R have the following syntax:

for (i in 1:2000) {
    df1 <- 5
    df2 <- 10
    x <-rf(5, df1=df1, df2=df2)
    b <- df2
    p.value <- t.test(x, mu=(b/(b-2)))$p.value
}

Additionally, it might be more efficient to employ an "apply" construct, for example with replicate:

get.p.value <- function() {
        df1 <- 5
        df2 <- 10
        x <-rf(5, df1=df1, df2=df2)
        b <- df2
        p.value <- t.test(x, mu=(b/(b-2)))$p.value
    }
replicate (2000, get.p.value())

This is not always true, but it simplifies the recovery of the p.values.

Calimo
In your first solution you could take assignments out of loop. There id no need to 2000 times assign the same value. So it could be done like `df1<-5;df2<-b<-10;for(i in 1:2000) p.value <- t.test(rf(5, df1=df1, df2=df2), mu=(b/(b-2)))$p.value`
Marek
thanks heaps that helps more than you can imangine,
Simon
And for `for` version (in my modification too) results aren't save. So after loop you stay with one value. So your replicate solution is much better. +1 for this
Marek
You're right Marek, the assignments to df1, df2, b (why b in the first place?) should be out of the loop. Even mu could be pre-computed.To save the p.values, the worst is to grow a vector with `p.values <- c(p.values, t.test(...))` in the `for` loop. `replicate` somehow pre-allocates a vector of the desired length at the beginning, and avoid slow memory re-allocations.For long computations, think about the `plyr` package that can display progression bars.
Calimo
ok im slightly confused by that calimo (also thanks again for the help) i chose b as i assignmed <-df2 to it and then in the t.test i used the B value rather than df2 so that way i could easily change the input value of df2.this is the actual link to what im doing its rather short if you wanted to look. http://www.2shared.com/document/lf3biLds/R_assignment_two.html
Simon