Execute the tapply function for multiple variables in a dataframe (with pairwise.t.test)

2

I try to execute the tapply function for several variables at the same time, but with the use of the pairwise.t.test function. However, I do not succeed.

So, I have to do this:

pairwise.t.test(seguro1$tmp_habilit,seguro1$group,method='bonferroni')
pairwise.t.test(seguro1$estciv,seguro1$group,method='bonferroni')
pairwise.t.test(seguro1$nmultas,seguro1$group,method='bonferroni')

That is, one at a time.

dput to aid in response:

data=structure(list(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16, 17, 18, 19, 20), tmp_habilit = c(20, 21, 25, 
25, 18, 23, 9, 12, 15, 14, 15, 10, 8, 7, 11, 10, 7, 9, 1, 3), 
estciv = c(3, 3, 3, 2, 2, 1, 3, 2, 1, 2, 1, 3, 2, 2, 1, 2, 
2, 1, 3, 1), nmultas = c(1, 0, 2, 3, 2, 2, 6, 4, 3, 2, 5, 
5, 4, 13, 15, 9, 6, 10, 8, 5), group = structure(c(2L, 2L, 
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L), .Label = c("2", "1", "3"), class = "factor"), 
group1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("1", 
"2", "3"), class = "factor")), .Names = c("id", "tmp_habilit", 
"estciv", "nmultas", "group", "group1"), row.names = c(NA, -20L
), class = "data.frame")

The intent is not to type pairwsise.t.test multiple times (for each group). Is it possible to apply tapply to this example?

    
asked by anonymous 24.09.2018 / 01:05

2 answers

2

Here is an option, you have to define the position of the variables you want to apply the test ( varNames ) and then apply sapply :

    # definir variáveis para o teste
    varNames <- names(seguro1)[2:4] # valor da coluna de cada variável desejável

    # aplicar 'sapply' para cada variável
    res <- sapply(varNames, function(x) pairwise.t.test(seguro1[, x],seguro1$group,method='bonferroni'), simplify = FALSE)

Since we use the simplify = FALSE argument, the res object is a list. To access each result of pairwise.t.test :

  res[[1]]

  #Pairwise comparisons using t tests with pooled SD
  #
  #data:  seguro1[, x] and seguro1$group
  #
  #  2       1
  #1 4.8e-05 -
  #3 0.0091  4.3e-07
  #
  #P value adjustment method: holm
    
24.09.2018 / 21:08
2

I do not know a solution with tapply but follows a solution in tidyverse style. In the end, you get a table with the p values for all the tests we did.

library(purrr)
library(dplyr)
library(tidyr)

cross_df(list(variaveis = variaveis, grupos = grupos)) %>%
  mutate(ttest = map2(
    variaveis, grupos, ~pairwise.t.test(data[[.x]], data[[.y]], "bonferroni"))
  ) %>%
  mutate(ttesttidy = map(ttest, broom::tidy)) %>%
  unnest(ttesttidy) 

# A tibble: 18 x 5
   variaveis   grupos group1 group2     p.value
   <chr>       <chr>  <chr>  <chr>        <dbl>
 1 tmp_habilit group  1      2      0.0000727  
 2 tmp_habilit group  3      2      0.0273     
 3 tmp_habilit group  3      1      0.000000428
 4 estciv      group  1      2      1          
 5 estciv      group  3      2      1          
 6 estciv      group  3      1      0.540      
 7 nmultas     group  1      2      0.226      
 8 nmultas     group  3      2      0.00178    
 9 nmultas     group  3      1      0.0000488  
10 tmp_habilit group1 2      1      0.0000727  
11 tmp_habilit group1 3      1      0.000000428
12 tmp_habilit group1 3      2      0.0273     
13 estciv      group1 2      1      1          
14 estciv      group1 3      1      0.540      
15 estciv      group1 3      2      1          
16 nmultas     group1 2      1      0.226      
17 nmultas     group1 3      1      0.0000488  
18 nmultas     group1 3      2      0.00178  
    
24.09.2018 / 21:56