2017-11-25 61 views
1

假設我想預測對解釋變量的特定值的響應。但我不明白我爲什麼使用type =「response」或「terms」或「link」。預測函數中type =「response」,「terms」和「link」有什麼區別?

+3

請參見[如何問一個很好的問題(https://stackoverflow.com/help/how-to-ask)和[最小的,完整的和可驗證的示例](https://開頭計算器。 com/help/mcve)和[如何製作一個很好的R可重現的例子](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。 –

回答

3

假設你在談論GLM,你應該首先了解模型是如何構造的以及它如何與因變量相關。這是一個廣泛的話題,值得在大學進行全面的講座。我的建議是拿起一本書並從那裏開始。

簡而言之,爲了使數學出來,您需要將y包裝到某個函數中,以便在等式的右邊得到一個「很好」的例如, 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) 

現在,當你問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 

響應將解決此術語,以便它處於「自然」等級。

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 

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 
相關問題