预测函数中的type =“ response”,“ terms”和“ link”有什么区别?

SOR*_*JON 4 r

假设我要预测对解释变量的特定值的响应。但是我不明白为什么要使用type =“ response”或“ terms”或“ link”。

Rom*_*rik 5

假设您正在谈论GLM,首先应该了解模型的构造方式以及它与因变量的关系。这是一个广泛的话题,值得在一所大学进行完整的讲座。我的建议是拿起一本书,从那里开始。

简而言之,为了简化数学运算,您需要将y函数包装到某个函数中,以便在方程式的右侧获得“ nice”,例如f(y) = beta_0 + beta_1 * X1 + beta_2 * X2 + e类型公式。

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)
Run Code Online (Sandbox Code Playgroud)

现在,当您要求predict返回时type = link,您将获得的值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 
Run Code Online (Sandbox Code Playgroud)

响应将解决这个问题,使它达到“自然”的程度。

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
Run Code Online (Sandbox Code Playgroud)

type = terms将返回给定的每一个观察配合上的线性标尺的矩阵。

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
Run Code Online (Sandbox Code Playgroud)