File: splitLexisDT.R

package info (click to toggle)
r-cran-popepi 0.4.13%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,656 kB
  • sloc: sh: 13; makefile: 2
file content (296 lines) | stat: -rw-r--r-- 10,686 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#' @title Split case-level observations
#' @author Joonas Miettinen
#' @description Split a `Lexis` object along one time scale
#' (as `[Epi::splitLexis]`) with speed
#' @param lex a Lexis object, split or not
#' @param breaks a vector of `[a,b)` breaks to split `data` by
#' @param timeScale a character string; name of the time scale to split by
#' @param merge logical; if `TRUE`, retains all variables
#' from the original data - i.e. original variables are
#' repeated for all the rows by original subject
#' @param drop logical; if `TRUE`, drops all resulting rows
#' after expansion that reside outside the time window
#' defined by the given breaks
#'
#'
#' @details
#'
#' `splitLexisDT` is in essence a \pkg{data.table} version of
#' `splitLexis` or `survSplit` for splitting along a single
#' time scale. It requires a Lexis object as input, which may have already
#' been split along some time scale.
#'
#' Unlike `splitLexis`, `splitLexisDT` drops observed time outside
#' the roof and floor of `breaks` by default - with `drop = FALSE`
#' the functions have identical behaviour.
#'
#' The `Lexis` time scale variables can be of any arbitrary
#' format, e.g. `Date`,
#' fractional years (see `[Epi::cal.yr]`) and `[get.yrs]`.
#'
#' @return
#' A `data.table` or `data.frame`
#' (depending on `options("popEpi.datatable")`; see `?popEpi`)
#' object expanded to accommodate split observations.
#'
#' @export
#' @family splitting functions
#' @examples
#' library(Epi)
#' data("sire", package = "popEpi")
#' x <- Lexis(data=sire[1000:1100, ],
#'            entry = list(fot=0, per=get.yrs(dg_date), age=dg_age),
#'            exit=list(per=get.yrs(ex_date)), exit.status=status)
#' BL <- list(fot=seq(0, 5, by = 3/12), per=c(2008, 2013))
#'
#' x2 <- splitMulti(x, breaks = BL, drop = FALSE)
#'
#' x3 <- splitLexisDT(x, breaks = BL$fot, timeScale = "fot", drop = FALSE)
#' x3 <- splitLexisDT(x3, breaks = BL$per, timeScale = "per", drop = FALSE)
#'
#' x4 <- splitLexis(x,  breaks = BL$fot, time.scale = "fot")
#' x4 <- splitLexis(x4, breaks = BL$per, time.scale = "per")
#' ## all produce identical results
#'
#' ## using Date variables
#' x <- Lexis(data=sire[1000:1100, ],
#'            entry = list(fot=0, per=dg_date, age=dg_date-bi_date),
#'            exit=list(per=ex_date), exit.status=status)
#' BL <- list(fot = 0:5*365.25, per = as.Date(c("2008-01-01", "2013-01-01")))
#'
#' x2 <- splitMulti(x, breaks = BL, drop = FALSE)
#'
#' x3 <- splitLexisDT(x, breaks = BL$fot, timeScale = "fot", drop = FALSE)
#' x3 <- splitLexisDT(x3, breaks = BL$per, timeScale = "per", drop = FALSE)
#'
#' ## splitLexis may not work when using Dates
splitLexisDT <- function(lex, breaks, timeScale, merge = TRUE, drop = TRUE) {

  do_split <- TRUE

  tol <- .Machine$double.eps^0.5
  checkLexisData(lex, check.breaks = FALSE)

  attr_list <- copy(attributes(lex)[c("time.scales", "breaks", "time.since")])
  allScales <- attr_list[["time.scales"]]
  allBreaks <- attr_list[["breaks"]]

  if (!timeScale %in% allScales) {
    stop("timeScale '", timeScale,"' not among following existing time scales: ",
         paste0("'", allScales, "'", collapse = ", "))
  }

  ## lexVars: if !merge, will drop all but these (NOTE: checkLexisData
  ## check for existence of these)
  lexVars <- c("lex.id", "lex.multi", allScales, "lex.dur", "lex.Cst", "lex.Xst")
  lexVars <- intersect(lexVars, names(lex))
  othVars <- setdiff(names(lex), lexVars)

  ## basic checks on breaks
  if (drop && length(breaks) == 1L) {
    stop("Length of breaks vector is one, but argument 'drop' is TRUE. ",
         "Cannot do dropping with only one break. Either supply at least ",
         "two breaks or set drop = FALSE.")
  }
  if (length(breaks) == 0L) {
    stop("No breaks supplied (length of breaks is zero).")
  }

  ## remove any existing breaks already split by;
  ## NOTE: setdiff would break Date format breaks!
  orig_breaks <- copy(breaks)
  if (length(allBreaks[[timeScale]])) {
    ## because any test like (x %in% NULL) results in FALSE.
    breaks <- breaks[!breaks %in% allBreaks[[timeScale]]]
  }

  breaks <- matchBreakTypes(lex, breaks, timeScale, modify.lex = FALSE)

  if (length(breaks) == 0L || (length(orig_breaks) == 2L && drop)) {
    ## former means no additional splitting to do. (we still crop & drop
    ## if argument drop = TRUE)
    ## latter means we only need to crop & drop.
    do_split <- FALSE
    breaks <- orig_breaks
  }

  breaks <- sort(breaks)
  if (!drop) breaks <- protectFromDrop(breaks)

  BL <- list(breaks)
  setattr(BL, "names", timeScale)
  checkBreaksList(x = lex, breaks = BL)


  ## use subset lex if dropping for efficiency
  orig_lex <- lex
  if (drop) {
    keepVars <- if (merge) NULL else lexVars ## NULL: all vars
    lex <- subsetDTorDF(lex, select = keepVars)
    rm(keepVars)
    lex <- data.table(lex)

    setattr(lex, "class", c("Lexis", "data.table", "data.frame"))

    lex <- intelliCrop(lex, breaks = BL, allScales = allScales,
                       cropStatuses = TRUE, tol = tol)
    lex <- intelliDrop(lex, breaks = BL, dropNegDur = TRUE,
                       check = FALSE, tol = tol)
  }

  if (!do_split) {

    l <- if (!drop) copy(lex) else lex

  } else {

    ## currently cannot handle NA values in split time scale; will add them in
    ## the end
    ts_is_na <- is.na(lex[[timeScale]])
    ts_any_na <- any(ts_is_na)
    if (ts_any_na) {
      warning("NA values in the time scale you are splitting along ('",
              timeScale,"'). Results may deviate from that produced by ",
              "splitLexis from package Epi. For safety you may want to split ",
              "using only the data with no NA values and combine the the split",
              " data with the NA-valued data using rbind.")
      lex_na <- lex[ts_is_na, ]
      lex <- lex[!ts_is_na, ]
    }


    ## will use this due to step below (and laziness)
    ts_values <- lex[[timeScale]]
    ## Date objects are based on doubles and therefore keep the most information
    if (inherits(ts_values, c("IDate"))) ts_values <- as.Date(ts_values)

    N_expand <- length(breaks)
    N_subjects <- nrow(lex)

    ## use tmp id to ensure correct status rolling -----------------------------
    id_dt <- data.table(
      tmp_id_values = 1:nrow(lex),
      orig_id_values = lex[["lex.id"]]
    )
    on.exit(set(lex, j = "lex.id", value = id_dt[["orig_id_values"]]))
    set(lex, j = "lex.id", value = id_dt[["tmp_id_values"]])

    ## quick data expansion ------------------------------------------------------

    l <- vector(mode = "list", length = N_expand)
    l[[1]] <- data.table(lex)

    if (!merge) setcolsnull(l[[1]], keep = lexVars, soft = FALSE)

    tmpID <- makeTempVarName(data = l[[1]], pre = "TEMP_SPLITTING_ID")
    tmpIE <- makeTempVarName(data = l[[1]], pre = "TEMP_SPLIT_INT_END")

    set(l[[1]], j = tmpID, value = 1:nrow(l[[1]]))
    if (N_expand > 1L) {
      for (k in 2:(N_expand)) {
        l[[k]] <- l[[1]]
      }
    }

    l <- rbindlist(l)

    ## time scale value determination --------------------------------------------
    set(l, j = tmpIE,  value = rep(breaks, each = N_subjects))
    set(l, j = tmpIE,  value = pmin(l[[tmpIE]], l[[timeScale]] + l$lex.dur) )
    set(l, j = timeScale, value = c(
      ts_values,
      pmax(ts_values, rep(breaks[-length(breaks)], each = N_subjects))
    ))

    set(l, j = "lex.dur", value = l[[tmpIE]] - l[[timeScale]] )

    ## other time scale values ---------------------------------------------------
    otherScales <- setdiff(allScales, timeScale)
    if (length(otherScales) > 0) {
      ## change in timeScale
      ts_delta <- l[[timeScale]] - ts_values
      for (k in otherScales) {
        set(l, j = k, value = lex[[k]] + ts_delta)
      }
    }

    ## dropping ----------------------------------------------------------------
    ## drops very very small intervals as well as dur <= 0
    has_zero_dur <- l[["lex.dur"]] < tol
    if (any(has_zero_dur)) {
      l <- l[!has_zero_dur]
    }



    ## roll states -------------------------------------------------------------
    # this avoids duplicate deaths, etc., where appropriate.
    setkeyv(l, c("lex.id", timeScale))
    lex_id <- mget_cols(c("lex.Cst", "lex.Xst", "lex.id"), data = lex)
    setattr(lex_id, "time.scales", allScales)
    roll_lexis_status_inplace(
      unsplit.data = lex_id, split.data = l, id.var = "lex.id"
    )
    rm("lex_id")


    set(l, j = c(tmpIE, tmpID), value = NULL)

    ## revert to original IDs --------------------------------------------------
    set(l, j = "lex.id", value = {
      id_dt[
        i = list(tmp_id_values = l$lex.id),
        j = .SD,
        on = "tmp_id_values",
        .SDcols = "orig_id_values"
        ]
    })

    if (ts_any_na) {
      l <- rbind(l, lex_na)
      setkeyv(l, c("lex.id", timeScale))
    }

  }

  ## harmonize statuses --------------------------------------------------------
  harmonizeStatuses(x = l, C = "lex.Cst", X = "lex.Xst")


  ## ensure time scales and lex.dur have same (ish) class as before ------------
  for (k in c(allScales, "lex.dur")) {

    if (inherits(orig_lex[[k]], "difftime") && !inherits(l[[k]], "difftime")){
      setattr(l[[k]], "class", "difftime")
      setattr(l[[k]], "units", attr(orig_lex[[k]], "units"))
    } else if (is.numeric(orig_lex[[k]]) && inherits(l[[k]], "difftime")) {
      set(l, j = k, value = as.numeric(l[[k]]))
    }

  }

  ## harmonize time scales -----------------------------------------------------
  ## numeric time scales are forced to the lowest common denominator:
  ## difftime -> integer -> double (though difftime is not numeric class)
  harmonizeNumericTimeScales(l, times = c(allScales, "lex.dur"))


  ## final touch & attributes --------------------------------------------------
  setcolorder(l, neworder = intersect(c(lexVars, othVars), names(l))) #merge=T/F
  if (!drop) breaks <- unprotectFromDrop(breaks)
  allBreaks[[timeScale]] <- sort(unique(c(allBreaks[[timeScale]], breaks)))

  allBreaks <- lapply(allScales, function(scale_nm) {
    allBreaks[[scale_nm]] ## intentionally NULL if not there
  })
  names(allBreaks) <- allScales

  setattr(l, "breaks", allBreaks)
  setattr(l, "time.scales", allScales)
  setattr(l, "time.since", attr_list[["time.since"]])
  setattr(l, "class", c("Lexis","data.table","data.frame"))
  if (!return_DT()) setDFpe(l)

  l[]
}