title: How to define contrast coefficient matrix?
body: <p>I have this data </p>
  
  <pre><code>y x1 x2 pre
  1  16  1  1  14
  2  15  1  1  13
  3  14  1  2  14
  4  13  1  2  13
  5  12  2  1  12
  6  11  2  1  12
  7  11  2  2  13
  8  13  2  2  13
  9  10  3  1  10
  10 11  3  1  11
  11 11  3  2  11
  12  9  3  2  10
  </code></pre>
  
  <p>And I fitted the following model</p>
  
  <pre><code>lm(y ~ x1 + x2 + x1*x2)
  </code></pre>
  
  <p>My design matrix is</p>
  
  <pre><code>  [,1] [,2] [,3] [,4] [,5] [,6] [,7]
   [1,]    1   14    1    0    1    1    0
   [2,]    1   13    1    0    1    1    0
   [3,]    1   14    1    0    0    0    0
   [4,]    1   13    1    0    0    0    0
   [5,]    1   12    0    1    1    0    1
   [6,]    1   12    0    1    1    0    1
   [7,]    1   13    0    1    0    0    0
   [8,]    1   13    0    1    0    0    0
   [9,]    1   10    0    0    1    0    0
  [10,]    1   11    0    0    1    0    0
  [11,]    1   11    0    0    0    0    0
  [12,]    1   10    0    0    0    0    0
  </code></pre>
  
  <p>I'm trying to use this design to reproduce the following table:</p>
  
  <pre><code>Source DF Squares Mean Square F Value Pr &gt; F
  Model 6 44.79166667 7.46527778 12.98 0.0064
  Error 5 2.87500000 0.57500000
  Corrected Total 11 47.66666667
  
  Source DF Type III SS Mean Square F Value Pr &gt; F
  pre 1 3.12500000 3.12500000 5.43 0.0671
  x1 2 4.58064516 2.29032258 3.98 0.0923
  x2 1 3.01785714 3.01785714 5.25 0.0706
  x1*x2 2 1.25000000 0.62500000 1.09 0.4055
  </code></pre>
  
  <p>The first part is fine</p>
  
  <pre><code>XtX &lt;- t(x) %*% x
  XtXinv &lt;- solve(XtX)
  betahat &lt;- XtXinv %*% t(x) %*% y
  
  H &lt;- x %*% XtXinv %*% t(x) 
  IH &lt;- (diag(1,12) - H)
  yhat &lt;- H %*% y 
  e &lt;- IH %*% y
  ybar &lt;- mean(y)
  
  MSS &lt;- t(betahat) %*% t(x) %*% y - length(y)*(ybar^2) 
  ESS &lt;- t(e) %*% e 
  TSS &lt;- MSS + ESS 
  
  dfM &lt;- sum(diag(H)) - 1 
  dfE &lt;- sum(diag(IH)) 
  dfT &lt;- dfM + dfE 
  
  MSM &lt;- MSS/dfM 
  MSE &lt;- ESS/dfE 
  
  Ftest &lt;- MSM / MSE
  pr &lt;- 1 - pf(Ftest, dfM, dfE)
  </code></pre>
  
  <p>The contrast coefficient matrix for 'pre' seems correct. </p>
  
  <pre><code>L &lt;- matrix(c(0,1,0,0,0,0,0), 1, 7, byrow=T)
  Lb &lt;- L %*% betahat 
  LXtXinvLt &lt;- round(L %*% XtXinv %*% t(L), digits=4) 
  SSpre &lt;- t(Lb) %*% solve(LXtXinvLt) %*% (Lb) 
  MSpre &lt;- SSpre / 1 
  Fpre &lt;- MSpre / MSE 
  PRpre &lt;- 1 - pf(Fpre, 1, 12-7)
  </code></pre>
  
  <p>But I can't understand how to define the contrast coefficient matrix for x1, x2, and x1*x2. What's the problem with the rest of my code? Below an example for how I think I should calculate for x1</p>
  
  <pre><code>L &lt;- matrix(c(0,0,1,1,0,0,0), 1, 7, byrow=T)
  Lb &lt;- L %*% betahat 
  LXtXinvLt &lt;- round(L %*% XtXinv %*% t(L), digits=4) 
  SSX1 &lt;- t(Lb) %*% solve(LXtXinvLt) %*% (Lb) 
  MSX1 &lt;- SSX1 / 1 
  FX1 &lt;- MSX1 / MSE 
  PRX1 &lt;- 1 - pf(FX1, 1, 12-7) 
  </code></pre>
  
  <p>Thanks!</p>