Search code examples
rsurvival-analysiscox-regression

Using "pec" R package for prediction from "coxph" function on lung dataset


I want to get predictions for the lung dataset available from penalized R package using the coxph R function for Cox proportional hazard model.

I have the following example code.

library(survival) 

library(pec)

library(penalized)

data("lung")

data <- lung

trainind <- sample(1:n,n*0.7)

testind <- (1:n)[-trainind]

frm <- as.formula(paste("Surv(time, status)~",paste(names(data[,-c(2,3)]), collapse="+")))

cox <- coxph(frm,data=data[trainind,])

PredError <- pec(list(Cox=cox),Hist(time,status)~1,data=data[testind,])

I get the following error from the above

Error in UseMethod("predictEventProb", object) : no applicable method for 'predictEventProb' applied to an object of class "coxph"

Can someone solve this problem?


Solution

  • Two problems must be addressed in the data dataset:
    - delete (or impute) missing values
    - recode status variable as 0 and 1

    library(survival) 
    library(pec)
    library(penalized)
    set.seed(123)
    data("lung")
    
    # Delete rows with missing values
    data <- na.omit(lung)
    # Recode status as 0 and 1
    table(data$status)
    #  1   2 
    # 47 120
    data$status <- data$status-1
    
    n <- nrow(data)
    trainind <- sample(1:n,n*0.7)
    trainset <- data[trainind,]
    testset <- data[-trainind,]
    frm <- as.formula(paste("Surv(time, status)~",paste(names(data[,-c(2,3)]), collapse="+")))
    cox <- coxph(frm, data=trainset, y=TRUE)
    PredError <- pec(list("Cox"=cox), Hist(time,status)~1, data=testset)
    plot(PredError)
    

    enter image description here