Install or load ggpubR package for data visualization

library(ggpubr)
## Warning: package 'ggpubr' was built under R version 3.6.2
## Loading required package: ggplot2
## Registered S3 methods overwritten by 'ggplot2':
##   method         from 
##   [.quosures     rlang
##   c.quosures     rlang
##   print.quosures rlang
## Loading required package: magrittr

R Function to Compute one-sample t-test

t.test(x, mu = 0, alternative = “two.sided”) x: a numeric vector containing your data values mu: the theoretical mean. Default is 0 but you can change it. alternative: the alternative hypothesis. Allowed value is one of “two.sided” (default), “greater” or “less”.

We want to know if the average weight of mice differs from 25 g

set.seed(1234)
my_data <- data.frame(
  name = paste0(rep("M_", 10), 1:10),
  weight = round(rnorm(10, 20, 2), 1)
)

##Check your Data

# Print the first 10 rows of the data
head(my_data, 10)
##    name weight
## 1   M_1   17.6
## 2   M_2   20.6
## 3   M_3   22.2
## 4   M_4   15.3
## 5   M_5   20.9
## 6   M_6   21.0
## 7   M_7   18.9
## 8   M_8   18.9
## 9   M_9   18.9
## 10 M_10   18.2

Produce Summary

# Statistical summaries of weight
summary(my_data$weight)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   15.30   18.38   18.90   19.25   20.82   22.20

Min.: the minimum value 1st Qu.: The first quartile. 25% of values are lower than this. Median: the median value. Half the values are lower; half are higher. 3rd Qu.: the third quartile. 75% of values are higher than this. Max.: the maximum valueMin.: the minimum value 1st Qu.: The first quartile. 25% of values are lower than this. Median: the median value. Half the values are lower; half are higher. 3rd Qu.: the third quartile. 75% of values are higher than this. Max.: the maximum value

ggboxplot(my_data$weight, 
          ylab = "Weight (g)", xlab = FALSE,
          ggtheme = theme_minimal())

Preliminary Test to check one sample t test assumptions

Is this a large sample

No, because n < 30. Since the sample size is not large enough (less than 30, central limit theorem), we need to check whether the data follow a normal distribution.

How to check the normality

Briefly, it’s possible to use the Shapiro-Wilk normality test and to look at the normality plot.

Shapiro-Wilk test: Null hypothesis: the data are normally distributed Alternative hypothesis: the data are not normally distributed

shapiro.test(my_data$weight)
## 
##  Shapiro-Wilk normality test
## 
## data:  my_data$weight
## W = 0.9526, p-value = 0.6993

From the output, the p-value is greater than the significance level 0.05 implying that the distribution of the data are not significantly different from normal distribtion. In other words, we can assume the normality.

Visual inspection of the data normality using Q-Q plots (quantile-quantile plots). Q-Q plot draws the correlation between a given sample and the normal distribution.

ggqqplot(my_data$weight, ylab = "Men's weight",
         ggtheme = theme_minimal())

From the normality plots, we conclude that the data may come from normal distributions.

Note that, if the data are not normally distributed, it’s recommended to use the non parametric one-sample Wilcoxon rank test.

Compute one-sample Test

We want to know, if the average weight of the mice differs from 25g (two-tailed test)?

# One-sample t-test
res <- t.test(my_data$weight, mu = 25)
# Printing the results
res 
## 
##  One Sample t-test
## 
## data:  my_data$weight
## t = -9.0783, df = 9, p-value = 7.953e-06
## alternative hypothesis: true mean is not equal to 25
## 95 percent confidence interval:
##  17.8172 20.6828
## sample estimates:
## mean of x 
##     19.25

In the result above :

t is the t-test statistic value (t = -9.078), df is the degrees of freedom (df= 9), p-value is the significance level of the t-test (p-value = 7.95310^{-6}). conf.int is the confidence interval of the mean at 95% (conf.int = [17.8172, 20.6828]); sample estimates is he mean value of the sample (mean = 19.25).

if you want to test whether the mean weight of mice is less than 25g (one-tailed test), type this: t.test(my_data\(weight, mu = 25, alternative = "less") Or, if you want to test whether the mean weight of mice is greater than 25g (one-tailed test), type this: t.test(my_data\)weight, mu = 25, alternative = “greater”)

Interpretaiton of result

The p-value of the test is 7.95310^{-6}, which is less than the significance level alpha = 0.05. We can conclude that the mean weight of the mice is significantly different from 25g with a p-value = 7.95310^{-6}.

Access to the values returned by t.test() function

statistic: the value of the t test statistics parameter: the degrees of freedom for the t test statistics p.value: the p-value for the test conf.int: a confidence interval for the mean appropriate to the specified alternative hypothesis. estimate: the means of the two groups being compared (in the case of independent t test) or difference in means (in the case of paired t test).

# printing the p-value
res$p.value
## [1] 7.953383e-06

Printing the mean

# printing the mean
res$estimate
## mean of x 
##     19.25
# printing the confidence interval
res$conf.int
## [1] 17.8172 20.6828
## attr(,"conf.level")
## [1] 0.95