Perform the tapply function for multiple variables on a dataframe (with pairwise.t.test)

Asked

Viewed 151 times

2

I try to perform the function tapply for several variables at the same time, but using the function pairwise.t.test. 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')

I mean, one at a time.

dput for assistance 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 intention is not to have to type pairwsise.t.test several times (for each group). It is possible to apply tapply in this example?

  • Corrigendum: is not method='bonferroni' and yes p.adj='bonferroni'

2 answers

2


Here is an option, you have to set the position of the variables you want to apply the test (varNames) and then apply the 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)

How we use the argument simplify = FALSE, the object res 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

2

I don’t know a solution with tapply but follows a solution in style tidyverse. At the end, you get a table with 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  

Browser other questions tagged

You are not signed in. Login or sign up in order to post.