9

Suppose I want to predict the response to a specific value of explanatory variables. But I didn't understand why I used type="response" or "terms" or "link".

Sorif Hossain
  • 1,231
  • 1
  • 11
  • 18
  • 3
    Please see [How to ask a good question](https://stackoverflow.com/help/how-to-ask) and [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Rui Barradas Nov 25 '17 at 13:45
  • This link might help,it adresses the same question https://stats.stackexchange.com/questions/448403/how-do-you-interpret-prediction-output-in-gbm-in-r-for-classification-problem – maleckicoa Sep 08 '20 at 12:32

1 Answers1

13

Assuming you are talking about GLM, you should first understand how the model is constructed and how it relates to the dependent variable. This is an extensive topic, worthy of full lectures at a university. My suggestion would be to pick up a book and start there.

In short and oversimplified, in order for the math to come out, you need to wrap the y into some function so that on the right side of the equation you get a "nice", e.g. f(y) = beta_0 + beta_1 * X1 + beta_2 * X2 + e type formula.

ldose <- rep(0:5, 2)
numdead <- c(1, 4, 9, 13, 18, 20, 0, 2, 6, 10, 12, 16)
sex <- factor(rep(c("M", "F"), c(6, 6)))
SF <- cbind(numdead, numalive = 20-numdead)

budworm.lg <- glm(SF ~ sex*ldose, family = binomial)

Now, when you ask the predict to return type = link, you get values of f(y).

predict(budworm.lg, type = "link")
         1          2          3          4          5          6 
-2.8185550 -1.5596055 -0.3006561  0.9582933  2.2172427  3.4761922 
         7          8          9         10         11         12 
-2.9935418 -2.0875053 -1.1814689 -0.2754324  0.6306040  1.5366404 

type = response will resolve this term so that it is on the "natural" scale.

predict(budworm.lg, type = "response")
         1          2          3          4          5          6 
0.05632970 0.17370326 0.42539710 0.72277997 0.90178726 0.97000272 
         7          8          9         10         11         12 
0.04771849 0.11031718 0.23478819 0.43157393 0.65262640 0.82297581

The type = terms will return a matrix given fit of each observation on a linear scale.

predict(budworm.lg, type = "terms")

           sex      ldose   sex:ldose
1   0.08749339 -2.2650911 -0.44114124
2   0.08749339 -1.3590547 -0.08822825
3   0.08749339 -0.4530182  0.26468474
4   0.08749339  0.4530182  0.61759773
5   0.08749339  1.3590547  0.97051072
6   0.08749339  2.2650911  1.32342371
7  -0.08749339 -2.2650911 -0.44114124
8  -0.08749339 -1.3590547 -0.44114124
9  -0.08749339 -0.4530182 -0.44114124
10 -0.08749339  0.4530182 -0.44114124
11 -0.08749339  1.3590547 -0.44114124
12 -0.08749339  2.2650911 -0.44114124
attr(,"constant")
[1] -0.199816
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197