0

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?

JanLauGe
  • 2,297
  • 2
  • 16
  • 40
Khan
  • 107
  • 1
  • 9

1 Answers1

1

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

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
  • can you please take a look at my question? https://stackoverflow.com/questions/65137064/r-plotting-roc-curves-without-the-pec-library thanks! –  Dec 04 '20 at 04:10