【问题标题】:T-test with bootstrap in R在 R 中使用引导程序进行 T 检验
【发布时间】:2020-01-11 08:27:42
【问题描述】:

我正在尝试在 R 中使用引导程序运行 t 检验。 我有 50 名参与者的样本,其中 39 名是女性。我有一个因变量 d' 并想看看男性和女性在这个变量上是否不同。由于我只有 11 名男性参与者,我想使用自举 t 检验(不是最好的主意,但我在文献中看到过)。

我有一个名为“data”的数据库,其中包含多个变量。所以,首先我提取了两个向量:

dPrimeFemales <- subset(data, Gender == "F", 
                  select=c(dPrime))

dPrimeMales <- subset(data, Gender == "M", 
                        select=c(dPrime))

然后,我尝试了一些在互联网上(和这里)找到的东西。 基于这个post我试过了:

set.seed(1315)
    B      <- 1000
    t.vect <- vector(length=B)
    p.vect <- vector(length=B)
    for(i in 1:B){
      boot.c <- sample(dPrimeFemales, size=nrow(dPrimeFemales), replace=T)
      boot.p <- sample(dPrimeMales, size=nrow(dPrimeMales), replace=T)
      ttest  <- t.test(boot.c, boot.p)
      t.vect[i] <- ttest$statistic
      p.vect[i] <- ttest$p.value
    }

但它说:

Error: Must use a vector in `[`, not an object of class matrix.
Call `rlang::last_error()` to see a backtrace

我也试过这个: boot.t.test: Bootstrap t-test

首先,我无法加载函数。所以,我复制粘贴并运行:

Bootstrap Function

然后我运行了这个:

boot.t.test(x = dPrimeFemales, y = dPrimeMales)

但是,它是这样说的:

Error in boot.t.test(x = dPrimeFemales, y = dPrimeMales) : 
  dims [product 1] do not match the length of object [1000]
In addition: There were 50 or more warnings (use warnings() to see the first 50)

如果我使用warnings(),它会说:

1: In mean.default(x) : argument is not numeric or logical: returning NA
2: In mean.default(y) : argument is not numeric or logical: returning NA
3: In mean.default(c(x, y)) : argument is not numeric or logical: returning NA
4: In mean.default(x) : argument is not numeric or logical: returning NA
5: In mean.default(y) : argument is not numeric or logical: returning NA

等等……

为了更清楚,我正在考虑类似 SPSS 中的自举 t 检验,如下所示:

我认为这会容易得多。 欢迎任何帮助

感谢大家的宝贵时间。

structure(list(dPrime = c(0.60805224661517, 0.430727299295457, 
-0.177380196159658, 0.771422126383253, 0.598621304083563, 0, 
0.167894004788105, -0.336998837042929, 0.0842422708809764, -0.440748778800912, 
0.644261556974516, -0.167303467814258, 0.169695369228671, -0.251545738695235, 
0.0842422708809764, -0.0985252105020469, -0.239508275220057, 
-0.143350050535084, 0.430727299295457, 0.757969499665785, -0.282230896122292, 
-0.271053409572241, -0.090032472207662, -0.090032472207662, 0.524400512708041, 
-0.218695510362827, -0.271053409572241, 1.07035864674857, 0.262833294507352, 
0.421241107923905, -0.0836517339071291, 0.090032472207662, -0.598621304083563, 
-0.356506507919935, 0.474566187745845, 0.336998837042929, 1.35083901409173, 
-0.336998837042929, -0.443021053393661, 0.757969499665785, -0.841621233572914, 
0.167303467814258, 0.167894004788105, 0.090032472207662, -0.177380196159658, 
0.251545738695235, -0.344495842891614, -0.17280082229969, -0.440748778800912, 
0), Gender = c("F", "F", "F", "F", "F", "F", "F", "F", "M", "M", 
"F", "F", "F", "F", "F", "F", "F", "F", "M", "F", "M", "M", "F", 
"F", "F", "F", "F", "F", "F", "F", "M", "F", "F", "F", "M", "F", 
"F", "F", "F", "M", "M", "F", "F", "M", "M", "F", "F", "F", "F", 
"F")), row.names = c(NA, -50L), class = c("tbl_df", "tbl", "data.frame"
))

【问题讨论】:

  • 我不清楚你到底打算引导什么。 t 统计量?
  • 可以分享data的结构吗?到底哪里出错了?
  • 是的,t 统计量。我会尝试上传数据(尝试中)...
  • 你应该可以在这里获取数据样本:filehosting.org/file/details/820987/data.Rda(不知道这里有没有更好的分享方式)。此外,我试图通过添加一张我试图在 SPSS 中实现的分析的图片来更清楚我的意思

标签: r statistics-bootstrap t-test


【解决方案1】:

这是一个将函数与模拟数据一起使用的示例,您希望 p 值接近 1。无需事先对其进行子集化并创建中间对象。

set.seed(0)
df <- data.frame(gender = sample(c('M', 'F'), size=50, replace=T),
                 measure = runif(n=50))

boot.t.test(df[df$gender=='M', 'measure'], df[df$gender=='F', 'measure'], reps=1000)

Bootstrap Two Sample t-test


t = -0.186, p-value = 0.859
Alternative hypothesis: true difference in means is not equal to 0

$mu0 
[1] 0

$statistic
[1] -0.1863362

$alternative
[1] "two.sided"

$p.value
[1] 0.859

【讨论】:

  • 嗨,Bill,这看起来不错,但您能告诉我您需要哪些软件包吗?因为我正在尝试类似的东西(我认为)但我得到: boot.t.test 中的错误(df[data$Gender == "M", "measure"], df[data$Gender == : could not find function “boot.t.test 显然我缺少一些包或库
  • 我也无法安装 tpepler/nonpar 包 (rdrr.io/github/tpepler/nonpar/src/R/boot.t.test.R),所以我只是将函数复制并粘贴到我的工作区中。不理想,但足以对其进行测试。
  • 抱歉,我只是在尝试您的建议。所以,这就是我尝试过的: set.seed(0) boot.t.test(data[data$Gender=='M', 'dPrime'], data[data$Gender=='F', 'dPrime' ], reps=1000) 我收到此错误: boot.t.test 中的错误(data[data$Gender == "M", "dPrime"], data[data$Gender == : dims [product 1] do not匹配对象的长度 [1000] 另外:有 50 个或更多警告(使用 warnings() 查看前 50 个)。这与我之前得到的类似(请参阅我的初始帖子)
  • 顺便说一句,如果我尝试您的示例,它会起作用。我真的不明白为什么它不适用于我的数据。
  • 无法回答,因为 filehosting.org 在我的工作场所被阻止。如果您使用 dput() 在上述问题中发布您的数据,我很乐意提供进一步的帮助。当您运行 data[data$Gender=='M', 'dPrime'] 时,它是否返回预期长度的数字向量?
猜你喜欢
  • 2015-09-17
  • 1970-01-01
  • 2019-07-22
  • 1970-01-01
  • 2016-03-28
  • 2021-06-16
  • 1970-01-01
  • 2012-11-28
  • 1970-01-01
相关资源
最近更新 更多