6.3 Further examples of contrasts illustrated with a factor with four levels

In order to understand repeated difference and polynomial contrasts, it may be instructive to consider an experiment with one between-subject factor with four levels. We load a corresponding data set, which contains simulated data about response times with a four-level between-subject factor. The sample sizes for each level and the means and standard errors are shown in Table 6.3, and the means and standard errors are also shown graphically in Figure 6.2.

## [1] 20
Means and error bars (showing standard errors) for a simulated data-set with one between-subjects factor with four levels.

FIGURE 6.2: Means and error bars (showing standard errors) for a simulated data-set with one between-subjects factor with four levels.

TABLE 6.3: Summary statistics per condition for the simulated data.
Factor N data Est. means Std. dev. Std. errors
F1 5 10 10 4.5
F2 5 20 10 4.5
F3 5 10 10 4.5
F4 5 40 10 4.5

We assume that the four factor levels F1 to F4 reflect levels of word frequency, including the levels low, medium-low, medium-high, and high frequency words, and that the dependent variable reflects some response time.4

6.3.1 Repeated contrasts

A popular contrast psychologists and psycholinguists are interested in is the comparison between neighboring levels of a factor. This type of contrast is called repeated contrast. In our example, our research question might be whether the frequency level leads to slower response times than frequency level , whether frequency level leads to slower response times than frequency level , and whether frequency level leads to slower response times than frequency level .

Repeated contrasts are used to implement these comparisons. Consider first how to derive the contrast matrix for repeated contrasts, starting out by specifying the hypotheses that are to be tested about the data. Importantly, this again applies the general strategy of how to translate (any) hypotheses about differences between groups or conditions into a set of contrasts, yielding a powerful tool of great value in many research settings. We follow the four-step procedure outlined above.

The first step is to specify our comparisons or hypotheses, and to write them down in a way such that their weights can be extracted easily. For a four-level factor, the three hypotheses are:

\[\begin{equation} H_{0_{2-1}}: -1 \cdot \mu_1 + 1 \cdot \mu_2 + 0 \cdot \mu_3 + 0 \cdot \mu_4 = 0 \end{equation}\]

\[\begin{equation} H_{0_{3-2}}: 0 \cdot \mu_1 - 1 \cdot \mu_2 + 1 \cdot \mu_3 + 0 \cdot \mu_4 = 0 \end{equation}\]

\[\begin{equation} H_{0_{4-3}}: 0 \cdot \mu_1 + 0 \cdot \mu_2 - 1 \cdot \mu_3 + 1 \cdot \mu_4 = 0 \end{equation}\]

Here, the \(\mu_x\) are the mean response times in condition \(x\). Each hypothesis gives weights to the different condition means. The first hypothesis (\(H_{0_{2-1}}\)) tests the difference between condition mean for F2 (\(\mu_2\)) minus the condition mean for F1 (\(\mu_1\)), but ignores condition means for F3 and F4 (\(\mu_3\), \(\mu_4\)). \(\mu_1\) has a weight of \(-1\), \(\mu_2\) has a weight of \(+1\), and \(\mu_3\) and \(\mu_4\) have weights of \(0\). As the second step, the vector of weights for the first hypothesis is extracted as c2vs1 <- c(F1=-1,F2=+1,F3=0,F4=0). Next, the same thing is done for the other hypotheses - the weights for all hypotheses are extracted and coded into a hypothesis matrix in R:

##    c2vs1 c3vs2 c4vs3
## F1    -1     0     0
## F2     1    -1     0
## F3     0     1    -1
## F4     0     0     1

Again, we show the transposed version of the hypothesis matrix (switching rows and columns), but now we leave out the hypothesis for the intercept (below, we discuss when this can be ignored).

Next, the new contrast matrix XcRE is obtained. This is the contrast matrix \(X_c\) that exactly tests the hypotheses written down above:

##    c2vs1 c3vs2 c4vs3
## F1 -3/4  -1/2  -1/4 
## F2  1/4  -1/2  -1/4 
## F3  1/4   1/2  -1/4 
## F4  1/4   1/2   3/4

In the case of the repeated contrast, the contrast matrix again looks very different from the hypothesis matrix. In this case, the contrast matrix looks a lot less intuitive than the hypothesis matrix, and if one did not know the associated hypothesis matrix, it seems unclear what the contrast matrix would actually test. To verify this custom-made contrast matrix, we compare it to the repeated contrast matrix as generated by the R function contr.sdif() in the package (Ripley 2019). The resulting contrast matrix is identical to our result:

##   2-1  3-2  4-3 
## 1 -3/4 -1/2 -1/4
## 2  1/4 -1/2 -1/4
## 3  1/4  1/2 -1/4
## 4  1/4  1/2  3/4

Step four in the procedure is to apply repeated contrasts to the factor F in the example data, and to run a linear model. This allows us to estimate the regression coefficients associated with each contrast. These are compared to the data in Figure 6.2 to test whether the regression coefficients actually correspond to the differences between successive condition means, as intended.

##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)       20        2.2     8.9      0.0
## Fc2vs1            10        6.3     1.6      0.1
## Fc3vs2           -10        6.3    -1.6      0.1
## Fc4vs3            30        6.3     4.7      0.0

The results show that, as expected, the regression coefficients reflect the differences that were of interest: the regression coefficient (‘Estimate’) Fc2vs1 has a value of \(10\), which exactly corresponds to the difference between the condition mean for F2 (\(20\)) minus the condition mean for F1 (\(10\)), i.e., \(20 - 10 = 10\). Likewise, the regression coefficient Fc3vs2 has a value of \(-10\), which corresponds to the difference between the condition mean for F3 (\(10\)) minus the condition mean for F2 (\(20\)), i.e., \(10 - 20 = -10\). Finally, the regression coefficient Fc4vs3 has a value of roughly \(30\), which reflects the difference between condition F4 (\(40\)) minus condition F3 (\(10\)), i.e., \(40 - 10 = 30\). Thus, the regression coefficients estimate differences between successive or neighboring condition means, and test the corresponding null hypotheses.

Again, we can perform the same computations automatically using the hypr package. For reasons of brevity, we here only show the relevant hypr command: hypr(b1=F1~F2, b2=F2~F3, b3=F3~F4), and leave it to the reader to investigate this more closely.

6.3.2 Contrasts in linear regression analysis: The design or model matrix

We have now discussed how different contrasts are created from the hypothesis matrix. However, we have not treated in detail how exactly contrasts are used in a linear model. Here, we will see that the contrasts for a factor in a linear model are just the same thing as continuous numeric predictors (i.e., covariates) in a linear/multiple regression analysis. That is, contrasts are the way to encode discrete factor levels into numeric predictor variables to use in linear/multiple regression analysis, by encoding which differences between factor levels are tested. The contrast matrix \(X_c\) that we have looked at so far has one entry (row) for each experimental condition. For use in a linear model, however, the contrast matrix is coded into a design or model matrix \(X\), where each individual data point has one row. The design matrix \(X\) can be extracted using the function model.matrix():

##    c2vs1 c3vs2 c4vs3
## F1 -3/4  -1/2  -1/4 
## F2  1/4  -1/2  -1/4 
## F3  1/4   1/2  -1/4 
## F4  1/4   1/2   3/4
##    (Intercept) Fc2vs1 Fc3vs2 Fc4vs3
## 1            1  -0.75   -0.5  -0.25
## 2            1  -0.75   -0.5  -0.25
## 3            1  -0.75   -0.5  -0.25
## 4            1  -0.75   -0.5  -0.25
## 5            1  -0.75   -0.5  -0.25
## 6            1   0.25   -0.5  -0.25
## 7            1   0.25   -0.5  -0.25
## 8            1   0.25   -0.5  -0.25
## 9            1   0.25   -0.5  -0.25
## 10           1   0.25   -0.5  -0.25
## 11           1   0.25    0.5  -0.25
## 12           1   0.25    0.5  -0.25
## 13           1   0.25    0.5  -0.25
## 14           1   0.25    0.5  -0.25
## 15           1   0.25    0.5  -0.25
## 16           1   0.25    0.5   0.75
## 17           1   0.25    0.5   0.75
## 18           1   0.25    0.5   0.75
## 19           1   0.25    0.5   0.75
## 20           1   0.25    0.5   0.75

For each of the \(20\) subjects, four numbers are stored in this model matrix. They represent the three values of three predictor variables used to predict response times in the task. Indeed, this matrix is exactly the design matrix \(X\) commonly used in multiple regression analysis, where each column represents one numeric predictor variable (covariate), and the first column codes the intercept term.

To further illustrate this, the covariates are extracted from this design matrix and stored separately as numeric predictor variables in the data-frame:

They are now used as numeric predictor variables in a multiple regression analysis:

##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)       20        2.2     8.9      0.0
## Fc2vs1            10        6.3     1.6      0.1
## Fc3vs2           -10        6.3    -1.6      0.1
## Fc4vs3            30        6.3     4.7      0.0

The results show that the regression coefficients are the same as in the contrast-based analysis shown in the previous section. This demonstrates that contrasts serve to code discrete factor levels into a linear/multiple regression analysis by numerically encoding comparisons between specific condition means.

6.3.3 Polynomial contrasts

Polynomial contrasts are another option for analyzing factors. Suppose that we expect a linear trend across conditions, where the response increases by a constant magnitude with each successive factor level. This could be the expectation when four levels of a factor reflect decreasing levels of word frequency (i.e., four factor levels: high, medium-high, medium-low, and low word frequency), where one expects the lowest response for high frequency words, and successively higher responses for lower word frequencies. The effect for each individual level of a factor may not be strong enough for detecting it in the statistical model. Specifying a linear trend in a polynomial constrast allows us to pool the whole increase into a single coefficient for the linear trend, increasing statistical power to detect the increase. Such a specification constrains the estimate to one interpretable parameter, e.g., a linear increase across factor levels. The larger the number of factor levels, the more parsimonious are polynomial contrasts compared to contrast-based specifications as introduced in the previous sections. Going beyond a linear trend, one may also have expectations about quadratic trends. For example, one may expect an increase only among very low frequency words, but no difference between high and medium-high frequency words.

##           .L   .Q      .C
## [1,] -0.6708  0.5 -0.2236
## [2,] -0.2236 -0.5  0.6708
## [3,]  0.2236 -0.5 -0.6708
## [4,]  0.6708  0.5  0.2236
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)     20.0        2.2     8.9        0
## F.L             17.9        4.5     4.0        0
## F.Q             10.0        4.5     2.2        0
## F.C             13.4        4.5     3.0        0

In this example, condition means increase across factor levels in a linear fashion, but there may also be quadratic and cubic trends.

References

Ripley, Brian. 2019. MASS: Support Functions and Datasets for Venables and Ripley’s Mass. https://CRAN.R-project.org/package=MASS.


  1. Qualitatively, the simulated pattern of results is quite similar to the empirically observed values for word frequency effects on single fixation durations (Heister, Würzner, and Kliegl 2012).