Skip to main content

\begin{equation} y \ =\ X\mu \ +\ \epsilon \ =\ X \ (C^{-1}C)\ \ \mu \ +\ \epsilon = \ (X C^{-1}) \ (C \mu) \ + \epsilon \end{equation}\begin{equation} y \ =\ X\mu \ +\ \epsilon \ =\ XI\mu \ +\ \epsilon \ =\ X \ (C^{-1}C)\ \ \mu \ +\ \epsilon = \ (X C^{-1}) \ (C \mu) \ + \epsilon \end{equation}

Therefore we can use the first term in parentheses to evaluate the second term (our comparisons), using the least squares method, just as we did for the original equation above. This is why we use the inverse of the contrast matrix C (it is invertible, and it needs to be square and full rank in this case).

We can define any type of contrast in this way, either using the built-in functions contr.treatment(), contr.sum() etc or by specifying which comparisons we want. For its contrasts arguments, lm() expects the inverse of C without the intercept column, solve(C)[,-1], and it adds the intercept column to generate $C^{-1}$, and uses it for the modified design matrix. There are many refinements on this scheme (orthogonal contrasts, more complex contrasts, not full rank design matrix etc), but this is the gist of it (cf also here for reference: https://cran.r-project.org/web/packages/codingMatrices/vignettes/codingMatrices.pdf).

\begin{equation} y \ =\ X\mu \ +\ \epsilon \ =\ X \ (C^{-1}C)\ \ \mu \ +\ \epsilon = \ (X C^{-1}) \ (C \mu) \ + \epsilon \end{equation}

Therefore we can use the first term in parentheses to evaluate the second term (our comparisons), using the least squares method, just as we did for the original equation above. This is why we use the inverse of the contrast matrix C (it is invertible in this case).

We can define any type of contrast in this way, either using the built-in functions contr.treatment(), contr.sum() etc or by specifying which comparisons we want. There are many refinements on this scheme (orthogonal contrasts, more complex contrasts, not full rank design matrix etc), but this is the gist of it (cf also here for reference: https://cran.r-project.org/web/packages/codingMatrices/vignettes/codingMatrices.pdf).

\begin{equation} y \ =\ X\mu \ +\ \epsilon \ =\ XI\mu \ +\ \epsilon \ =\ X \ (C^{-1}C)\ \ \mu \ +\ \epsilon = \ (X C^{-1}) \ (C \mu) \ + \epsilon \end{equation}

Therefore we can use the first term in parentheses to evaluate the second term (our comparisons), using the least squares method, just as we did for the original equation above. This is why we use the inverse of the contrast matrix C, and it needs to be square and full rank in this case.

We can define any type of contrast in this way, either using the built-in functions contr.treatment(), contr.sum() etc or by specifying which comparisons we want. For its contrasts arguments, lm() expects the inverse of C without the intercept column, solve(C)[,-1], and it adds the intercept column to generate $C^{-1}$, and uses it for the modified design matrix. There are many refinements on this scheme (orthogonal contrasts, more complex contrasts, not full rank design matrix etc), but this is the gist of it (cf also here for reference: https://cran.r-project.org/web/packages/codingMatrices/vignettes/codingMatrices.pdf).

added 116 characters in body
Source Link

And we evaluate: \begin{equation} C\mu \end{equation} using the method of least squares. The coefficients for this model can be evaluated as before using least squares, replacing the original design matrix by the new one. Or callingnaming $X_{1} = (X C^{-1})$ the modified design matrix: \begin{equation} \hat{C\mu} = (X_{1}^{'}X_{1})^{-1}X_{1}^{'}y=\\C\hat{\mu}= \begin{pmatrix} \hat{\mu1} \\\hat{\mu2}-\hat{\mu1} \\\hat{\mu3}-\hat{\mu1} \\\hat{\mu4}-\hat{\mu1} \end{pmatrix} \end{equation}

We can define any type of contrast in this way, either using the built-in functions contr.treatment(), contr.sum() etc or by specifying which comparisons we want. There are many refinements on this scheme (orthogonal contrasts, more complex contrasts, not full rank design matrix etc), but this is the gist of it (cf also here for reference: https://cran.r-project.org/web/packages/codingMatrices/vignettes/codingMatrices.pdf).

And we evaluate: \begin{equation} C\mu \end{equation} using the method of least squares. The coefficients for this model can be evaluated as before using least squares, replacing the original design matrix by the new one. Or calling $X_{1} = (X C^{-1})$ the modified design matrix: \begin{equation} \hat{C\mu} = (X_{1}^{'}X_{1})^{-1}X_{1}^{'}y=\\C\hat{\mu}= \begin{pmatrix} \hat{\mu1} \\\hat{\mu2}-\hat{\mu1} \\\hat{\mu3}-\hat{\mu1} \\\hat{\mu4}-\hat{\mu1} \end{pmatrix} \end{equation}

We can define any type of contrast in this way, either using the built-in functions contr.treatment(), contr.sum() etc or by specifying which comparisons we want. There are many refinements on this scheme (orthogonal contrasts, more complex contrasts, not full rank design matrix etc), but this is the gist of it.

And we evaluate: \begin{equation} C\mu \end{equation} using the method of least squares. The coefficients for this model can be evaluated as before using least squares, replacing the original design matrix by the new one. Or naming $X_{1} = (X C^{-1})$ the modified design matrix: \begin{equation} \hat{C\mu} = (X_{1}^{'}X_{1})^{-1}X_{1}^{'}y=\\C\hat{\mu}= \begin{pmatrix} \hat{\mu1} \\\hat{\mu2}-\hat{\mu1} \\\hat{\mu3}-\hat{\mu1} \\\hat{\mu4}-\hat{\mu1} \end{pmatrix} \end{equation}

We can define any type of contrast in this way, either using the built-in functions contr.treatment(), contr.sum() etc or by specifying which comparisons we want. There are many refinements on this scheme (orthogonal contrasts, more complex contrasts, not full rank design matrix etc), but this is the gist of it (cf also here for reference: https://cran.r-project.org/web/packages/codingMatrices/vignettes/codingMatrices.pdf).

deleted 302 characters in body
Source Link

And we evaluate: \begin{equation} C\mu \end{equation} using the method of least squares. The coefficients for this model can be evaluated as before using least squares, replacing the original design matrix by the new one. Or working it out in details, by replacing X and u bycalling $X_{1} = (X C^{-1})$ the new matricesmodified design matrix: \begin{equation} [(X C^{-1})'(X C^{-1})]^{-1}(X C^{-1})'y=[(C^{-1})' X'(XC^{-1})]^{-1}(X C^{-1})'y= (C^{-1})^{-1}(X'X)^{-1}[(C^{-1})']^{-1}(X C^{-1})'y= C(X'X)^{-1}[(C^{-1})^{-1}]'(XC^{-1})'y= C(X'X)^{-1}C'(C^{-1})'X'y=C(X'X)^{-1}C'(C')^{-1}X'y= C(X^{\prime }X)^{-1}\ X^{\prime }y=\\C\hat{\mu}= \begin{pmatrix} \hat{\mu1} \\\hat{\mu2}-\hat{\mu1} \\\hat{\mu3}-\hat{\mu1} \\\hat{\mu4}-\hat{\mu1} \end{pmatrix} \end{equation}\begin{equation} \hat{C\mu} = (X_{1}^{'}X_{1})^{-1}X_{1}^{'}y=\\C\hat{\mu}= \begin{pmatrix} \hat{\mu1} \\\hat{\mu2}-\hat{\mu1} \\\hat{\mu3}-\hat{\mu1} \\\hat{\mu4}-\hat{\mu1} \end{pmatrix} \end{equation}

(cf the least square equation above for the last step)

As expected from the model equation, using Using the modified design matrix (with the inverse of the contrast matrix) and the least squares method, we evaluate the desired constrasts. Of course, to get the original contrast matrix, we need to invert the contrast coding matrix used in R.

We can define any type of contrast in this way, either using the built-in functions contr.treatment(), contr.sum() etc or by specifying which comparisons we want. There are many refinements on this scheme (orthogonal contrasts, more complex contrasts, not full rank design matrix etc), but this is the gist of it.

And we evaluate: \begin{equation} C\mu \end{equation} using the method of least squares. The coefficients for this model can be evaluated as before using least squares, replacing the original design matrix by the new one. Or working it out in details, by replacing X and u by the new matrices: \begin{equation} [(X C^{-1})'(X C^{-1})]^{-1}(X C^{-1})'y=[(C^{-1})' X'(XC^{-1})]^{-1}(X C^{-1})'y= (C^{-1})^{-1}(X'X)^{-1}[(C^{-1})']^{-1}(X C^{-1})'y= C(X'X)^{-1}[(C^{-1})^{-1}]'(XC^{-1})'y= C(X'X)^{-1}C'(C^{-1})'X'y=C(X'X)^{-1}C'(C')^{-1}X'y= C(X^{\prime }X)^{-1}\ X^{\prime }y=\\C\hat{\mu}= \begin{pmatrix} \hat{\mu1} \\\hat{\mu2}-\hat{\mu1} \\\hat{\mu3}-\hat{\mu1} \\\hat{\mu4}-\hat{\mu1} \end{pmatrix} \end{equation}

(cf the least square equation above for the last step)

As expected from the model equation, using the modified design matrix (with the inverse of the contrast matrix) and the least squares method, we evaluate the desired constrasts. Of course, to get the original contrast matrix, we need to invert the contrast coding matrix used in R.

We can define any type of contrast in this way, either using the built-in functions contr.treatment(), contr.sum() etc or by specifying which comparisons we want. There are many refinements on this scheme (orthogonal contrasts, more complex contrasts etc), but this is the gist of it.

And we evaluate: \begin{equation} C\mu \end{equation} using the method of least squares. The coefficients for this model can be evaluated as before using least squares, replacing the original design matrix by the new one. Or calling $X_{1} = (X C^{-1})$ the modified design matrix: \begin{equation} \hat{C\mu} = (X_{1}^{'}X_{1})^{-1}X_{1}^{'}y=\\C\hat{\mu}= \begin{pmatrix} \hat{\mu1} \\\hat{\mu2}-\hat{\mu1} \\\hat{\mu3}-\hat{\mu1} \\\hat{\mu4}-\hat{\mu1} \end{pmatrix} \end{equation}

Using the modified design matrix (with the inverse of the contrast matrix) and the least squares method, we evaluate the desired constrasts. Of course, to get the original contrast matrix, we need to invert the contrast coding matrix used in R.

We can define any type of contrast in this way, either using the built-in functions contr.treatment(), contr.sum() etc or by specifying which comparisons we want. There are many refinements on this scheme (orthogonal contrasts, more complex contrasts, not full rank design matrix etc), but this is the gist of it.

deleted 98 characters in body
Source Link
Loading
deleted 7 characters in body
Source Link
Loading
added 15 characters in body
Source Link
Loading
edited body
Source Link
Loading
Source Link
Loading