reshapeIDM: Wide to Long Format For Illness Death Model

View source: R/riskIDM.R

reshapeIDMR Documentation

Wide to Long Format For Illness Death Model

Description

Convert a dataset from the wide to the long format when considering 1 starting state, 1 or many irreversible, exclusive, intermediate states, and 1 absorbing state.

Usage

reshapeIDM(data, var.id, var.time, var.type, var.cov = NULL, start.type = NULL)

Arguments

data

[data.frame] data set in the wide format

var.id

[character] name of the column containing the subject id, i.e. unique identifier for each line.

var.time

[character vector of length 2] name of the columns containing the time variables, i.e. time at which each type of event happen (intermediate or absorbing). If an intermediate event does not occur (e.g. no switch of treatment) then the time variable should be set to the end of follow-up time.

var.type

[character vector of length 2] name of the columns containing event type indicator. The first event type indicator can be categorical (multiple intermediate states) but the last one should be binary.

var.cov

[character vecotr] optional baseline covariate values to be considered.

start.type

[character] starting state. Deduced from var.type if left unspecified.

Details

Argument var.time and var.type should refer to the same states, first the intermediate states, and then the absorbing state.

Value

A data.frame with class "dataIMD".

Examples

#### generate data ####
set.seed(10)
n <- 1000 ## sample size (half)
tau <- 01 ## max follow-up time
Tevent <- rexp(2*n, rate = 1/10)
Tswitch <- c(runif(n, min = 1, max = 6), rep(Inf, n))
Cswitch <- sample.int(2, size = 2*n, replace = 2)
index.OC <- which(Tevent[1:n]>Tswitch[1:n])
Tevent[index.OC] <- Tswitch[index.OC] + rexp(length(index.OC), rate = c(1/5,1/2.5)[Cswitch[index.OC]])

#### wide format ####
df.W <- data.frame(id = 1:(2*n),
                   gender = 0:1,
                   time.event = pmin(Tevent,tau),
                   time.switch = pmin(Tevent,Tswitch,tau),
                   switch = ifelse(Tswitch<pmin(Tevent,tau), Cswitch, 0),
                   event = as.numeric(Tevent <= tau))
head(df.W)

#### long format ####
df.L <- reshapeIDM(df.W,
                   var.id = "id",
                   var.type = c("switch","event"),
                   var.time = c("time.switch","time.event"),
                   var.cov = "gender")
head(df.L)

#### in mstate ####
if(require(mstate)){
tmat <- transMat(x = list(c(2, 3, 4), c(4), c(4), c()), names = c("NoOC","IUC","OC", "Depression"))
tmat
}

bozenne/butils documentation built on Oct. 14, 2023, 6:19 a.m.