-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions_Cox.R
44 lines (38 loc) · 1.17 KB
/
Functions_Cox.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#devtools::install_github("mdbrown/partlyconditional")
library(survival)
#--------------------------------------------------
# Cox
#--------------------------------------------------
# Train
train.Cox <- function(data.train) {
set.seed(12345)
model.cox <- coxph(Surv(time, status) ~ intensity, data=data.train)
return(model.cox)
}
# Test
test.Cox <- function(data.test, model.Cox) {
set.seed(12345)
newd <- cbind(data.test, status=rep(0,nrow(data.test)))
newd <- newd[1,,drop=FALSE]
result <- 1 - predict(model.Cox, newdata = newd, type="survival")
return(result)
}
# Predict
pred.Cox <- function(model.Cox, data.test, data.test.id) {
result <- list()
for(i in 1:nrow(data.test.id)) {
test <- data.test[data.test[,"subject"]==data.test.id[i,"subject"],c("id","time","intensity")]
result[[i]] <- test.Cox(test, model.Cox)
}
names(result) <- data.test.id[,"subject"]
return(result)
}
# Subset data, utility function
subsetData <- function(data, limit) {
subset <- foreach(s=unique(data$subject), .combine=rbind) %do% {
dat <- data[which(data$subject==s),]
dat <- dat[which(dat$time<limit),]
dat[which.max(dat$time),]
}
return(subset)
}