R/GendataAFT.R

Defines functions GendataAFT

Documented in GendataAFT

#' Generate simulation data (Survival data based on the accelerated failure time model)
#'
#' This function helps you quickly generate simulation data based on the AFT model.
#' You just need to input the sample and dimension of the data
#' you want to generate and the covariance parameter rho.
#'
#' @param n Number of subjects in the dataset to be simulated. It will also equal to the
#' number of rows in the dataset to be simulated, because it is assumed that each
#' row represents a different independent and identically distributed subject.
#' @param p Number of predictor variables (covariates) in the simulated dataset.
#' These covariates will be the features screened by model-free procedures.
#' @param rho The correlation between adjacent covariates in the simulated matrix X.
#' The within-subject covariance matrix of X is assumed to has the same form as an
#' AR(1) auto-regressive covariance matrix, although this is not meant to imply
#' that the X covariates for each subject are in fact a time series. Instead, it is just
#' used as an example of a parsimonious but nontrivial covariance structure. If
#' rho is left at the default of zero, the X covariates will be independent and the
#' simulation will run faster.
#' @param lambda This parameter control the censoring rate in survival data.
#' The censored time is generated by exponential distribution with mean 1/lambda. The default
#' is lambda=0.1.
#' @param beta A vector with length of n, which are the coefficients that you want to generate
#' about Linear model. The default is beta=(1,1,1,1,1,0,...,0)^T;
#' @param error The distribution of error term.
#'
#' @return the list of your simulation data
#' @import MASS
#' @importFrom MASS mvrnorm
#' @importFrom stats rexp
#' @importFrom stats rnorm
#' @importFrom stats rt
#' @importFrom stats rcauchy
#'
#' @export
#' @author Xuewei Cheng \email{xwcheng@hunnu.edu.cn}
#' @examples
#' n <- 100
#' p <- 200
#' rho <- 0.5
#' data <- GendataAFT(n, p, rho)
#'
#' @references
#' Wei LJ (1992). “The accelerated failure time model: a useful alternative to the Cox regression model in survival analysis.” Statistics in medicine, 11(14-15), 1871–1879.
GendataAFT <- function(n, p, rho,
                       beta = c(rep(1, 5), rep(0, p - 5)), lambda = 0.1,
                       error = "gaussian") # n sample size; p dimension size.
{
  sig <- matrix(0, p, p)
  sig <- rho^abs(row(sig) - col(sig))
  diag(sig) <- rep(1, p)
  X <- mvrnorm(n, rep(0, p), sig)
  if (error == "gaussian") {
    myrates <- exp(X %*% beta + rnorm(n))
  } else if (error == "t") {
    myrates <- exp(X %*% beta + rt(n, 2))
  } else if (error == "cauchy") {
    myrates <- exp(X %*% beta + rcauchy(n))
  }
  Sur <- myrates
  CT <- rexp(n, lambda)
  time <- pmin(Sur, CT)
  status <- as.numeric(Sur <= CT)
  return(list(X = X, time = time, status = status))
}

Try the MFSIS package in your browser

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

MFSIS documentation built on June 22, 2024, 9:42 a.m.