inst/doc/survkl.R

## ----include=FALSE------------------------------------------------------------
knitr::opts_chunk$set(fig.alt = "Plot generated in survkl vignette")

## ----eval=FALSE---------------------------------------------------------------
# install.packages("survkl")

## ----eval=FALSE---------------------------------------------------------------
# require(devtools)
# require(remotes)
# remotes::install_github("UM-KevinHe/survkl", ref = "main")

## -----------------------------------------------------------------------------
library(survkl)

## -----------------------------------------------------------------------------
data(ExampleData_lowdim)

train  <- ExampleData_lowdim$train
test   <- ExampleData_lowdim$test

z      <- train$z
delta  <- train$status
time   <- train$time
strat  <- train$stratum

## -----------------------------------------------------------------------------
beta_ext <- ExampleData_lowdim$beta_external_good

## -----------------------------------------------------------------------------
eta_grid <- generate_eta(method = "exponential", n = 100, max_eta = 30)
fit_lowdim <- coxkl(
  z = z,
  delta = delta,
  time = time,
  stratum = strat,
  beta = beta_ext,
  etas = eta_grid
)

## -----------------------------------------------------------------------------
coef(fit_lowdim, eta = 1)

## -----------------------------------------------------------------------------
RS_ext <- as.matrix(z) %*% as.matrix(beta_ext)

fit_lowdim_RS <- coxkl(
  z = z,
  delta = delta,
  time = time,
  stratum = strat,
  RS = RS_ext,
  etas = eta_grid
)

coef(fit_lowdim_RS)[1:5]

## -----------------------------------------------------------------------------
plot(
  fit_lowdim,
  test_z       = test$z,
  test_time    = test$time,
  test_delta   = test$status,
  test_stratum = test$stratum,
  criteria     = "loss"
)    

## -----------------------------------------------------------------------------
cv_lowdim <- cv.coxkl(
  z        = z,
  delta    = delta,
  time     = time,
  stratum  = strat,
  beta     = beta_ext,
  etas     = eta_grid,
  nfolds   = 5,
  criteria = "V&VH",
  seed = 1)

## -----------------------------------------------------------------------------
cv.plot(cv_lowdim)

## -----------------------------------------------------------------------------
data(ExampleData_highdim)

train_hd  <- ExampleData_highdim$train
test_hd   <- ExampleData_highdim$test

z_hd      <- train_hd$z
delta_hd  <- train_hd$status
time_hd   <- train_hd$time
strat_hd  <- train_hd$stratum

## -----------------------------------------------------------------------------
beta_external_hd <- ExampleData_highdim$beta_external

## -----------------------------------------------------------------------------
model_ridge <- coxkl_ridge(
  z        = z_hd,
  delta    = delta_hd,
  time     = time_hd,
  stratum  = strat_hd,
  beta     = beta_external_hd,   # external coefficients (length 50)
  eta      = 1                  # KL integration weight
)

## -----------------------------------------------------------------------------
# All lambdas (columns ordered in decreasing lambda)
coef(model_ridge)[1:5, 1:5]  # first 5 lambdas

## -----------------------------------------------------------------------------
lambda_target <- model_ridge$lambda[5]
coef(model_ridge, lambda = lambda_target)[1:5]

## -----------------------------------------------------------------------------
plot(
  model_ridge,
  test_z       = test_hd$z,
  test_time    = test_hd$time,
  test_delta   = test_hd$status,
  test_stratum = test_hd$stratum,
  criteria     = "CIndex"
)

## -----------------------------------------------------------------------------
eta_grid_hd <- generate_eta(method = "exponential", n = 50, max_eta = 100)

cv_ridge_hd <- cv.coxkl_ridge(
  z           = z_hd,
  delta       = delta_hd,
  time        = time_hd,
  stratum     = strat_hd,
  beta        = beta_external_hd,
  etas        = eta_grid_hd,
  nfolds      = 5,
  cv.criteria = "V&VH",
  seed = 1)

## -----------------------------------------------------------------------------
cv_ridge_hd$integrated_stat.best_per_eta

## -----------------------------------------------------------------------------
cv.plot(cv_ridge_hd)

## -----------------------------------------------------------------------------
model_enet <- coxkl_enet(
  z        = z_hd,
  delta    = delta_hd,
  time     = time_hd,
  stratum  = strat_hd,
  beta     = beta_external_hd,   
  eta      = 1,                 
  alpha    = 1                  # LASSO penalty
)

## -----------------------------------------------------------------------------
coef(model_enet)[1:5, 1:5]

## -----------------------------------------------------------------------------
lambda_target <- model_enet$lambda[5]
coef(model_enet, lambda = lambda_target)[1:5]

## -----------------------------------------------------------------------------
plot(
  model_enet,
  test_z = test_hd$z,
  test_time = test_hd$time,
  test_delta = test_hd$status,
  test_stratum = test_hd$stratum,
  criteria = "loss"
)

## -----------------------------------------------------------------------------
eta_grid_hd <- generate_eta(method = "exponential",
                            n = 50,
                            max_eta = 100)

cv_enet_hd <- cv.coxkl_enet(
  z = z_hd,
  delta = delta_hd,
  time = time_hd,
  stratum = strat_hd,
  beta = beta_external_hd,
  etas = eta_grid_hd,
  alpha = 1, # LASSO
  nfolds = 5,
  cv.criteria = "V&VH",
  seed = 1
)

## -----------------------------------------------------------------------------
cv.plot(cv_enet_hd)

Try the survkl package in your browser

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

survkl documentation built on April 22, 2026, 1:08 a.m.