使用broom :: augment函数以响应比例获取预测值

时间:2015-12-31 20:05:15

标签: r statistics glm predict broom

我在glm中拟合R模型,并且可以使用predict.glm(object=fm1, type="response")获得响应比例的预测值,其中fm1是拟合模型。我想知道如何使用augment包中的broom函数在响应规模上获得预测值。我的最低工作示例如下。

Dilution <- c(1/128, 1/64, 1/32, 1/16, 1/8, 1/4, 1/2, 1, 2, 4)
NoofPlates <- rep(x=5, times=10)
NoPositive <- c(0, 0, 2, 2, 3, 4, 5, 5, 5, 5)
Data <- data.frame(Dilution,  NoofPlates, NoPositive)


fm1 <- glm(formula=NoPositive/NoofPlates~log(Dilution),
           family=binomial("logit"), data=Data, weights=NoofPlates)
predict.glm(object=fm1, type="response")
# 1          2          3          4          5          6          7          8          9         10 
# 0.02415120 0.07081045 0.19005716 0.41946465 0.68990944 0.87262421 0.95474066 0.98483820 0.99502511 0.99837891 

library(broom)
broom::augment(x=fm1)

# NoPositive.NoofPlates log.Dilution. X.weights.    .fitted   .se.fit     .resid       .hat    .sigma
# 1                    0.0    -4.8520303          5 -3.6989736 1.1629494 -0.4944454 0.15937234 0.6483053
# 2                    0.0    -4.1588831          5 -2.5743062 0.8837030 -0.8569861 0.25691194 0.5662637
# 3                    0.4    -3.4657359          5 -1.4496388 0.6404560  1.0845988 0.31570923 0.4650405
# 4                    0.4    -2.7725887          5 -0.3249714 0.4901128 -0.0884021 0.29247321 0.6784308
# 5                    0.6    -2.0794415          5  0.7996960 0.5205868 -0.4249900 0.28989252 0.6523116
# 6                    0.8    -1.3862944          5  1.9243633 0.7089318 -0.4551979 0.27931425 0.6486704
# 7                    1.0    -0.6931472          5  3.0490307 0.9669186  0.6805552 0.20199632 0.6155754
# 8                    1.0     0.0000000          5  4.1736981 1.2522190  0.3908698 0.11707018 0.6611557
# 9                    1.0     0.6931472          5  5.2983655 1.5498215  0.2233227 0.05944982 0.6739965
# 10                   1.0     1.3862944          5  6.4230329 1.8538108  0.1273738 0.02781019 0.6778365
# .cooksd .std.resid
# 1  0.0139540988 -0.5392827
# 2  0.0886414317 -0.9941540
# 3  0.4826245827  1.3111391
# 4  0.0022725303 -0.1050972
# 5  0.0543073747 -0.5043322
# 6  0.0637954916 -0.5362006
# 7  0.0375920888  0.7618349
# 8  0.0057798939  0.4159767
# 9  0.0008399932  0.2302724
# 10 0.0001194412  0.1291827

1 个答案:

答案 0 :(得分:16)

对于广义线性模型,为了使数学出来,需要使用link function来转换模型。对于高斯模型,这是身份函数,但对于逻辑回归,我们使用logit函数(也可以是probit,是否响铃?)。这意味着你可以得到&#34; raw&#34;预测值或转换。这就是?predict.glm提供type参数的原因,该参数转换为type.predict中的augment

broom::augment(x=fm1, newdata = Data, type.predict = "response")