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?> r <- model.matrix(Reading~0+OLT, reading) > r[1:5,]OLTFALSE OLTTRUE 1 1 0 2 1 0 3 1 0 4 1 0 5 1 0> reading$OLT[1:2][1] FALSE FALSE> r <- model.matrix(Reading~OLT, reading) > r[1:5,](Intercept) OLTTRUE 1 1 0 2 1 0 3 1 0 4 1 0 5 1 0> reading[1:5,]Spec Reader Reading True OLT 1 1 18 1 1 FALSE 2 2 18 1 1 FALSE 3 3 18 1 1 FALSE 4 4 18 4 4 FALSE 5 5 18 4 4 FALSE R 2.5.1-2 on Debian GNU/Linux, running inside ESS. -- 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
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. If you don't want the extra column you can always drop it with mm[,-1] ... Ben Bolker -- View this message in context: http://www.nabble.com/Why-is-model.matrix-creating-2-columns-for-boolean--tf4817540.html#a13811570 Sent from the R help mailing list archive at Nabble.com.