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".
The type="response" option tells R to output probabilities of the form P(Y = 1|X) , as opposed to other information such as the logit . If no data set is supplied to the predict() function, then the probabilities are computed for the training data that was used to fit the logistic regression model.
The glm() function in R can be used to fit generalized linear models. This function is particularly useful for fitting logistic regression models, Poisson regression models, and other complex models. Once we've fit a model, we can then use the predict() function to predict the response value of a new observation.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With