Exercises

The function lm can be used to fit a simple, two group linear model. The test statistic from a linear model is equivalent to the test statistic we get when we perform a t-test with the equal variance assumption. Though the linear model in this case is equivalent to a t-test, we will soon explore more complicated designs, where the linear model is a useful extension (confounding variables, testing contrasts of terms, testing interactions, testing many terms at once, etc.).

Here we will review the mathematics on why these produce the same test statistic and therefore p-value.

We already know that the numerator of the t-statistic in both cases is the difference between the average of the groups, so we only have to see that the denominator is the same. Of course, it makes sense that the denominator should be the same, since we are calculating the standard error of the same quantity (the difference) under the same assumptions (equal variance), but here we will show equivalence of the formula.

In the linear model, we saw how to calculate this standard error using the design matrix and the estimate of from the residuals. The estimate of was the sum of squared residuals divided by , where is the total number of samples and is the number of terms (an intercept and a group indicator, so here ).

In the t-test, the denominator of the t-value is the standard error of the difference. The t-test formula for the standard error of the difference, if we assume equal variance in the two groups, is the square root of the variance:

Here is the number of samples in the first group and is the number of samples in the second group.

If we look carefully, the second part of this equation is the sum of squared residuals, divided by .

All that is left to show is that the entry in the second row, second column of is

  1. You can make a design matrix X for a two group comparison, either using model.matrix or simply with:

     X <- cbind(rep(1,Nx + Ny),rep(c(0,1),c(Nx, Ny)))
    

    In order to compare two groups, where the first group has Nx=5 samples and the second group has Ny=7 samples, what is the element in the 1st row and 1st column of $$X^\top X$?

  2. The other entries of are all the same. What is this number?

Now we just need to invert the matrix to obtain . The formula for matrix inversion for a 2x2 matrix is actually relatively easy to memorize:

The element of the inverse in the 2nd row and the 2nd column is the element which will be used to calculate the standard error of the second coefficient of the linear model. This is . And for our two group comparison, we saw that and the . So it follows that this element is:

which simplifies to: