Skip to content

Anomaly in [.terms

3 messages · Terry Therneau, Brian Ripley, William Dunlap

#
This arose when working on an addition to coxph, which has the features
that the X matrix never has an intercept column, and we remove strata()
terms before computing an X matrix.  The surprise: when a terms object
is subset the intercept attribute is turned back on.
  My lines 2 and 3 below were being executed just before a call to
model.frame.  The simple solution was of course to do them in the
opposite order so I am not waiting on a "fix". 
  Not to mention that I am not sure a fix is required, though I was
surprised. 
    Terry T.


tmt1131% R

R version 2.12.0 (2010-10-15)
Copyright (C) 2010 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-unknown-linux-gnu (64-bit)
+    specials='strata')
Surv(time, status) ~ age
attr(,"variables")
list(Surv(time, status), age)
attr(,"factors")
                   age
Surv(time, status)   0
age                  1
attr(,"term.labels")
[1] "age"
attr(,"specials")
attr(,"specials")$strata
NULL

attr(,"order")
[1] 1
attr(,"intercept")
[1] 1
attr(,"response")
[1] 1
#
\item \code{drop.terms} and the \code{[} method for class
       \code{"terms"} no longer add back an intercept.  (Reported by
       Niels Hansen.)

so it has been fixed, just not rolled out yet.
On Mon, 21 Feb 2011, Terry Therneau wrote:

            

  
    
#
I've wondered about how to deal with terms objects
whose formula and attributes disagreed with each
other.  In your case the intercept attribute was 0
but there was no -1 in the formula and you (and others)
complained that [.terms respected the formula and not
the attributes.  In another case you can set the response
attribute to 0 but leave the response term in the formula.
Then [.terms alters the formula to drop the response term.
  > t <- terms(y ~ x1 + x2)
  > attr(t, "response")
  [1] 1
  > attr(t, "response") <- 0
  > t[1]
  ~x1
  attr(,"variables")
  list(x1)
  attr(,"factors")
     x1
  x1  1
  attr(,"term.labels")
  [1] "x1"
  attr(,"order")
  [1] 1
  attr(,"intercept")
  [1] 1
  attr(,"response")
  [1] 0
  attr(,".Environment")
  <environment: R_GlobalEnv>
  > version$version.string
  [1] "R version 2.12.1 (2010-12-16)"
Is altering the formula to match the attributes desirable?

I suspect you would be displeased if [.terms added
a -1 to the formula if the intercept term were 0.

If you have a terms object without a response in
the formula and you set the response attribute to
1 then [.terms just gets mixed up.  Altering most
other attributes of a terms object risks confusing
lots of functions.

Should R have a function to set the intercept term
to a legal value so we could say that directly altering
the attributes of a terms object should never be done?

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com