T-Tests in R

A t-test is a type of inferential statistic used to determine if there is a significant difference between the means of two groups.

Note: When the output says p-value => 4.903e-08 this also means <0.0001.

Load Packages

packages <- c("tidyverse", "ggpubr", "ggcorrplot")

packages <- lapply(packages, FUN = function(x) {

if(!require(x, character.only = TRUE)) {install.packages(x)

library(x, character.only = TRUE)}})

Load dataset

data <- ToothGrowth

Independent 2-Group T-Test

One numeric and one binary factor

t.test(df$len ~ df$supp)

Create Plot

plot <- ggboxplot(df, x = "supp", y = "len", color = "supp", palette = "jco", add = "jitter") +

stat_compare_means() # Add p-value

plot

Both are numeric

t.test(df$len, df$dose)

plot <- ggboxplot(df, x = "len", y = "dose", color = "supp", palette = "jco", add = "jitter") +

stat_compare_means() # Add p-value

plot

Paired T-Test

One numeric and one binary factor

compare_means(len ~ supp, data = df, paired = TRUE)

Create Plot

ggpaired(df, x = "supp", y = "len", color = "supp", line.color = "gray", line.size = 0.4, palette = "jco")+stat_compare_means(paired = TRUE)

Both are numeric

t.test(df$len, df$dose, paired=TRUE)

One Sample T-Test

Ho: mu=3

t.test(df$len, mu=3)

You can use the var.equal = TRUE option to specify equal variances and a pooled variance estimate. You can use the alternative="less" or alternative="greater" option to specify a one tailed test.

The value 2.2e-16 actually means 2.2 X 10 ^ -16. It is just a way R prints numbers that are either too big or too small. As you said that if the P-Value is less than 0.05, we reject the null hypothesis in case of test with 95% confidence or 5% significance. In general, we reject the null hypothesis when the P-value is less then the level of significance of the test.And if you're doing at test with 95% confidence or 5% significance, P-value of 2.2 X 10 ^-16 is very less compared to 5%=0.05. So you will reject the null hypothesis.

Sabalico Logo
Sabalytics Logo
Senty Logo
SEO Guide Logo
World Map Logo
rStatistics Logo
Day Map Logo
Time Zone Logo
Galaxy View Logo
Periodic Table Logo
My Location Logo
Weather Track Logo
Sprite Sheet Logo
Barcode Generator Logo
Test Speed Logo
Website Tools Logo
Image Tools Logo
Color Tools Logo
Text Tools Logo
Finance Tools Logo
File Tools Logo
Data Tools Logo
History of Humanity - History Archive Logo
History of Humanity - History Mysteries Logo
History of Humanity - Ancient Mesopotamia Logo
History of Humanity - Persian Empire Logo
History of Humanity - Alexander the Great Logo
History of Humanity - Roman History Logo
History of Humanity - Punic Wars Logo
History of Humanity - Golden Age of Piracy Logo
History of Humanity - Revolutionary War Logo