# Question about sliding differences contrast coding rationale

I've been reading up on sliding differences coding (forward differences coding, on the ucla page: http://www.ats.ucla.edu/stat/r/library/contrast_coding.htm).

Here is the contrast matrix recreated below:

      A vs B   B vs C   C vs D
A      3/4      1/2      1/4
B     -1/4      1/2      1/4
C     -1/4     -1/2      1/4
D     -1/4     -1/2     -3/4


I don't quite understand why the comparisons are as they are. If I saw this matrix, I would have assumed at first glance that the first comparison is between A and the mean of B,C,D; the second comparison is between mean of A and B compared to the mean of C and D; etc.

Can someone explain this to me?

Thanks!

You are looking at the Moore-Penrose pseudoinverse (a.k.a. generalized inverse) of the contrast matrix you are interested in, rather than the contrast matrix itself. For the sliding contrast matrix in this case:

$$\begin{bmatrix} 1 & -1 & 0 & 0 \\ 0 & 1 & -1 & 0 \\ 0 & 0 & 1 & -1 \\ \end{bmatrix}$$

The pseudoinverse, as acquired in for example Matlab:

>a = [1 -1 0 0; 0 1 -1 0; 0 0 1 -1];
>pinv(a)

ans =

0.7500    0.5000    0.2500
-0.2500    0.5000    0.2500
-0.2500   -0.5000    0.2500
-0.2500   -0.5000   -0.7500


Is the matrix shown in this infamous UCLA page.

R requires the input of the pseudoinverse into its contrasts method. It will generate this automatically for you, for example if you enter

> contr.sdif(4)


R will output the above matrix

    2-1  3-2   4-3
1 -0.75 -0.5 -0.25
2  0.25 -0.5 -0.25
3  0.25  0.5 -0.25
4  0.25  0.5  0.75


while calling the generalized inverse on this function output will produce the contrast matrix you are expecting:

> library(MASS); fractions(ginv(contr.sdif(4)))
[,1] [,2] [,3] [,4]
[1,] -1    1    0    0
[2,]  0   -1    1    0
[3,]  0    0   -1    1


I had a similar question (see here), to which I was able to answer after reading this source. To save time re-typing my whole answer, I suggest you look into the link of my answer to my question. In short, the answer is that the matrix you are referring to defines the transformation from betas to means (rather than means to betas, which is how you interpret it by mistake). Therefore, the right way to read it is that the mean of A is the summation of b0 (not in the matrix), 3/4 b1, 1/2 b2 and 1/4 b3. To see how each beta codes for a contrast of means, you need to solve this set of equations for each beta, i.e. get the inverse.