Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting predicted values at response scale using broom::augment function

I'm fitting glm model in R and can get predicted values at response scale using predict.glm(object=fm1, type="response") where fm1 is the fitted model. I wonder how to get predicted values at response scale using augment function from broom package. My minimum working example is given below.

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
like image 525
MYaseen208 Avatar asked Dec 31 '15 20:12

MYaseen208


People also ask

What does the augment function do in R?

augment : add columns to the original data that was modeled. This includes predictions, residuals, and cluster assignments. glance : construct a concise one-row summary of the model. This typically contains values such as R^2, adjusted R^2, and residual standard error that are computed once for the entire model.

What is type response in predict () R?

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.

What does augment mean in R?

augment: Augment data according to a tidied model Given an R statistical model or other non-tidy object, add columns to the original dataset such as predictions, residuals and cluster assignments.


1 Answers

For generalized linear model, in order for the math to come out, the model needs to be transformed using a link function. For Gaussian model, this is the identity function, but for logistic regression, we use a logit function (can also be probit, does that ring a bell?). This means that you can get "raw" predicted values or transformed. This is why ?predict.glm offers a type argument, which translates to type.predict in augment.

broom::augment(x=fm1, newdata = Data, type.predict = "response")
like image 69
Roman Luštrik Avatar answered Sep 21 '22 07:09

Roman Luštrik