Matrix of student t-test

Introduction

student t-test is used to compare the mean of two groups of samples. The aim of this article is to show how to perform a matrix of t-test in R. In this case a t-test is computed between each variable and the others. The result is a matrix of p-value containing the significance levels between all possible pairs of variables.

Note that online software is also available here to compute t-test without any installation.

Custom function for multiple t-test

We will use a the following custom R function :

# matrix of t-test
# mat : data.frame or matrix
# ... : further arguments to pass to the t.test function
multi.ttest <- function(mat, ...) {
  mat <- as.matrix(mat)
  n <- ncol(mat)
  p.mat<- matrix(NA, n, n)
  diag(p.mat) <- 1
  for (i in 1:(n - 1)) {
    for (j in (i + 1):n) {
      test <- t.test(mat[, i], mat[, j], ...)
      p.mat[i, j] <- p.mat[j, i] <- test$p.value
    }
  }
  colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
  signif(p.mat,4)
}

Data for t-test

The mtcars data is used in the following examples :

head(mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

We will use the first seven columns for the t-test.

Compute the matrix of t-test

p.mat<-multi.ttest(mtcars[,1:7])
p.mat
           mpg       cyl      disp        hp      drat        wt      qsec
mpg  1.000e+00 9.508e-15 7.978e-11 1.030e-11 3.164e-16 1.028e-16 5.107e-02
cyl  9.508e-15 1.000e+00 1.774e-11 8.322e-13 2.280e-09 9.118e-11 3.731e-35
disp 7.978e-11 1.774e-11 1.000e+00 1.546e-03 1.347e-11 1.293e-11 6.336e-11
hp   1.030e-11 8.322e-13 1.546e-03 1.000e+00 5.275e-13 4.919e-13 7.245e-12
drat 3.164e-16 2.280e-09 1.347e-11 5.275e-13 1.000e+00 6.025e-02 5.915e-33
wt   1.028e-16 9.118e-11 1.293e-11 4.919e-13 6.025e-02 1.000e+00 7.269e-39
qsec 5.107e-02 3.731e-35 6.336e-11 7.245e-12 5.915e-33 7.269e-39 1.000e+00

The result is a matrix of p-value between all possible pairs of variables.

Keep only the lower triangle of the matrix

The R function lower.tri() is used to hide the lower triangle of the matrix of the p-value

# Hide upper triangle
upper<-p.mat
upper[upper.tri(p.mat)]<-""
upper<-as.data.frame(upper)
upper
           mpg       cyl      disp        hp      drat        wt qsec
mpg          1                                                       
cyl  9.508e-15         1                                             
disp 7.978e-11 1.774e-11         1                                   
hp    1.03e-11 8.322e-13  0.001546         1                         
drat 3.164e-16  2.28e-09 1.347e-11 5.275e-13         1               
wt   1.028e-16 9.118e-11 1.293e-11 4.919e-13   0.06025         1     
qsec   0.05107 3.731e-35 6.336e-11 7.245e-12 5.915e-33 7.269e-39    1

Infos

This analysis has been performed using R (ver. 3.1.0).


Enjoyed this article? I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter, Facebook or Linked In.

Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
Avez vous aimé cet article? Je vous serais très reconnaissant si vous aidiez à sa diffusion en l'envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.

Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!





This page has been seen 14125 times