Search code examples
rstatistics-bootstrapt-test

T-test with bootstrap in R


I am trying to run a t-test with bootstrap in R. I have a sample of 50 participants, 39 are females. I have a dependent variable, d' and want to see if males and females differ on this var. As I only have 11 male participants, I want to use a bootstrapped t-test (not the best idea but I've seen it in literature).

I have a database called "data" with several variables. So, first I extracted two vectors:

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

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

Then, I tried several things found on the internet (and here). Based on this post I tried:

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
    }

But it says:

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

I also tried this: boot.t.test: Bootstrap t-test

First, I couldn't load the functions. So, I copy-pasted and ran this:

Bootstrap Function

Then I ran this:

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

But, it says this:

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)

If I use warnings() it says:

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

Etc...

To be more clear, I am thinking of something like the bootstrapped t-test in SPSS, like this: enter image description here

I thought this was going to be much easier. Any help is welcome

Thank you all for your time.

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

Solution

  • Here's an example of using that function with simulated data where you'd expect a p-value close to 1. No need to subset it beforehand and create intermediate objects.

    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