Niels Richard Hansen
2010-Aug-26 07:17 UTC
[R] Problem with terms of the form (a >1) and subsetting of a terms object
I have the following problem mydata <- data.frame(a=1:3) form <- ~ (a>1) - 1 model.matrix(form,mydata) a > 1FALSE a > 1TRUE 1 1 0 2 0 1 3 0 1 ... However model.matrix(update(terms(form)[1],~.-1),mydata) (Intercept) a > 1 - 1TRUE 1 1 1 2 1 1 3 1 1 ... Taking terms(form)[1] extracts the formula with only the first term, but the parentheses are thrown away, and after the update, I get a different formula. Is that supposed to be so? Thanks, Niels -- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: Niels.R.Hansen at math.ku.dk Department of Mathematical Sciences nielsrichardhansen at gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +45 353 20783 (office) 2100 Copenhagen ? +45 2859 0765 (mobile) Denmark
Dimitris Rizopoulos
2010-Aug-26 07:30 UTC
[R] Problem with terms of the form (a >1) and subsetting of a terms object
I think you need an I(), i.e., form <- ~ I(a > 1) - 1 I hope it helps. Best, Dimitris On 8/26/2010 9:17 AM, Niels Richard Hansen wrote:> I have the following problem > > mydata <- data.frame(a=1:3) > form <- ~ (a>1) - 1 > model.matrix(form,mydata) > > a > 1FALSE a > 1TRUE > 1 1 0 > 2 0 1 > 3 0 1 > ... > > However > > model.matrix(update(terms(form)[1],~.-1),mydata) > > (Intercept) a > 1 - 1TRUE > 1 1 1 > 2 1 1 > 3 1 1 > ... > > Taking > > terms(form)[1] > > extracts the formula with only the first term, but the parentheses > are thrown away, and after the update, I get a different formula. > Is that supposed to be so? > > Thanks, Niels >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
Niels Richard Hansen
2010-Aug-26 08:35 UTC
[R] Problem with terms of the form (a >1) and subsetting of a terms object
On 26/08/10 09.30, Dimitris Rizopoulos wrote:> I think you need an I(), i.e., > > form <- ~ I(a > 1) - 1Yes, it solves the concrete problem, but does not really answer the question. Let me rephrase. Should the use of terms like (a>1) be discouraged in R and replaced by I(a>1) systematically, or is the behavior below unexpected? Best, Niels> > > I hope it helps. > > Best, > Dimitris > > > On 8/26/2010 9:17 AM, Niels Richard Hansen wrote: >> I have the following problem >> >> mydata <- data.frame(a=1:3) >> form <- ~ (a>1) - 1 >> model.matrix(form,mydata) >> >> a > 1FALSE a > 1TRUE >> 1 1 0 >> 2 0 1 >> 3 0 1 >> ... >> >> However >> >> model.matrix(update(terms(form)[1],~.-1),mydata) >> >> (Intercept) a > 1 - 1TRUE >> 1 1 1 >> 2 1 1 >> 3 1 1 >> ... >> >> Taking >> >> terms(form)[1] >> >> extracts the formula with only the first term, but the parentheses >> are thrown away, and after the update, I get a different formula. >> Is that supposed to be so? >> >> Thanks, Niels >> >-- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: Niels.R.Hansen at math.ku.dk Department of Mathematical Sciences nielsrichardhansen at gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +45 353 20783 (office) 2100 Copenhagen ? +45 2859 0765 (mobile) Denmark