R/node_aalen.r

Defines functions node_aalen

Documented in node_aalen

## generate survival times from an Aalen additive hazards model
## (with time-constant coefficients and time-constant baseline hazard)
#' @importFrom data.table as.data.table
#' @export
node_aalen <- function(data, parents, formula=NULL, betas, intercept,
                       cens_dist=NULL, cens_args, name, as_two_cols=TRUE,
                       left=0) {

  data <- as.data.table(data)
  data <- data[, parents, with=FALSE]

  hazard <- calc_linpred(data=data, betas=betas, intercept=intercept)

  if (min(hazard) < 0) {
    stop("Hazards < 0 encountered, meaning the model cannot be simulated",
         " from as specified. Please change the coefficients or other",
         " parts of the data generation process to ensure positive hazards.",
         call.=FALSE)
  }

  u <- stats::runif(nrow(data))
  time <- left - log(u) / hazard

  if (!as_two_cols && is.null(cens_dist)) {
    out_data <- time
  } else {
    out_data <- add_censoring(times=time, cens_dist=cens_dist,
                              cens_args=cens_args, name=name)
  }

  return(out_data)
}

Try the simDAG package in your browser

Any scripts or data that you put into this service are public.

simDAG documentation built on Jan. 9, 2026, 1:08 a.m.