【问题标题】:Grabbing certain results out of multiple t.test outputs to create a table从多个 t.test 输出中获取某些结果以创建表
【发布时间】:2014-03-17 09:28:35
【问题描述】:

我已经运行了 48 次 t-tests(手工编码而不是编写循环),并且想拼接出那些 t.tests 的某些结果来创建一个我最感兴趣的事物的表格。

具体来说,我想只保留 p 值置信区间x 的平均值和 y 的平均值这 48 个测试中的每一个,然后构建一个结果表。

除了详细的最佳答案 here 之外,是否有一种优雅、快速的方法来执行此操作,其中我将参加所有 48 次测试,并使用类似于 ttest$p.value 的内容获取所有三个所需的输出?也许是一个循环?

下面是一个 t 检验的编码输入样本,然后是 R 提供的输出。

# t.test comparing means of Change_Unemp for 2005 government employment (ix)

lowgov6 <- met_res[met_res$Gov_Emp_2005 <= 93310, "Change_Unemp"]
highgov6 <- met_res[met_res$Gov_Emp_2005 > 93310, "Change_Unemp"]
t.test(lowgov6,highgov6,pool.sd=FALSE,na.rm=TRUE)  

Welch Two Sample t-test

data:  lowgov6 and highgov6
t = 1.5896, df = 78.978, p-value = 0.1159
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.1813909  1.6198399
sample estimates:
mean of x mean of y 
4.761224  4.042000 

【问题讨论】:

  • 你为什么要手工做48?他们叫什么名字?如果所有 48 的命名模式都相似,那么它很容易
  • 我在学习循环之前做过它们,但我仍然不确定如何执行一个。我想我会找出是否有办法做到这一点,而不是花时间在循环中为 t 检验重做我的工作。
  • 请参阅我的自动化问题t.tests:stackoverflow.com/questions/55249057/…

标签: r statistics


【解决方案1】:

将所有 t 检验保存到一个列表中:

tests <- list()
tests[[1]] <- t.test(lowgov6,highgov6,pool.sd=FALSE,na.rm=TRUE)
# repeat for all tests
# there are probably faster ways than doing all of that by hand

# extract your values using `sapply`
sapply(tests, function(x) {
     c(x$estimate[1],
       x$estimate[2],
       ci.lower = x$conf.int[1],
       ci.upper = x$conf.int[2],
       p.value = x$p.value)
})

输出如下:

                 [,1]        [,2]
mean of x  0.12095949  0.03029474
mean of y -0.05337072  0.07226999
ci.lower  -0.11448679 -0.31771191
ci.upper   0.46314721  0.23376141
p.value    0.23534905  0.76434012

但将有 48 列。如果您想转置结果,可以t()

【讨论】:

  • 嘿@Thomas,感谢分享代码。我们如何通过修改您的代码来获得两个置信区间?我发现这段代码可以单独为 x 或 y 变量执行此操作。对你的代码做同样的事情会很好。 stats.stackexchange.com/questions/86509/…
  • @Rocky 请在网站上将其作为一个新问题提出,并提供指向该问题和答案以及 CrossValidated 的那个问题的链接。
猜你喜欢
  • 2013-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多