For this project I am required to use an R script to simulate the effectiveness of the t-test. I must use a f开发者_开发问答or 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
}
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, and include the df as function arguments:
get.p.value <- function(df1, df2) {
x <- rf(5, df1=df1, df2=df2)
p.value <- t.test(x, mu=(df2/(df2-2)))$p.value
}
replicate (2000, get.p.value(df1 = 5, df2 = 10))
This is not always true, but it simplifies the recovery of the p.values.
精彩评论