[R] predict.glm -> which class does it predict?

Marc Schwartz marc_schwartz at me.com
Fri Jul 10 17:37:13 CEST 2009


On Jul 10, 2009, at 9:46 AM, Peter Schüffler wrote:

> Hi,
>
> I have a question about logistic regression in R.
>
> Suppose I have a small list of proteins P1, P2, P3 that predict a  
> two-class target T, say cancer/noncancer. Lets further say I know  
> that I can build a simple logistic regression model in R
>
> model <- glm(T ~ ., data=d.f(Y), family=binomial)   (Y is the  
> dataset of the Proteins).
>
> This works fine. T is a factored vector with levels cancer,  
> noncancer. Proteins are numeric.
>
> Now, I want to use predict.glm to predict a new data.
>
> predict(model, newdata=testsamples, type="response")    (testsamples  
> is a small set of new samples).
>
> The result is a vector of the probabilites for each sample in  
> testsamples. But probabilty WHAT for? To belong to the first level  
> in T? To belong to second level in T?
>
> Is this fallowing expression
> factor(predict(model, newdata=testsamples, type="response") >= 0.5)
> TRUE, when the new sample is classified to Cancer or when it's  
> classified to Noncancer? And why not the other way around?
>
> Thank you,
>
> Peter

As per the Details section of ?glm:

A typical predictor has the form response ~ terms where response is  
the (numeric) response vector and terms is a series of terms which  
specifies a linear predictor forresponse. ***For binomial and  
quasibinomial families the response can also be specified as a factor  
(when the first level denotes failure and all others success)*** or as  
a two-column matrix with the columns giving the numbers of successes  
and failures. A terms specification of the form first + second  
indicates all the terms in first together with all the terms in second  
with any duplicates removed.


So, given your description above, you are predicting  
"noncancer"...that is, you are predicting the probability of the  
second level of the factor ("success"), given the covariates.

If you want to predict "cancer", alter the factor levels thusly:

   T <- factor(T, levels = c("noncancer", "cancer"))

By default, R will alpha sort the factor levels, so "cancer" would be  
first.

Think of it in terms of using a 0,1 integer code for absence,presence,  
where you are predicting the probability of a '1', or the presence of  
the event or characteristic of interest.

BTW, using 'T' as the name of the response vector is not a good habit:

 > T
[1] TRUE

'T' is shorthand for the built in R constant TRUE. R is generally  
smart enough to know the difference, but it is better to avoid getting  
into trouble by not using it.

HTH,

Marc Schwartz




More information about the R-help mailing list