[R] Why is model.matrix creating 2 columns for boolean?

Ross Boylan ross at biostat.ucsf.edu
Mon Nov 19 19:50:17 CET 2007

On Sat, 2007-11-17 at 09:51 -0800, Ben Bolker wrote:
> Ross Boylan wrote:
> > 
> > I have a data frame "reading" that includes a logical variable "OLT"
> > along with response variable "Reading" and predictor "True" (BOTH are
> > numeric variables; it's "True" as in the true value).
> > 
> > When I suppress the intercept, model.matrix gives me OLTTRUE and
> > OLTFALSE columns.  Why?  Can I do anything to prevent it?
> > 
> > 
>  I guess I don't understand the question -- this seems to be the
> right behavior ... if you fitted the model Reading~OLT-1, you
> need two coefficients, one to predict the value of cases where
> OLT=FALSE and one to predict the value where OLT=TRUE.
> You can parameterize the model as (value when FALSE,
> difference between FALSE and TRUE) or (value when TRUE,
> difference between TRUE and FALSE) or (value when TRUE,
> value when FALSE) -- but however you do it you'll need two
> variables in the model matrix -- right?  Adding a continuous
> predictor shouldn't matter.
What I want is is a model that fits one coefficient.  When OLT is false,
it predicts 0; when OLT is true, it predicts the value of the

I'm computing a likelihood from 2 separate design matrices and some
other terms that don't appear in the formulae at all (those "other
terms" are the intercepts in the sense appropriate for the model).

I thought the -1 trick might work by analogy with the behavior for
continuous predictors.
>  If you don't want the extra column you can always drop
> it with mm[,-1] ...
Thanks.  That's what I did.
>   Ben Bolker
Ross Boylan                                      wk:  (415) 514-8146
185 Berry St #5700                               ross at biostat.ucsf.edu
Dept of Epidemiology and Biostatistics           fax: (415) 514-8150
University of California, San Francisco
San Francisco, CA 94107-1739                     hm:  (415) 550-1062

More information about the R-help mailing list