survTreeLaplaceHazard: Laplace Hazards Of Survival Tree For Competing Risks

View source: R/survTreeLaplaceHazards.R

survTreeLaplaceHazardR Documentation

Laplace Hazards Of Survival Tree For Competing Risks

Description

Predicts the Laplace-smoothed hazards of discrete survival tree based on fitted objects of class "rpart" or "ranger". Can be used for single-risk or competing risk discrete survival data.

Usage

survTreeLaplaceHazard(treeModel, newdata, alpha, rangerData = NULL)

Arguments

treeModel

Fitted tree object as generated by function rpart (class "rpart").

newdata

Data in long format for which hazards are to be computed. Must contain the same columns that were used for tree fitting (class "data.frame").

alpha

Smoothing parameter for laplace-smoothing. Must be a non-negative number. A value of 0 corresponds to no smoothing (class "numeric").

rangerData

Original training data (class "data.frame") for fitting treeModel of class "ranger". Must be in long format.

Value

A m by k matrix with m being the length of newdata and k being the number of classes in treeModel. Each row corresponds to the smoothed hazard of the respective observation.

See Also

ranger

Examples

############################################
# Example with rpart discrete survival tree
library(pec)
library(caret)

# Example data
data(cost)

# Convert time to years and select training and testing subsample
cost$time <- ceiling(cost$time/365)
costTrain <- cost[1:100, ]
costTest  <- cost[101:120, ]

# Convert to long format
timeColumn <- "time"
eventColumn <- "status"
costTrainLong <- dataLong(dataShort=costTrain, timeColumn = "time", 
                          eventColumn = "status")
costTestLong  <- dataLong(dataShort=costTest, timeColumn = "time", 
                          eventColumn = "status")
head(costTrainLong)

# Fit a survival tree
costTree <- rpart(formula = y ~ timeInt + prevStroke + age + sex, data = costTrainLong, 
                  method = "class")

# Compute smoothed hazards for test data
predictedhazards <- survTreeLaplaceHazard(costTree, costTestLong, 1)
predictedhazards

############################################
# Example with ranger discrete survival tree
library(pec)
library(caret)
library(ranger)
data(cost)

# Take subsample and convert time to years
cost$time <- ceiling(cost$time/365)
costSubTrain <- cost[1:50,]
costSubTest <- cost[51:70,]

# Specify column names for data augmentation
timeColumn<-"time"
eventColumn<-"status"
costSubTrainLong <- dataLong(costSubTrain, timeColumn, eventColumn)
costSubTestLong <- dataLong(costSubTest, timeColumn, eventColumn)

# Estimate discrete survival tree
formula <- y ~ timeInt + diabetes + prevStroke + age + sex
rangerTree <- ranger(formula, costSubTrainLong, num.trees = 1, mtry = 5, 
classification = TRUE, splitrule = "hellinger", replace = FALSE, 
sample.fraction = 1, max.depth = 5)

# Compute laplace-smoothed hazards
laplHaz <- survTreeLaplaceHazard(rangerTree, 
costSubTestLong, alpha = 1, costSubTrainLong)
laplHaz

discSurv documentation built on April 29, 2026, 9:07 a.m.