【问题标题】:Pairwise t test using plyr使用 plyr 进行成对 t 检验
【发布时间】:2014-05-28 20:10:32
【问题描述】:

我想使用 R 包 plyr 在一个非常大的数据框上运行成对 t 检验,但我不知道该怎么做。我最近学习了如何使用 plyr 进行相关性,我非常喜欢您如何指定要比较的组,然后 plyr 为您分解数据。例如,您可以让 plyr 计算 iris 数据集中每种鸢尾花的萼片长度和萼片宽度之间的相关性,如下所示:

Correlations <- ddply(iris, "Species", function(x) cor(x$Sepal.Length, x$Sepal.Width))

可以自己分解数据框,指定鸢尾花品种的数据在 1:50 行中,依此类推,但 plyr 不太可能搞砸例如,不小心说出了第 1:51 行。

那么我该如何使用配对 t 检验做类似的事情呢?我如何指定哪些观察是成对的?这是一些与我正在使用的数据相似的示例数据,我希望这些对成为主题,并且我想按 Pesticide 分解数据:

 Exposure <- data.frame("Subject" = rep(1:4, 6), 
                     "Season" = rep(c(rep("summer", 4), rep("winter", 4)),3),
                     "Pesticide" = rep(c("atrazine", "metolachlor", "chlorpyrifos"), each=8),
                      "Exposure" = sample(1:100, size=24))
 Exposure$Subject <- as.factor(Exposure$Subject)

换句话说,我想评估的问题是,每个人在冬季和夏季的农药接触量是否存在差异,我想分别针对这三个问题分别回答这个问题杀虫剂。

提前非常感谢!

编辑:澄清一下,这是如何在 plyr 中进行非配对 t 检验:

 TTests <- dlply(Exposure, "Pesticide", function(x) t.test(x$Exposure ~ x$Season))

如果我在其中添加“paired=T”,plyr 将进行配对 t 检验,但它假定我总是以相同的顺序排列配对。虽然在上面的示例数据框中我确实以相同的顺序将它们全部保存,但我没有在我的真实数据中,因为我有时会丢失数据。

【问题讨论】:

    标签: r plyr hypothesis-test


    【解决方案1】:

    你想要这个吗?

    library(data.table)
    
    # convert to data.table in place
    setDT(Exposure)
    
    # make sure data is sorted correctly
    setkey(Exposure, Pesticide, Season, Subject)
    
    Exposure[, list(res = list(t.test(Exposure[Season == "summer"],
                                      Exposure[Season == "winter"],
                                      paired = T)))
             , by = Pesticide]$res
    #[[1]]
    #
    #        Paired t-test
    #
    #data:  Exposure[Season == "summer"] and Exposure[Season == "winter"]
    #t = -4.1295, df = 3, p-value = 0.02576
    #alternative hypothesis: true difference in means is not equal to 0
    #95 percent confidence interval:
    # -31.871962  -4.128038
    #sample estimates:
    #mean of the differences 
    #                    -18 
    #
    #
    #[[2]]
    #
    #        Paired t-test
    #
    #data:  Exposure[Season == "summer"] and Exposure[Season == "winter"]
    #t = -6.458, df = 3, p-value = 0.007532
    #alternative hypothesis: true difference in means is not equal to 0
    #95 percent confidence interval:
    # -73.89299 -25.10701
    #sample estimates:
    #mean of the differences 
    #                  -49.5 
    #
    #
    #[[3]]
    #
    #        Paired t-test
    #
    #data:  Exposure[Season == "summer"] and Exposure[Season == "winter"]
    #t = -2.5162, df = 3, p-value = 0.08646
    #alternative hypothesis: true difference in means is not equal to 0
    #95 percent confidence interval:
    # -30.008282   3.508282
    #sample estimates:
    #mean of the differences 
    #                 -13.25
    

    【讨论】:

    • 谢谢,@eddi;这让我更接近,但我仍然想知道如何处理缺失的观察结果。例如,如果我在冬天错过了采样主题 1,即使我对数据进行了适当的排序,我也会收到“并非所有参数的长度都相同”的错误。如果我在这里尝试用多元线性回归做类似的事情,即使缺少数据,这样的事情也会起作用:“lm(曝光〜季节+主题)”(在这种情况下不是最好的例子,但想不出更好的东西)。那将指定主题。也许这与 t 检验没有任何推论。
    • @LauraS 这是一个选项 - 在开始执行上述操作之前过滤数据:Exposure = Exposure[, if(.N == 2) .SD, by = list(Pesticide, Subject)]
    • 实际上,该代码对我不起作用; R告诉我“by = ...”部分未使用。 :-)
    • @LauraS 我想你忘了转换成data.table
    • 实际上,我对您回答中的代码还有其他几个问题:“setkey”和“setDT”命令是什么?在这里非常感谢您的帮助!
    【解决方案2】:

    我不知道ddply,但这里是我如何使用一些base 函数。

    by(data = Exposure, INDICES = Exposure$Pesticide, FUN = function(x) {
      t.test(Exposure ~ Season, data = x)
    })
    
    Exposure$Pesticide: atrazine
    
        Welch Two Sample t-test
    
    data:  Exposure by Season
    t = -0.1468, df = 5.494, p-value = 0.8885
    alternative hypothesis: true difference in means is not equal to 0
    95 percent confidence interval:
     -49.63477  44.13477
    sample estimates:
    mean in group summer mean in group winter 
                   60.50                63.25 
    
    ---------------------------------------------------------------------------------------------- 
    Exposure$Pesticide: chlorpyrifos
    
        Welch Two Sample t-test
    
    data:  Exposure by Season
    t = -0.8932, df = 4.704, p-value = 0.4151
    alternative hypothesis: true difference in means is not equal to 0
    95 percent confidence interval:
     -83.58274  41.08274
    sample estimates:
    mean in group summer mean in group winter 
                   52.25                73.50 
    
    ---------------------------------------------------------------------------------------------- 
    Exposure$Pesticide: metolachlor
    
        Welch Two Sample t-test
    
    data:  Exposure by Season
    t = 0.8602, df = 5.561, p-value = 0.4252
    alternative hypothesis: true difference in means is not equal to 0
    95 percent confidence interval:
     -39.8993  81.8993
    sample estimates:
    mean in group summer mean in group winter 
                    62.5                 41.5 
    

    【讨论】:

    • 嗯...感谢@Roman Luštrik 的建议,但这并不能解决配对问题。
    猜你喜欢
    • 2016-03-28
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 2017-02-02
    • 2023-04-09
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    相关资源
    最近更新 更多