Diff of /function/leaveoneout.R [000000] .. [daa031]

Switch to unified view

a b/function/leaveoneout.R
1
runLOO_jm = function(i,Tstart,predict_times,data,id){
2
  tryCatch({
3
    RD = data[which(data$PatientID %in% id[-i,1]),]
4
    ND = data[which(data$PatientID %in% id[i,1]),]
5
    RD.id = RD[which(!duplicated(RD$PatientID)), ]
6
    lmeFit1 <- lme( logAF ~ ns(TestDate,2),data=RD,random = ~  ns(TestDate,2) | PatientID,
7
                    control = lmeControl(opt = "optim",msMaxIter =1000))
8
    coxFit1 <- coxph(Surv(DFS,DFS_status)~TP53+T_stage,data=RD.id,x = TRUE)
9
    iForm <- list(fixed = ~ 0 + TestDate + ins(TestDate, 2), random = ~ 0 + TestDate + ins(TestDate, 2),
10
                  indFixed = 1:3, indRandom = 1:3)
11
    JM.RD <-jointModelBayes(lmeFit1, coxFit1, timeVar = "TestDate",
12
                            param = "td-extra", extraForm = iForm
13
    )
14
    NDn = ND[which(ND$TestDate<=Tstart),]
15
    survfit <- survfitJM(JM.RD, newdata = NDn,idVar = "PatientID",type="SurvProb",
16
                         survTimes = predict_times )
17
    survsum = survfit$summaries[[1]]
18
    res = cbind( ND,
19
           prob1=round(survsum[1,"Median"],4), # at 12 months
20
           prob2=round(survsum[2,"Median"],4) # at 15 months
21
    )
22
    return(res)
23
  },
24
  error=function(e) { print(e);return(NULL)}
25
  )
26
}
27
28
runLOO_cox = function(i,Tstart,predict_times,data,id){
29
  tryCatch({
30
    library(pec)
31
    RD = data[which(data$PatientID %in% id[-i,1]),]
32
    ND = data[which(data$PatientID %in% id[i,1]),]
33
    RD.id = RD[which(!duplicated(RD$PatientID)), ]
34
    dataLM <- JMbayes:::dataLM
35
    D1 = dataLM(RD, Tstart, idVar ="PatientID" ,respVar = "Test_status", timeVar = "TestDate", 
36
                evTimeVar = "DFS")
37
    # landmarking cox
38
    CoxLM1 <- coxph(Surv(DFS, DFS_status) ~ Test_status+T_stage+TP53, x=TRUE,
39
                    data = D1)
40
    NDn = tail(ND[which(ND$TestDate<=Tstart),],1)
41
    pred_lm1 = predictSurvProb(CoxLM1,newdata = NDn, times = predict_times)
42
    res = cbind( ND,
43
                 cox1_prob1=round(pred_lm1[[1]],4),
44
                 cox1_prob2=round(pred_lm1[[2]],4)
45
                 )
46
    return(res)
47
  },
48
  error=function(e) { print(e);return(NULL)}
49
  )
50
}
51
52
###### test #########
53
data = mydata
54
id = mydata.id
55
runLOO_jm(1,Tstart,predict_times,mydata,mydata.id)
56