Expand covariates in multi-state dataset in long format
Source:R/expand.covs.R
expand.covs.msdata.Rd
Given a multi-state dataset in long format, and one or more covariates, this function adds transition-specific covariates, expanding the original covariate(s), to the dataset. The original dataset with the transition-specific covariates appended is returned.
Usage
# S3 method for class 'msdata'
expand.covs(data, covs, append = TRUE, longnames = TRUE, ...)
Arguments
- data
An object of class
"msdata"
, such as output bymsprep
- covs
A character vector containing the names of the covariates in
data
to be expanded- append
Logical value indicating whether or not the design matrix for the expanded covariates should be appended to the data (default=
TRUE
)- longnames
Logical value indicating whether or not the labels are to be used for the names of the expanded covariates that are categorical (default=
TRUE
); in case ofFALSE
numbers from 1 up to the number of contrasts are used- ...
Further arguments to be passed to or from other methods. They are ignored in this function.
Value
An object of class 'msdata', containing the design matrix for the
transition- specific covariates, either on its own
(append
=FALSE
) or appended to the data
(append
=TRUE
).
Details
For a given basic covariate Z
, the transition-specific covariate for
transition s
is called Z.s
. The concept of transition-specific
covariates in the context of multi-state models was introduced by Andersen,
Hansen & Keiding (1991), see also Putter, Fiocco & Geskus (2007). It is only
unambiguously defined for numeric covariates or for explicit codings. Then
it will take the value 0 for all rows in the long format dataframe for which
trans
does not equal s
. For the rows for which trans
equals s
, the original value of Z
is copied. In
expand.covs
, when a given covariate is a factor, it will be expanded
on the design matrix given by
model.matrix
. Missing values in the basic
covariates are allowed and result in missing values in the expanded
covariates.
References
Andersen PK, Hansen LS, Keiding N (1991). Non- and semi-parametric estimation of transition probabilities from censored observation of a non-homogeneous Markov process. Scandinavian Journal of Statistics 18, 153–167.
Putter H, Fiocco M, Geskus RB (2007). Tutorial in biostatistics: Competing risks and multi-state models. Statistics in Medicine 26, 2389–2430.
Author
Hein Putter H.Putter@lumc.nl
Examples
# transition matrix for illness-death model
tmat <- trans.illdeath()
# small data set in wide format
tg <- data.frame(illt=c(1,1,6,6,8,9),ills=c(1,0,1,1,0,1),
dt=c(5,1,9,7,8,12),ds=c(1,1,1,1,1,1),
x1=c(1,1,1,2,2,2),x2=c(6:1))
tg$x1 <- factor(tg$x1,labels=c("male","female"))
# data in long format using msprep
tglong <- msprep(time=c(NA,"illt","dt"),
status=c(NA,"ills","ds"),data=tg,
keep=c("x1","x2"),trans=tmat)
# expanded covariates
expand.covs(tglong,c("x1","x2"),append=FALSE)
#> x1female.1 x1female.2 x1female.3 x2.1 x2.2 x2.3
#> 1 0 0 0 6 0 0
#> 2 0 0 0 0 6 0
#> 3 0 0 0 0 0 6
#> 4 0 0 0 5 0 0
#> 5 0 0 0 0 5 0
#> 6 0 0 0 4 0 0
#> 7 0 0 0 0 4 0
#> 8 0 0 0 0 0 4
#> 9 1 0 0 3 0 0
#> 10 0 1 0 0 3 0
#> 11 0 0 1 0 0 3
#> 12 1 0 0 2 0 0
#> 13 0 1 0 0 2 0
#> 14 1 0 0 1 0 0
#> 15 0 1 0 0 1 0
#> 16 0 0 1 0 0 1
expand.covs(tglong,"x1")
#> An object of class 'msdata'
#>
#> Data:
#> id from to trans Tstart Tstop time status x1 x2 x1female.1 x1female.2
#> 1 1 1 2 1 0 1 1 1 male 6 0 0
#> 2 1 1 3 2 0 1 1 0 male 6 0 0
#> 3 1 2 3 3 1 5 4 1 male 6 0 0
#> 4 2 1 2 1 0 1 1 0 male 5 0 0
#> 5 2 1 3 2 0 1 1 1 male 5 0 0
#> 6 3 1 2 1 0 6 6 1 male 4 0 0
#> 7 3 1 3 2 0 6 6 0 male 4 0 0
#> 8 3 2 3 3 6 9 3 1 male 4 0 0
#> 9 4 1 2 1 0 6 6 1 female 3 1 0
#> 10 4 1 3 2 0 6 6 0 female 3 0 1
#> 11 4 2 3 3 6 7 1 1 female 3 0 0
#> 12 5 1 2 1 0 8 8 0 female 2 1 0
#> 13 5 1 3 2 0 8 8 1 female 2 0 1
#> 14 6 1 2 1 0 9 9 1 female 1 1 0
#> 15 6 1 3 2 0 9 9 0 female 1 0 1
#> 16 6 2 3 3 9 12 3 1 female 1 0 0
#> x1female.3
#> 1 0
#> 2 0
#> 3 0
#> 4 0
#> 5 0
#> 6 0
#> 7 0
#> 8 0
#> 9 0
#> 10 0
#> 11 1
#> 12 0
#> 13 0
#> 14 0
#> 15 0
#> 16 1
expand.covs(tglong,"x1",longnames=FALSE)
#> An object of class 'msdata'
#>
#> Data:
#> id from to trans Tstart Tstop time status x1 x2 x1.1 x1.2 x1.3
#> 1 1 1 2 1 0 1 1 1 male 6 0 0 0
#> 2 1 1 3 2 0 1 1 0 male 6 0 0 0
#> 3 1 2 3 3 1 5 4 1 male 6 0 0 0
#> 4 2 1 2 1 0 1 1 0 male 5 0 0 0
#> 5 2 1 3 2 0 1 1 1 male 5 0 0 0
#> 6 3 1 2 1 0 6 6 1 male 4 0 0 0
#> 7 3 1 3 2 0 6 6 0 male 4 0 0 0
#> 8 3 2 3 3 6 9 3 1 male 4 0 0 0
#> 9 4 1 2 1 0 6 6 1 female 3 1 0 0
#> 10 4 1 3 2 0 6 6 0 female 3 0 1 0
#> 11 4 2 3 3 6 7 1 1 female 3 0 0 1
#> 12 5 1 2 1 0 8 8 0 female 2 1 0 0
#> 13 5 1 3 2 0 8 8 1 female 2 0 1 0
#> 14 6 1 2 1 0 9 9 1 female 1 1 0 0
#> 15 6 1 3 2 0 9 9 0 female 1 0 1 0
#> 16 6 2 3 3 9 12 3 1 female 1 0 0 1