title: What does predict.glm(, type="terms") actually do?
body: <p>I am confused with the way <strong>predict.glm</strong> function in R works.
  According to the help, </p>
  
  <blockquote>
    <p>The "terms" option returns a matrix giving the fitted values of each term in the model formula on the linear predictor scale. </p>
  </blockquote>
  
  <p>Thus, if my model has form f(y) = X*beta, then command</p>
  
  <pre><code>predict(model, X, type='terms')
  </code></pre>
  
  <p>is expected to produce the same matrix X, multiplied by beta element-wise. For example, if I train the following model</p>
  
  <pre><code>test.data = data.frame(y = c(0,0,0,1,1,1,1,1,1), x=c(1,2,3,1,2,2,3,3,3))
  model = glm(y~(x==1)+(x==2), family = 'binomial', data = test.data)
  </code></pre>
  
  <p>the resulting coefficients are</p>
  
  <pre><code>beta &lt;- model$coef
  </code></pre>
  
  <p>Design matrix is</p>
  
  <pre><code>X &lt;- model.matrix(y~(x==1)+(x==2), data = test.data)
  
    (Intercept) x == 1TRUE x == 2TRUE
  1           1          1          0
  2           1          0          1
  3           1          0          0
  4           1          1          0
  5           1          0          1
  6           1          0          1
  7           1          0          0
  8           1          0          0
  9           1          0          0
  </code></pre>
  
  <p>Then multiplied by coefficients it should look like</p>
  
  <pre><code>pred1 &lt;- t(beta * t(X))
  
    (Intercept) x == 1TRUE x == 2TRUE
  1    1.098612  -1.098612  0.0000000
  2    1.098612   0.000000 -0.4054651
  3    1.098612   0.000000  0.0000000
  4    1.098612  -1.098612  0.0000000
  5    1.098612   0.000000 -0.4054651
  6    1.098612   0.000000 -0.4054651
  7    1.098612   0.000000  0.0000000
  8    1.098612   0.000000  0.0000000
  9    1.098612   0.000000  0.0000000
  </code></pre>
  
  <p>However, actual matrix produced by <code>predict.glm</code> seems to be unrelated to this. The following code</p>
  
  <pre><code>pred2 &lt;- predict(model, test.data, type = 'terms')
  
        x == 1     x == 2
  1 -0.8544762  0.1351550
  2  0.2441361 -0.2703101
  3  0.2441361  0.1351550
  4 -0.8544762  0.1351550
  5  0.2441361 -0.2703101
  6  0.2441361 -0.2703101
  7  0.2441361  0.1351550
  8  0.2441361  0.1351550
  9  0.2441361  0.1351550
  attr(,"constant")
  [1] 0.7193212
  </code></pre>
  
  <p><strong>How does one interpret such results?</strong></p>