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.
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).
Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
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!
Recommended for You!
Recommended for you
This section contains the best data science and self-development resources to help you on your path.
Books - Data Science
Our Books
- Practical Guide to Cluster Analysis in R by A. Kassambara (Datanovia)
- Practical Guide To Principal Component Methods in R by A. Kassambara (Datanovia)
- Machine Learning Essentials: Practical Guide in R by A. Kassambara (Datanovia)
- R Graphics Essentials for Great Data Visualization by A. Kassambara (Datanovia)
- GGPlot2 Essentials for Great Data Visualization in R by A. Kassambara (Datanovia)
- Network Analysis and Visualization in R by A. Kassambara (Datanovia)
- Practical Statistics in R for Comparing Groups: Numerical Variables by A. Kassambara (Datanovia)
- Inter-Rater Reliability Essentials: Practical Guide in R by A. Kassambara (Datanovia)
Others
- R for Data Science: Import, Tidy, Transform, Visualize, and Model Data by Hadley Wickham & Garrett Grolemund
- Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems by Aurelien Géron
- Practical Statistics for Data Scientists: 50 Essential Concepts by Peter Bruce & Andrew Bruce
- Hands-On Programming with R: Write Your Own Functions And Simulations by Garrett Grolemund & Hadley Wickham
- An Introduction to Statistical Learning: with Applications in R by Gareth James et al.
- Deep Learning with R by François Chollet & J.J. Allaire
- Deep Learning with Python by François Chollet
Click to follow us on Facebook :
Comment this article by clicking on "Discussion" button (top-right position of this page)