Search code examples
rvtablehypothesis-test

How to compare groups using a t.test in `vtable::sumtable`?


I have a simple question. I'd like like to use the group.test argument to conduct a t.test instead of an f.test between the two groups. I see from the help documents that this should be related to the independence.test function but I am not quite exactly sure what to do.

vtable::sumtable(data = mtcars, 
               vars = "mpg",
               group = "am", 
               out = "return",
               group.test = T)
#>   Variable  N Mean  SD  N Mean  SD       Test
#> 1       am  0           1                    
#> 2      mpg 19   17 3.8 13   24 6.2 F=16.86***

Created on 2023-08-24 with reprex v2.0.2


Solution

  • Apologies for the slow reply; been a moment since I searched vtable.

    As per the docs, as you noticed, doing a custom group.test requires that you write a function suitable to be passed to independence.test. This means that it returns a list with (1) the name of the test statistic, (2) the statistic, and (3) the p-value. So to do a t-test you'll have to write a function that does that, and then pass it to group.test as an option for numeric.test (or similarly if you want to write a function for factor.test)

    library(vtable)
    
    data(mtcars)
    
    custom_t = function(x, y, w = NULL) {
      t_test_result = t.test(y~x)
      stat = unname(t_test_result$statistic)
      pval = t_test_result$p.value
      return(list('t',stat,pval))
    }
    
    sumtable(mtcars, 
             vars = 'mpg',
             group = 'am',
             group.test = list('numeric.test' = custom_t))
    

    Edit: in vtable version 1.4.5, which is now on CRAN, this is included in the package, and you can get it using group.test = list('numeric.test' = vtable:::groupt.it)