Reformulate a terms object such that some specials are stripped off
strip.terms(
terms,
specials,
alias.names = NULL,
unspecials = NULL,
arguments,
keep.response = TRUE
)Terms object
Character vector of specials which should be stripped off
Optional. A named list with alias names for the specials.
Optional. A special name for treating all the unspecial terms.
A named list of arguments, one for each element
of specials. Elements are passed to parseSpecialNames.
Keep the response in the resulting object?
Reformulated terms object with an additional attribute which contains the stripped.specials.
This function is used to remove special specials, i.e., those which cannot or should not be evaluated. IMPORTANT: the unstripped terms need to know about all specials including the aliases. See examples.
parseSpecialNames reformulate drop.terms
## parse a survival formula and identify terms which
## should be treated as proportional or timevarying:
f <- Surv(time,status)~age+prop(factor(edema))+timevar(sex,test=0)+prop(bili,power=1)
tt <- terms(f,specials=c("prop","timevar"))
attr(tt,"specials")
#> $prop
#> [1] 3 5
#>
#> $timevar
#> [1] 4
#>
st <- strip.terms(tt,specials=c("prop","timevar"),arguments=NULL)
formula(st)
#> Surv(time, status) ~ age + factor(edema) + sex + bili
#> <environment: 0x5613bd03edc8>
attr(st,"specials")
#> $prop
#> NULL
#>
#> $timevar
#> NULL
#>
attr(st,"stripped.specials")
#> $prop
#> [1] 3 5
#>
#> $timevar
#> [1] 4
#>
## provide a default value for argument power of proportional treatment
## and argument test of timevarying treatment:
st2 <- strip.terms(tt,
specials=c("prop","timevar"),
arguments=list("prop"=list("power"=0),"timevar"=list("test"=0)))
formula(st2)
#> Surv(time, status) ~ age + factor(edema) + sex + bili
#> <environment: 0x5613bd03edc8>
attr(st2,"stripped.specials")
#> $prop
#> [1] 3 5
#>
#> $timevar
#> [1] 4
#>
attr(st2,"stripped.arguments")
#> $prop
#> $prop$`factor(edema)`
#> $prop$`factor(edema)`$power
#> [1] 0
#>
#>
#> $prop$bili
#> $prop$bili$power
#> [1] "1"
#>
#>
#>
#> $timevar
#> $timevar$sex
#> $timevar$sex$test
#> [1] "0"
#>
#>
#>
## treat all unspecial terms as proportional
st3 <- strip.terms(tt,
unspecials="prop",
specials=c("prop","timevar"),
arguments=list("prop"=list("power"=0),"timevar"=list("test"=0)))
formula(st3)
#> Surv(time, status) ~ age + factor(edema) + sex + bili
#> <environment: 0x5613bd03edc8>
attr(st3,"stripped.specials")
#> $prop
#> [1] 2 3 5
#>
#> $timevar
#> [1] 4
#>
attr(st3,"stripped.arguments")
#> $prop
#> $prop$age
#> $prop$age$power
#> [1] 0
#>
#>
#> $prop$`factor(edema)`
#> $prop$`factor(edema)`$power
#> [1] 0
#>
#>
#> $prop$bili
#> $prop$bili$power
#> [1] "1"
#>
#>
#>
#> $timevar
#> $timevar$sex
#> $timevar$sex$test
#> [1] "0"
#>
#>
#>
## allow alias names: strata for timevar and tp, const for prop.
## IMPORTANT: the unstripped terms need to know about
## all specials including the aliases
f <- Surv(time,status)~age+const(factor(edema))+strata(sex,test=0)+prop(bili,power=1)+tp(albumin)
tt2 <- terms(f,specials=c("prop","timevar","strata","tp","const"))
st4 <- strip.terms(tt2,
specials=c("prop","timevar"),
unspecials="prop",
alias.names=list("timevar"="strata","prop"=c("const","tp")),
arguments=list("prop"=list("power"=0),"timevar"=list("test"=0)))
formula(st4)
#> Surv(time, status) ~ age + factor(edema) + sex + bili + albumin
#> <environment: 0x5613bd03edc8>
attr(st4,"stripped.specials")
#> $prop
#> [1] 2 3 5 6
#>
#> $timevar
#> [1] 4
#>
attr(st4,"stripped.arguments")
#> $prop
#> $prop$age
#> $prop$age$power
#> [1] 0
#>
#>
#> $prop$`factor(edema)`
#> $prop$`factor(edema)`$power
#> [1] 0
#>
#>
#> $prop$bili
#> $prop$bili$power
#> [1] "1"
#>
#>
#> $prop$albumin
#> $prop$albumin$power
#> [1] 0
#>
#>
#>
#> $timevar
#> $timevar$sex
#> $timevar$sex$test
#> [1] "0"
#>
#>
#>
## test if alias works also without unspecial argument
st5 <- strip.terms(tt2,
specials=c("prop","timevar"),
alias.names=list("timevar"="strata","prop"=c("const","tp")),
arguments=list("prop"=list("power"=0),"timevar"=list("test"=0)))
formula(st5)
#> Surv(time, status) ~ age + factor(edema) + sex + bili + albumin
#> <environment: 0x5613bd03edc8>
attr(st5,"stripped.specials")
#> $prop
#> [1] 3 5 6
#>
#> $timevar
#> [1] 4
#>
attr(st5,"stripped.arguments")
#> $prop
#> $prop$`factor(edema)`
#> $prop$`factor(edema)`$power
#> [1] 0
#>
#>
#> $prop$bili
#> $prop$bili$power
#> [1] "1"
#>
#>
#> $prop$albumin
#> $prop$albumin$power
#> [1] 0
#>
#>
#>
#> $timevar
#> $timevar$sex
#> $timevar$sex$test
#> [1] "0"
#>
#>
#>
library(survival)
data(pbc)
model.design(st4,data=pbc[1:3,],specialsDesign=TRUE)
#> $design
#> NULL
#>
#> $prop
#> age factor(edema)0.5 factor(edema)1 bili albumin
#> 1 58.76523 0 1 14.5 2.60
#> 2 56.44627 0 0 1.1 4.14
#> 3 70.07255 1 0 1.4 3.48
#> attr(,"levels")
#> attr(,"levels")$`factor(edema)`
#> [1] "0" "0.5" "1"
#>
#> attr(,"arguments")
#> attr(,"arguments")$age
#> attr(,"arguments")$age$power
#> [1] 0
#>
#>
#> attr(,"arguments")$`factor(edema)`
#> attr(,"arguments")$`factor(edema)`$power
#> [1] 0
#>
#>
#> attr(,"arguments")$bili
#> attr(,"arguments")$bili$power
#> [1] "1"
#>
#>
#> attr(,"arguments")$albumin
#> attr(,"arguments")$albumin$power
#> [1] 0
#>
#>
#> attr(,"arguments.terms")
#> attr(,"arguments.terms")$power
#> age factor(edema)0 factor(edema)0.5 factor(edema)1
#> "0" "0" "0" "0"
#> bili albumin
#> "1" "0"
#>
#> attr(,"matrix.terms")
#> attr(,"matrix.terms")$age
#> [1] "age"
#>
#> attr(,"matrix.terms")$`factor(edema)`
#> [1] "factor(edema)0" "factor(edema)0.5" "factor(edema)1"
#>
#> attr(,"matrix.terms")$bili
#> [1] "bili"
#>
#> attr(,"matrix.terms")$albumin
#> [1] "albumin"
#>
#>
#> $timevar
#> sexf
#> 1 1
#> 2 1
#> 3 0
#> attr(,"levels")
#> attr(,"levels")$sex
#> [1] "m" "f"
#>
#> attr(,"arguments")
#> attr(,"arguments")$sex
#> attr(,"arguments")$sex$test
#> [1] "0"
#>
#>
#> attr(,"arguments.terms")
#> attr(,"arguments.terms")$test
#> sexm sexf
#> "0" "0"
#>
#> attr(,"matrix.terms")
#> attr(,"matrix.terms")$sex
#> [1] "sexm" "sexf"
#>
#>
model.design(st5,data=pbc[1:3,],specialsDesign=TRUE)
#> $design
#> (Intercept) age
#> 1 1 58.76523
#> 2 1 56.44627
#> 3 1 70.07255
#> attr(,"assign")
#> [1] 0 1
#> attr(,"levels")
#> named list()
#>
#> $prop
#> factor(edema)0.5 factor(edema)1 bili albumin
#> 1 0 1 14.5 2.60
#> 2 0 0 1.1 4.14
#> 3 1 0 1.4 3.48
#> attr(,"levels")
#> attr(,"levels")$`factor(edema)`
#> [1] "0" "0.5" "1"
#>
#> attr(,"arguments")
#> attr(,"arguments")$`factor(edema)`
#> attr(,"arguments")$`factor(edema)`$power
#> [1] 0
#>
#>
#> attr(,"arguments")$bili
#> attr(,"arguments")$bili$power
#> [1] "1"
#>
#>
#> attr(,"arguments")$albumin
#> attr(,"arguments")$albumin$power
#> [1] 0
#>
#>
#> attr(,"arguments.terms")
#> attr(,"arguments.terms")$power
#> factor(edema)0 factor(edema)0.5 factor(edema)1 bili
#> "0" "0" "0" "1"
#> albumin
#> "0"
#>
#> attr(,"matrix.terms")
#> attr(,"matrix.terms")$`factor(edema)`
#> [1] "factor(edema)0" "factor(edema)0.5" "factor(edema)1"
#>
#> attr(,"matrix.terms")$bili
#> [1] "bili"
#>
#> attr(,"matrix.terms")$albumin
#> [1] "albumin"
#>
#>
#> $timevar
#> sexf
#> 1 1
#> 2 1
#> 3 0
#> attr(,"levels")
#> attr(,"levels")$sex
#> [1] "m" "f"
#>
#> attr(,"arguments")
#> attr(,"arguments")$sex
#> attr(,"arguments")$sex$test
#> [1] "0"
#>
#>
#> attr(,"arguments.terms")
#> attr(,"arguments.terms")$test
#> sexm sexf
#> "0" "0"
#>
#> attr(,"matrix.terms")
#> attr(,"matrix.terms")$sex
#> [1] "sexm" "sexf"
#>
#>