【问题标题】:R: multiple Friedman testsR:多个弗里德曼测试
【发布时间】:2014-12-05 15:11:39
【问题描述】:

多次做同一个测试时的小问题。

我正在使用friedman.test 来测试配对样本的变化。函数本身没有问题,我使用脚本对每一列都有预期的结果:

friedman.test(Variable ~ Time | Patient, data=table1)

但是,我为每位患者测量了几个变量(在几个时间点)。我可以使用上面的脚本对每个变量进行测试,但我想在一组选定的变量上按顺序自动进行测试。我尝试在向量或列表中输入要测试的变量,并将向量/列表用作“变量”参数,但它不起作用。

有人可以为这种类型的循环指出正确的方向吗?

谢谢! 赛博

【问题讨论】:

    标签: r loops statistics


    【解决方案1】:

    函数as.formula() 是关键。我会用一个小例子来解释。

    来自内置的 warpbreaks 数据集(参见?friedman.test):

    wb <- aggregate(warpbreaks$breaks,
                    by = list(w = warpbreaks$wool,
                              t = warpbreaks$tension),
                    FUN = mean)
    
    > friedman.test(x ~ w | t, data = wb)
    
        Friedman rank sum test
    
    data:  x and w and t
    Friedman chi-squared = 0.3333, df = 1, p-value = 0.5637
    

    现在,为了简单起见,假设我们有 3 个变量要在循环中测试而不是 x:

    (对于这个例子我每次都会使用x变量,因为它是一个演示)

    myvariables <- c('x','x','x')  #this is your vector with all of the variables you will use
    
    for ( i in myvariables) {  #and this block is the loop
      formula_text <- sprintf('%s ~ w | t', i) #writes the formula as text
      a <- as.formula(formula_text) #converts text to formula
      print(friedman.test(a, data = wb)) #runs as wanted!
    }
    

    上述循环的输出:

        Friedman rank sum test
    
    data:  x and w and t
    Friedman chi-squared = 0.3333, df = 1, p-value = 0.5637
    
    
        Friedman rank sum test
    
    data:  x and w and t
    Friedman chi-squared = 0.3333, df = 1, p-value = 0.5637
    
    
        Friedman rank sum test
    
    data:  x and w and t
    Friedman chi-squared = 0.3333, df = 1, p-value = 0.5637
    

    希望对你有帮助!

    【讨论】:

    • 太棒了!谢谢,这正是我想要的。
    【解决方案2】:

    尝试以下方法:

    varnames <- c("Variable1","Variable2")
    
    for (curvar in varnames) {
    print(curvar)
    print(friedman.test(table1[,curvar] ~ Time | Patient, data=table1)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 2014-02-22
      相关资源
      最近更新 更多