[R] logistic regression using "glm",which "y" is set to be "1"

Marc Schwartz marc_schwartz at comcast.net
Thu Dec 6 19:11:53 CET 2007


On Wed, 2007-12-05 at 22:33 -0800, Bin Yue wrote:
> Dear all:
>      By comparing glmresult$y and model.response(model.frame(glmresult)),  I
> have found out which one is 
> set to be "TRUE" and which "FALSE".But it seems that to fit a logistic
> regression , logit (or logistic) transformation has to be done before
> regression.
>      Does anybody know how to obtain the transformation result ? It is hard
> to settle down before knowing the actual process R works . I have read some
> books and the "?glm" help file , but what they told me was not sufficient.
>    Best wishes ,
>  Bin Yue

Bin,

I may be mis-interpreting your follow up query, but here goes:

You have presumably created a logistic regression model. The resultant
model object is called 'glmresult'.

If you use:

  fitted(glmresult)

it will return the fitted predicted values on a probability scale (0 -
1) for the original set of data that you used.

You can also use:

  predict(glmresult, type = "response")

The advantage of using predict.glm() is that you can apply the model
against new data.


If you want the linear predicted values on a log-odds scale, you can
use:

  glmresult$linear.predictors

or more easily:

  predict(glmresult)

See ?fitted and ?predict.glm for more information.


Let's use an example from ?infert:

model1 <- glm(case ~ spontaneous+induced, data=infert,family=binomial())

# Summary of fitted values on a probability scale
> summary(fitted(model1))
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.1534  0.1534  0.2949  0.3347  0.3750  0.7511 


# Same
> summary(predict(model1, type = "response"))
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.1534  0.1534  0.2949  0.3347  0.3750  0.7511 


# Get log-odds scale values
> summary(model1$linear.predictors)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
-1.7080 -1.7080 -0.8716 -0.7781 -0.5107  1.1050


# Same
> summary(predict(model1))
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
-1.7080 -1.7080 -0.8716 -0.7781 -0.5107  1.1050


If we wanted to do the log-odds scale to probability scale transform
manually, we could do:

> summary(exp(predict(model1)) / (1 + exp(predict(model1))))
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.1534  0.1534  0.2949  0.3347  0.3750  0.7511 

Look familiar?

I would urge you to read through An Introduction To R, which is
available with your R installation or via the R web site under
Documentation. In addition, there are various books listed on the R web
site regarding model building and related subject matter. Which you
choose can be a matter of taste, but two I recommend would be:

William N. Venables and Brian D. Ripley. Modern Applied Statistics with
S. Fourth Edition. Springer, New York, 2002. ISBN 0-387-95457-0

Frank E. Harrell. Regression Modeling Strategies, with Applications to
Linear Models, Survival Analysis and Logistic Regression. Springer,
2001. ISBN 0-387-95232-2

HTH,

Marc Schwartz



More information about the R-help mailing list