Simulating t-test p-values using a for loop

前端 未结 1 1165
有刺的猬
有刺的猬 2021-01-27 21:48

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

相关标签:
1条回答
  • 2021-01-27 22:13

    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.

    0 讨论(0)
提交回复
热议问题