Bug 17169 - Error in [.terms when there is an offset
Summary: Error in [.terms when there is an offset
Status: UNCONFIRMED
Alias: None
Product: R
Classification: Unclassified
Component: Models (show other bugs)
Version: R-devel (trunk)
Hardware: Other Linux
: P5 normal
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2016-10-24 16:54 UTC by Terry Therneau
Modified: 2016-10-24 16:54 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Terry Therneau 2016-10-24 16:54:32 UTC
I subscript the terms object in the survival library's Cox model code to manipulate strata and cluster terms.

A user (D James) sent a bug report that survfit.coxph was ignoring offset terms in a particular case, and it turns out to be an issue with terms.   Here is an example:

> test2 <- terms(~age + offset(weight) + strata(sex), specials="strata")
> attr(test2, 'specials')
$strata
[1] 3

> test2b <- test2[-attr(test2, 'specials')$strata]
> attr(test2b, 'variables')
list(age, strata(sex))

The issue is in line 2 of [.terms, in library/stats/R/models.R

    newformula <- attr(termobj, "term.labels")[i]

The term.labels attribute omits the offset.  One possible fix is 
    newformula <- (rownames(attr(termobj, "factors"))[i]

but I have not thought it through with any depth.