Last data update: 2014.03.03

R: A function for splitting a time according to time periods
timeSplitterR Documentation

A function for splitting a time according to time periods

Description

If we have a violation of the cox proprtional hazards assumption we need to split an individual's followup time into several.See vignette("timeSplitter", package="Greg") for a detailed description.

Usage

timeSplitter(data, by, time_var, event_var, event_start_status,
  time_related_vars, time_offset)

Arguments

data

The dataset that you want to split according to the time_var option.

by

The time period that you want to split the dataset by. The size of the variable must be in proportion to the the time_var.

time_var

The name of the main time variable in the dataset. This variable must be a numeric variable.

event_var

The event variable

event_start_status

The start status of the event status, e.g. "Alive"

time_related_vars

A dataset often contains other variabels that you want to update during the split, most commonly these are age or calendar time.

time_offset

If you want to skip the initial years you can offset the entire dataset by setting this variable. See detailed description below.

Details

Important note: The time variables must have the same time unit. I.e. function can not dedu if all variables are in years or if one happens to be in days.

Value

data.frame with the split data. The starting time for each period is named Start_time and the ending time is called Stop_time. Note that the resulting event_var will now contain the time-splitted eventvar.

The time_offset - details

Both time_var and other variables will be adjusted by the time_offset, e.g. if we the time scale is in years and we want to skip the first 4 years we set the time_offset = 4. In the outputted dataset the smallest time_var will be 0. Note: 0 will not be included as we generally want to look at those that survived the start date, e.g. if a patient dies on the 4-year mark we would not include him/her in our study.

Examples

test_data <- data.frame(
  id = 1:4,
  time = c(4, 3.5, 1, 5),
  event = c("alive", "censored", "dead", "dead"),
  age = c(62.2, 55.3, 73.7, 46.3),
  date = as.Date(
    c("2003-01-01", 
      "2010-04-01", 
      "2013-09-20",
      "2002-02-23"))
)
timeSplitter(test_data, .5, 
             time_var = "time",
             time_related_vars = c("age", "date"),
             event_var = "event")

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(Greg)
Loading required package: forestplot
Loading required package: grid
Loading required package: magrittr
Loading required package: Gmisc
Loading required package: Rcpp
Loading required package: htmlTable
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/Greg/timeSplitter.Rd_%03d_medium.png", width=480, height=480)
> ### Name: timeSplitter
> ### Title: A function for splitting a time according to time periods
> ### Aliases: timeSplitter
> 
> ### ** Examples
> 
> test_data <- data.frame(
+   id = 1:4,
+   time = c(4, 3.5, 1, 5),
+   event = c("alive", "censored", "dead", "dead"),
+   age = c(62.2, 55.3, 73.7, 46.3),
+   date = as.Date(
+     c("2003-01-01", 
+       "2010-04-01", 
+       "2013-09-20",
+       "2002-02-23"))
+ )
> timeSplitter(test_data, .5, 
+              time_var = "time",
+              time_related_vars = c("age", "date"),
+              event_var = "event")
   id    event  age     date Start_time Stop_time
1   1    alive 62.2 2002.999        0.0       0.5
2   1    alive 62.7 2003.499        0.5       1.0
3   1    alive 63.2 2003.999        1.0       1.5
4   1    alive 63.7 2004.499        1.5       2.0
5   1    alive 64.2 2004.999        2.0       2.5
6   1    alive 64.7 2005.499        2.5       3.0
7   1    alive 65.2 2005.999        3.0       3.5
8   1    alive 65.7 2006.499        3.5       4.0
9   2    alive 55.3 2010.246        0.0       0.5
10  2    alive 55.8 2010.746        0.5       1.0
11  2    alive 56.3 2011.246        1.0       1.5
12  2    alive 56.8 2011.746        1.5       2.0
13  2    alive 57.3 2012.246        2.0       2.5
14  2    alive 57.8 2012.746        2.5       3.0
15  2 censored 58.3 2013.246        3.0       3.5
16  3    alive 73.7 2013.718        0.0       0.5
17  3     dead 74.2 2014.218        0.5       1.0
18  4    alive 46.3 2002.145        0.0       0.5
19  4    alive 46.8 2002.645        0.5       1.0
20  4    alive 47.3 2003.145        1.0       1.5
21  4    alive 47.8 2003.645        1.5       2.0
22  4    alive 48.3 2004.145        2.0       2.5
23  4    alive 48.8 2004.645        2.5       3.0
24  4    alive 49.3 2005.145        3.0       3.5
25  4    alive 49.8 2005.645        3.5       4.0
26  4    alive 50.3 2006.145        4.0       4.5
27  4     dead 50.8 2006.645        4.5       5.0
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>