使用for循环模拟t检验p值 [英] Simulating t-test p-values using a for loop

查看:88
本文介绍了使用for循环模拟t检验p值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这个项目,我需要使用R脚本来模拟t检验的有效性.我必须使用for循环将用于执行以下2000次:

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:

循环看起来像这样

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
}

推荐答案

按照您编写它的方式,它将是一个"while"循环.

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

R中的For循环具有以下语法:

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
}

另外,采用"apply"构造(例如带有复制)并包括df作为函数参数可能会更有效:

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))

这并不总是正确的,但是它简化了p.values的恢复.

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

这篇关于使用for循环模拟t检验p值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆