-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation-study.R
More file actions
201 lines (160 loc) · 7.2 KB
/
Copy pathsimulation-study.R
File metadata and controls
201 lines (160 loc) · 7.2 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# This code was used to conduct a simulation study to compare the performance
# of the estimation method for different choices of the tuning parameter (grid length).
#load function to simulate survival data
source("sim.survdata.R")
# install and load package CPsurv from GitHub
devtools::install_github("stefkruegel/CPsurv")
library(CPsurv)
# load further packages
library(foreach)
library(doParallel)
library(snow)
# fixed parameters:
nsamp <- 1000 # number of simulation samples
shape <- 0.44
scale <- 100
cpmax <- 360
censpoint <- 540
B.correct = 49
B = 999
cores <- min(19, parallel::detectCores()) #CPU-cores
# varying parametes for simulating data:
n <- c(1000, 5000) # number of observations
cens <- c("random", "type1", "no") # censoring type
jump <- c(1, 0) #jump in hazard rate at changepoint
chp <- c(55, 90) # changepoint
# all possible parameter combinations:
param <- as.data.frame(expand.grid(n, chp, jump, cens))
names(param) <- c("n", "chp", "jump", "cens")
param$cens <- as.character(param$cens)
# parameter for estimation: interval width
intwd <- c(3, 5, 10, 20)
set.seed(20160701)
seeds <- sample.int(1e8, size = nsamp)
results <- list()
for(i in 1:nrow(param)){
cl <- makeCluster(cores)
registerDoParallel(cl)
resultlist <- foreach(idx = 1:nsamp, .packages = "CPsurv") %dopar% {
set.seed(seeds[idx])
# simulate main dataset of length 'nobs' with parameters param[i,]
simdata <- sim.survdata(nobs = param$n[i], changeP = param$chp[i],
shape = shape, scale = scale, jump = param$jump[i],
censoring = param$cens[i], censpoint = censpoint)
#---------------------------------------------------------------------------
resmat <- matrix(nrow = length(intwd), ncol = 10)
for(j in 1:length(intwd)){
est <- cpsurv(time = simdata$time, event = simdata$event,
cpmax = cpmax, intwd = intwd[j],
censoring = param$cens[i], censpoint = censpoint,
biascorrect = FALSE, parametric = FALSE,
B.correct = B.correct, boot.ci = TRUE, B = B,
parallel = FALSE)
tab <- table(cut(simdata$time[simdata$event == 1], est$lower.lim))
tab2 <- table(cut(simdata$time[simdata$event == 1],
est$lower.lim[est$lower.lim <= param$chp[i]]))
cilow <- est$ci.normal[1]
ciup <- est$ci.normal[2]
perclow <- est$ci.percent[1]
percup <- est$ci.percent[2]
std <- est$sd
min.events <- min(tab)
mean.events <- mean(tab)
min.events.chp <- min(tab2)
resmat[j, ] <- c(est$cp, est$pv.mean, cilow, ciup, perclow, percup, std,
min.events, mean.events, min.events.chp)
}
rownames(resmat) <- intwd
resmat
}
stopCluster(cl)
#============================================================================
output <- matrix(nrow = 0, ncol = 10)
colnames(output) <- c("cp", "pv.mean", "ci1", "ci2", "perc1", "perc2", "sd",
"min.events", "mean.events", "min.events.chp")
for(m in 1:length(resultlist)){
output <- rbind(output, resultlist[[m]])
}
names <- paste("cens=", param$cens[i], "; jump=", param$jump[i],
"; chp=", param$chp[i], "; n=", param$n[i], sep="")
results[[names]] <- output
counter <- c(i)
save(counter, file="counter.rda")
save(results, file="simulation_results.rda")
}
##############################################################################
### processing the results
##############################################################################
# function for root mean squared error (RMSE)
rmse <- function(x, i){
if(i %in% c(1,2,5,6,9,10,13,14,17,18,21,22)){
tau <- 55
}else{
tau <- 90
}
ret <- round(sqrt( mean((x-tau)^2, na.rm = TRUE) ),2)
ret
}
# function for mean average distance (MAD)
mad <- function(x, i){
if(i %in% c(1,2,5,6,9,10,13,14,17,18,21,22)){
tau <- 55
}else{
tau <- 90
}
ret <- round( mean(abs(x-tau), na.rm = TRUE) ,2)
ret
}
# function for calculating the coverage of confidence or percentile intervals
cover <- function(lower, upper, i){
if(i %in% c(1,2,5,6,9,10,13,14,17,18,21,22)){
tau <- 55
}else{
tau <- 90
}
abscover <- sum(sapply(seq_along(lower),
function(i) tau >= lower[i] && tau <= upper[i]))
round(abscover / length(lower), 3)
}
# calculate lengths of confidence and percentile intervals
for(i in 1:24){
results[[i]] <- cbind(results[[i]], results[[i]][, "ci2"] - results[[i]][, "ci1"])
results[[i]] <- cbind(results[[i]], results[[i]][, "perc2"] - results[[i]][, "perc1"])
colnames(results[[i]])[11:12] <- c("ci.len", "perc.len")
}
#===========================================================================
names.est <- rownames(results$`cens=random; jump=1; chp=55; n=1000`[1:24,])
measures <- matrix(NA, nrow = (length(results) * 4), ncol = 11)
colnames(measures) <- c("median", "mean", "mad", "rmse", "ci.length", "ci.cover",
"perc.length", "perc.cover", "min.events", "mean.events",
"min.events.beforecp")
for(i in (seq_along(results)-1)){
measures[(1:4)+i*4, 1] <- sapply(1:4, function(x) round(median(results[[i+1]][x+((1:1000)-1)*4, "cp"], na.rm = TRUE)))
measures[(1:4)+i*4, 2] <- sapply(1:4, function(x) round(mean(results[[i+1]][x+((1:1000)-1)*4, "cp"], na.rm = TRUE)))
measures[(1:4)+i*4, 3] <- sapply(1:4, function(x) mad(results[[i+1]][x+((1:1000)-1)*4, "cp"], i+1))
measures[(1:4)+i*4, 4] <- sapply(1:4, function(x) rmse(results[[i+1]][x+((1:1000)-1)*4, "cp"], i+1))
measures[(1:4)+i*4, 5] <- sapply(1:4, function(x) round(mean(results[[i+1]][x+((1:1000)-1)*4, "ci.len"], na.rm = TRUE)))
measures[(1:4)+i*4, 6] <- sapply(1:4, function(x) cover(results[[i+1]][x+((1:1000)-1)*4, "ci1"],
results[[i+1]][x+((1:1000)-1)*4, "ci2"], i+1))
measures[(1:4)+i*4, 7] <- sapply(1:4, function(x) round(mean(results[[i+1]][x+((1:1000)-1)*4, "perc.len"], na.rm = TRUE)))
measures[(1:4)+i*4, 8] <- sapply(1:4, function(x) cover(results[[i+1]][x+((1:1000)-1)*4, "perc1"],
results[[i+1]][x+((1:1000)-1)*4, "perc2"], i+1))
measures[(1:4)+i*4, 9] <- sapply(1:4, function(x) round(mean(results[[i+1]][x+((1:1000)-1)*4, "min.events"], na.rm = TRUE)))
measures[(1:4)+i*4, 10] <- sapply(1:4, function(x) round(mean(results[[i+1]][x+((1:1000)-1)*4, "mean.events"], na.rm = TRUE)))
measures[(1:4)+i*4, 11] <- sapply(1:4, function(x) round(mean(results[[i+1]][x+((1:1000)-1)*4, "min.events.chp"], na.rm = TRUE)))
}
#===========================================================================
# convert matrix to LaTeX table
library(xtable)
measures1 <- data.frame(row=rep(names.est, 4), measures)
xmeasures <- xtable(measures1)
addtorow <- list()
addtorow$pos <- list()
addtorow$command <- vector("numeric")
for(i in 1:24){
names <- names(results)[i]
addtorow$pos[[i]] <- c(4*i-4)
addtorow$command[i] <- paste0("\\newpage \n \\multicolumn{12}{l}{\\textit{", names, "}}\\\\ \n")
}
print(xmeasures, add.to.row = addtorow, include.rownames = FALSE,
include.colnames = TRUE, tabular.environment="longtable", floating=FALSE)