Bug 16029 - drop.terms does not preserve the predvars attribute
Summary: drop.terms does not preserve the predvars attribute
Alias: None
Product: R
Classification: Unclassified
Component: Models (show other bugs)
Version: R-devel (trunk)
Hardware: Other Linux
: P5 normal
Assignee: R-core
Depends on:
Reported: 2014-10-16 14:57 UTC by Terry Therneau
Modified: 2014-11-13 15:22 UTC (History)
1 user (show)

See Also:


Note You need to log in before you can comment on or make changes to this bug.
Description Terry Therneau 2014-10-16 14:57:07 UTC
> test <- model.frame(Employed ~ Year + poly(GNP,3) + Population, data=longley)
> mterm <- terms(test)

> names(attributes(mterm))
 [1] "variables"    "factors"      "term.labels"  "order"        "intercept"   
 [6] "response"     "class"        ".Environment" "predvars"     "dataClasses" 

> names(attributes(drop.terms(mterm,3)))
[1] "variables"    "factors"      "term.labels"  "order"        "intercept"   
[6] "response"     "class"        ".Environment"

Why do I care?  In model.matrix.coxph any cluster() term is removed from the terms before calling model.matrix.default; this loses the predvars for terms that I keep.  Ditto for strata() terms, since they could possibly generate thousands of columns that I don't need, e.g., matched case-control data.

model.matrix.coxph currently has a simple workaround which could be copied.  I don't know what other modeling routines may use specials in their formula that will not be a part of the model matrix.
Comment 1 Duncan Murdoch 2014-11-05 18:02:24 UTC
The docs in ?terms.object don't mention a "predvars" component, so that might be why it was dropped.  However, various other functions make use of it, so it really should be kept, and should be documented.

Can you write a couple of lines about what the "predvars" component is supposed to contain?
Comment 2 Duncan Murdoch 2014-11-07 19:30:11 UTC
I'll fix this for predvars and dataClasses in drop.terms and [.terms in R-devel, and eventually port to R-patched if this doesn't cause worse problems.
Comment 3 Duncan Murdoch 2014-11-13 15:22:31 UTC
Now ported to R-patched as well.