This article is part of Quantide’s web book “Raccoon – Statistical Models with R“. Raccoon is Quantide’s third web book after “Rabbit – Introduction to R” and “Ramarro – R for Developers“. See the full project here.

The second chapter of Racooon is focused on T-test and Anova. Through example it shows theory and R code of:

This post is the first section of the chapter, about 1-sample t-test. 

Throughout the web-book we will widely use the package qdata, containing about 80 datasets. You may find it here:


Ch. 2.1 – T-test, Anova: 1-sample t-test

Example: Reaction temperature

Data description

A chemical theory suggests that the temperature at which a certain chemical reaction occurs is 180 °C. reaction contains the results of 10 independent measurements.

Data loading

Let us load and have a look at the data


First of all, let us calculate the main descriptive statistics on reaction:

Sample mean and median are close to the hypothesized value of 180, and the data point values are close to 180 too.
Let us plot data (with collection time if meaningful) and add, with geom_hline() a reference line for temperature of 180 °C (red) and for the mean temperature (179.36 °C, green)


Plot of reaction values

The plot shows the temperature (y-axis) on the index of observations (x-axis). Our objective is to verify if the distance between the two lines (red and green) may be due to chance or if it is unlikely from a statistical point of view. Remember that the main objective of a statistical test is not to accept the null hypothesis, but to find if enough evidence appear to refuse it.
Now let us plot data without time (no index) by building a stripchart, which directly shows the points and is a good alternative to the boxplot:


Stripchart of reaction values

Inference and models

One-sample Student’s \(t\)-test

Standard instruments, as one-sample Student’s \(t\)-test, do not use linear models at appearance. Is it true?

First let us check if the normality assumption of the \(t\)-test are plausible: let us check if data does not depart too much from normality by using the R function ad.test of the nortest require:

Anderson-Darling’s test tests the normality vs. the non-normality of data distribution. The p-value of the test is fully larger than 0.05, the usual standard for the first type α“>α error probability: therefore there is no evidence of non-normality.
The Normal Probability Plot confirms the Anderson-Darling’s test result:



Normal probability plot of reaction

The plot compares the observed quantiles with the corresponding quantiles of a standard Normal distribution.
If data points come from a Normal distribution, then they will tendto follow a straight line.
In this case this plot is “nice”.
Now, let us test the hypothesis \(H_0\) that the “true mean is equal to 180 °C”.

We cannot refuse \((H_0)\) since the p-value is 0.2876.

Linear models

How can we think to an alternative which uses a model? Since normality assumption seems satisfied, we could fit a linear model with reaction as response variable and the only intercept as parameter (the model will then report only the mean):

In above line of code, the formula argument of lm function gives the “structure” of the relation between dependent and independent variables: the ~ symbol separates the dependent variable from the model formulation, and the reaction ~ 1 formula means that the dependent variable reaction is modeled by the intercept term only (the 1) , or, equivalently in this case, by the mean.
The output of linear model mod is
By applying the summary() R function to lm-type objects we get more information:
The very small p-value (\(<2^{-16}\)) tells us that the intercept is not 0; also, the intercept estimate (i.e., the mean) is close to 180.
or, equivalently:
If the null hypothesis is true, then reaction-180 has 0 mean.
The output of the linear model fm is
The estimated coefficient of the model, in this case the mean of reaction minus 180, is -0.51, indeed
The p-value, 0.288, is the same of the \(t\)-test: in this case it means that the intercept is not significantly different from 0 (that is the mean of original data can be 180).
This particularly simple regression model is then equivalent to one-sample \(t\)-test.
Let us check for normality of residuals, even if in this particular case is redundant, since it is equivalent to check the normality of reaction.

Normal probability plot of residuals

Indeed, the p-value of Anderson-Darling’s test is the same as before. The q-q plot also confirms that the normality assumption is plausible.