R: Create a data structures suitable for use with packages...
foreign.Lexis
R Documentation
Create a data structures suitable for use with packages mstate, etm.
Description
The mstate package requires input in the form of a stacked
dataset with specific variable names. This is provided by
msdata.Lexis. The resulting dataframe contains the same
information as the result of a call to stack.Lexis.
The etm package requires input (almost) in the form of a
Lexis object, but with specific column names etc. This is
provided by etm.Lexis.
Usage
msdata(obj, ...)
## S3 method for class 'Lexis'
msdata(obj,
time.scale = timeScales(obj)[1],
... )
## S3 method for class 'Lexis'
etm( obj,
time.scale = timeScales(obj)[1],
cens.name = "cens",
s = 0,
t = "last",
covariance = TRUE,
delta.na = TRUE,
... )
Arguments
obj
A Lexis object.
time.scale
Name or number of timescale in the Lexis
object.
cens.name
Name of the code for censoring used by etm. It
is only necessary to change this if one of the states in the
Lexis object has name "cens".
s
Passed on to etm.
t
Passed on to etm.
covariance
Passed on to etm.
delta.na
Passed on to etm.
...
Further arguments.
Value
msdata.Lexis returns a dataframe with the Lexis specific
variables stripped, and with the following added: id,
Tstart, Tstop, from, to, trans,
status, which are used in the mstate package.
etm.Lexis transforms the Lexis object into a dataframe
suitable for analysis by the function etm from the etm
package, and actually calls this function, so returns an object of
class etm.
data(DMlate)
str(DMlate)
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[1:1000,] )
dmi <- cutLexis( dml, cut=dml$doins, new.state="Ins", pre="DM" )
summary( dmi )
# Use the interface to the mstate package
if( require(mstate) )
{
ms.dmi <- msdata.Lexis( dmi )
# Check that all the transitions and person-years got across.
with( ms.dmi, rbind( table(status,trans),
tapply(Tstop-Tstart,trans,sum) ) )
}
# Use the etm package directly with a Lexis object
if( require(etm) )
{
dmi <- subset(dmi,lex.id<1000)
etm.D <- etm.Lexis( dmi, time.scale=3 )
plot( etm.D, col=rainbow(5), lwd=2, lty=1, xlab="DM duration" )
}
Results
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/foreign.Lexis.Rd_%03d_medium.png", width=480, height=480)
> ### Name: foreign.Lexis
> ### Title: Create a data structures suitable for use with packages mstate,
> ### etm.
> ### Aliases: msdata msdata.Lexis etm etm.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[1:1000,] )
NOTE: entry.status has been set to "DM" for all.
> 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 616 178 195 989 373 4454.40 989
Ins 0 142 47 189 47 854.16 189
Sum 616 320 242 1178 420 5308.56 1000
>
> # Use the interface to the mstate package
> if( require(mstate) )
+ {
+ ms.dmi <- msdata.Lexis( dmi )
+ # Check that all the transitions and person-years got across.
+ with( ms.dmi, rbind( table(status,trans),
+ tapply(Tstop-Tstart,trans,sum) ) )
+ }
Loading required package: mstate
Loading required package: survival
1 2 3
0 811.000 794.000 142.0000
1 178.000 195.000 47.0000
4454.401 4454.401 854.1602
>
> # Use the etm package directly with a Lexis object
> if( require(etm) )
+ {
+ dmi <- subset(dmi,lex.id<1000)
+ etm.D <- etm.Lexis( dmi, time.scale=3 )
+ plot( etm.D, col=rainbow(5), lwd=2, lty=1, xlab="DM duration" )
+ }
Loading required package: etm
Attaching package: 'etm'
The following object is masked from 'package:Epi':
etm
>
>
>
>
>
> dev.off()
null device
1
>