[R] probability values ?

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Tue Jun 17 13:41:56 CEST 2003



orkun wrote:
> Hello
> 
> I try to find probability values of some predictor combinations using 
> logistic reg. in response level.
> Firstly I found coefficients by glm function.
> Then I followed two ways to get probability values:
> 1- probility <- exp(X0+bX1+cX2+...)/((1+exp(X0+bX1+cX2+...))
> 2- probility <- predict(glm.obj,type="resp")
> 
> Should have these two given same result ?
> if so, I did not have. Why ?
> 
> Does anyone have any idea ?
> 

This works for me. Are you sure you're getting the correct linear 
predictor in (1). Here's an example:

R> x = glm(y ~ trt + I(week > 2), data = bacteria, family = binomial)
R> str(predict(x, type = "resp"))
  Named num [1:220] 0.944 0.944 0.823 0.823 0.900 ...
  - attr(*, "names")= chr [1:220] "1" "2" "3" "4" ...
R> coef(x)
     (Intercept)         trtdrug        trtdrug+ I(week > 2)TRUE
       2.8332455      -1.1186847      -0.6372255      -1.2948522
R> lp = model.matrix(x) %*% coef(x)
R> str(exp(lp)/(1 + exp(lp)))
  num [1:220, 1] 0.944 0.944 0.823 0.823 0.900 ...
  - attr(*, "dimnames")=List of 2
   ..$ : chr [1:220] "1" "2" "3" "4" ...
   ..$ : NULL
R>




More information about the R-help mailing list