R: Functions to facilitate analysis of multistate models.
stack.Lexis
R Documentation
Functions to facilitate analysis of multistate models.
Description
stack.Lexis produces a stacked object suited for analysis of
several transition intensities simultaneously.
Usage
## S3 method for class 'Lexis'
stack(x, ...)
tmat( x, ... )
## S3 method for class 'Lexis'
tmat(x, Y=FALSE, mode = "numeric", ...)
Arguments
x
A Lexis object.
Y
Logical. Should the risk time be put in the diagonal? This is
a facility which is used by boxes.Lexis.
mode
Should the matrix be returned as a numeric matrix with
NAs at unused places or (mode="logical") as a logical
matrix with FALSE on the diagonal.
...
Not used.
Value
tmat.Lexis returns a square transition matrix, classified by the
levels of lex.Cst and lex.Xst, for every transition
occurring the entry is the number of transitions occurring and NA
in all oter entries. If Y=TRUE, the diagonal will contain the
risk time in each of the states.
stack.Lexis returns a dataframe to be used for analysis of
multistate data when all transitions are modelled together, for example
if some parameters are required to be the same for different transitions.
The dataframe has class stacked.Lexis, and inherits the
attributes time.scales and breaks from the Lexis
object, and so function timeBand applies to a
stacked.Lexis object too.
The dataframe has same variables as the original Lexis object,
but with each record duplicated as many times as there are possible
exits from the current state, lex.Cst. Two variables are added:
lex.Fail, an indicator of wheter an event for the transition
named in the factor lex.Tr has occurred or not. lex.Tr is
a factor with levels made up of combinations of the levels of
lex.Cst and lex.Xst that do occur together in x,
joined by a "->".
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(Epi)
Attaching package: 'Epi'
The following object is masked from 'package:base':
merge.data.frame
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/Epi/stack.Lexis.Rd_%03d_medium.png", width=480, height=480)
> ### Name: stack.Lexis
> ### Title: Functions to facilitate analysis of multistate models.
> ### Aliases: stack.Lexis tmat tmat.Lexis
> ### Keywords: survival
>
> ### ** Examples
>
> data(DMlate)
> str(DMlate)
'data.frame': 10000 obs. of 7 variables:
$ sex : Factor w/ 2 levels "M","F": 2 1 2 2 1 2 1 1 2 1 ...
$ dobth: num 1940 1939 1918 1965 1933 ...
$ dodm : num 1999 2003 2005 2009 2009 ...
$ dodth: num NA NA NA NA NA ...
$ dooad: num NA 2007 NA NA NA ...
$ doins: num NA NA NA NA NA NA NA NA NA NA ...
$ dox : num 2010 2010 2010 2010 2010 ...
> dml <- Lexis( entry=list(Per=dodm, Age=dodm-dobth, DMdur=0 ),
+ exit=list(Per=dox),
+ exit.status=factor(!is.na(dodth),labels=c("DM","Dead")),
+ data=DMlate )
NOTE: entry.status has been set to "DM" for all.
Warning message:
In Lexis(entry = list(Per = dodm, Age = dodm - dobth, DMdur = 0), :
Dropping 4 rows with duration of follow up < tol
> dmi <- cutLexis( dml, cut=dml$doins, new.state="Ins", pre="DM" )
> summary( dmi )
Transitions:
To
From DM Ins Dead Records: Events: Risk time: Persons:
DM 6157 1694 2048 9899 3742 45885.49 9899
Ins 0 1340 451 1791 451 8387.77 1791
Sum 6157 3034 2499 11690 4193 54273.27 9996
> ls.dmi <- stack( dmi )
> str( ls.dmi )
Classes 'stacked.Lexis' and 'data.frame': 21589 obs. of 16 variables:
$ Per : num 1999 2003 2005 2009 2009 ...
$ Age : num 58.7 64.1 86.3 44 75.8 ...
$ DMdur : num 0 0 0 0 0 0 0 0 0 0 ...
$ lex.dur : num 11.08 6.689 5.446 0.736 1.344 ...
$ lex.Cst : Factor w/ 3 levels "DM","Ins","Dead": 1 1 1 1 1 1 1 1 1 1 ...
$ lex.Xst : Factor w/ 3 levels "DM","Ins","Dead": 1 1 1 1 1 3 1 1 3 1 ...
$ lex.Tr : Factor w/ 3 levels "DM->Ins","DM->Dead",..: 1 1 1 1 1 1 1 1 1 1 ...
$ lex.Fail: logi FALSE FALSE FALSE FALSE FALSE FALSE ...
$ lex.id : int 1 2 3 4 5 6 7 8 9 10 ...
$ sex : Factor w/ 2 levels "M","F": 2 1 2 2 1 2 1 1 2 1 ...
$ dobth : num 1940 1939 1918 1965 1933 ...
$ dodm : num 1999 2003 2005 2009 2009 ...
$ dodth : num NA NA NA NA NA ...
$ dooad : num NA 2007 NA NA NA ...
$ doins : num NA NA NA NA NA NA NA NA NA NA ...
$ dox : num 2010 2010 2010 2010 2010 ...
- attr(*, "breaks")=List of 3
..$ Per : NULL
..$ Age : NULL
..$ DMdur: NULL
- attr(*, "time.scales")= chr "Per" "Age" "DMdur"
> # Check that all the transitions and person-years got across.
> with( ls.dmi, rbind( table(lex.Fail,lex.Tr),
+ tapply(lex.dur,lex.Tr,sum) ) )
DM->Ins DM->Dead Ins->Dead
FALSE 8205.00 7851.00 1340.000
TRUE 1694.00 2048.00 451.000
45885.49 45885.49 8387.773
>
>
>
>
>
> dev.off()
null device
1
>