-
Notifications
You must be signed in to change notification settings - Fork 1
/
.Rhistory
512 lines (512 loc) · 20 KB
/
.Rhistory
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
for (i_bern in 1:ndim_bern) {
# one bootstrap sample one full simulation
for (tfur in tcut + 1:tdelta:tpred) {
# prediction from next year
K_B[as.integer((tfur - tcut) / tdelta), i_bern] <-
0 # initialize the predicted number
# Mu_K[tfur - 2020, i_bern] <- 0 # used for CI prediction
# Sig_K[tfur - 2020, i_bern] <- 0 # used for CI prediction
# Ga_K[tfur - 2020, i_bern] <- 0 # used for CI prediction
# mu_K <- 0 # used for CI prediction
# sig_K <- 0 # used for CI prediction
# ga_K <- 0 # used for CI prediction
for (idx_i in 1:nrisk) {
# get the current age of the transformer
tcur_i <- data.matrix(data_set_risk[, "time"])[idx_i]
# get the future age at future year tfur
tfur_i <- tfur - tcut + tcur_i
# get the conditional probability of failure at future year
rou <-
(exp(-(tcur_i / B[i_bern, 2]) ^ B[i_bern, 1]) - exp(-(tfur_i / B[i_bern, 2]) ^
B[i_bern, 1])) / (exp(-(tcur_i / B[i_bern, 2]) ^ B[i_bern, 1]))
# simulate the outcomes at future year with Bernoulli
if (runif(1) < dbern(1, rou, log = FALSE)) {
I <- 1 # simulated as failure at the future year
# mu_K <- mu_K + rou # used for CI prediction
# sig_K <- sig_K + rou * (1 - rou) # used for CI prediction
# ga_K <- ga_K + rou * (1 - rou) * (1 - 2 * rou) # used for CI prediction
} else{
I <- 0 # simulated as survived at the future year
}
# record the number of failured simulated for the future year
K_B[as.integer((tfur - tcut) / tdelta), i_bern] <-
K_B[as.integer((tfur - tcut) / tdelta), i_bern] + I
}
# sig_K <- sqrt(sig_K) # used for CI prediction
# ga_K <- sig_K ^ (-3) * ga_K # used for CI prediction
# Mu_K[tfur - 2020, i_bern] <- mu_K # used for CI prediction
# Sig_K[tfur - 2020, i_bern] <- sig_K # used for CI prediction
# Ga_K[tfur - 2020, i_bern] <- ga_K # used for CI prediction
}
}
View(K_B)
# prediction from next year
K_B[as.integer((tfur - tcut) / tdelta), i_bern] <-
0 # initialize the predicted number
# one bootstrap sample one full simulation
tfur <- tcut + 1
library(Rlab)
# the number of bootstrap samples used for simulation
ndim_bern <- 10 # cannot be greater than numB
tcut <- 2020 # the cutting year, end of current observation
tpred <- 2030 # the end year of prediction
tdelta <-
1 # the accuracy of the prediction like 2021, 2022 or 2021.2, 2021.3..
# the number of samples still at risk (survived)
data_set_risk <- data_set[which(data_set$status == 0),]
nrisk <- dim(data.matrix(data_set_risk[, "time"]))[1]
# initialize the matrix for saving the simulated number of failures
K_B <-
matrix(nrow = as.integer((tpred - tcut) / tdelta), ncol = ndim_bern)
# Mu_K <- matrix(nrow = 10, ncol = ndim_bern) # used for CI prediction
# Sig_K <- matrix(nrow = 10, ncol = ndim_bern) # used for CI prediction
# Ga_K <- matrix(nrow = 10, ncol = ndim_bern) # used for CI prediction
for (i_bern in 1:ndim_bern) {
# one bootstrap sample one full simulation
tfur <- tcut + 1
for (tfur in tcut + 1:tdelta:tpred) {
# prediction from next year
K_B[as.integer((tfur - tcut) / tdelta), i_bern] <-
0 # initialize the predicted number
# Mu_K[tfur - 2020, i_bern] <- 0 # used for CI prediction
# Sig_K[tfur - 2020, i_bern] <- 0 # used for CI prediction
# Ga_K[tfur - 2020, i_bern] <- 0 # used for CI prediction
# mu_K <- 0 # used for CI prediction
# sig_K <- 0 # used for CI prediction
# ga_K <- 0 # used for CI prediction
for (idx_i in 1:nrisk) {
# get the current age of the transformer
tcur_i <- data.matrix(data_set_risk[, "time"])[idx_i]
# get the future age at future year tfur
tfur_i <- tfur - tcut + tcur_i
# get the conditional probability of failure at future year
rou <-
(exp(-(tcur_i / B[i_bern, 2]) ^ B[i_bern, 1]) - exp(-(tfur_i / B[i_bern, 2]) ^
B[i_bern, 1])) / (exp(-(tcur_i / B[i_bern, 2]) ^ B[i_bern, 1]))
# simulate the outcomes at future year with Bernoulli
if (runif(1) < dbern(1, rou, log = FALSE)) {
I <- 1 # simulated as failure at the future year
# mu_K <- mu_K + rou # used for CI prediction
# sig_K <- sig_K + rou * (1 - rou) # used for CI prediction
# ga_K <- ga_K + rou * (1 - rou) * (1 - 2 * rou) # used for CI prediction
} else{
I <- 0 # simulated as survived at the future year
}
# record the number of failured simulated for the future year
K_B[as.integer((tfur - tcut) / tdelta), i_bern] <-
K_B[as.integer((tfur - tcut) / tdelta), i_bern] + I
}
# sig_K <- sqrt(sig_K) # used for CI prediction
# ga_K <- sig_K ^ (-3) * ga_K # used for CI prediction
# Mu_K[tfur - 2020, i_bern] <- mu_K # used for CI prediction
# Sig_K[tfur - 2020, i_bern] <- sig_K # used for CI prediction
# Ga_K[tfur - 2020, i_bern] <- ga_K # used for CI prediction
}
}
library(Rlab)
# the number of bootstrap samples used for simulation
ndim_bern <- 10 # cannot be greater than numB
tcut <- 2020 # the cutting year, end of current observation
tpred <- 2030 # the end year of prediction
tdelta <-
1 # the accuracy of the prediction like 2021, 2022 or 2021.2, 2021.3..
# the number of samples still at risk (survived)
data_set_risk <- data_set[which(data_set$status == 0), ]
nrisk <- dim(data.matrix(data_set_risk[, "time"]))[1]
# initialize the matrix for saving the simulated number of failures
K_B <-
matrix(nrow = as.integer((tpred - tcut) / tdelta), ncol = ndim_bern)
# Mu_K <- matrix(nrow = 10, ncol = ndim_bern) # used for CI prediction
# Sig_K <- matrix(nrow = 10, ncol = ndim_bern) # used for CI prediction
# Ga_K <- matrix(nrow = 10, ncol = ndim_bern) # used for CI prediction
for (i_bern in 1:ndim_bern) {
# one bootstrap sample one full simulation
for (tfur in 2021:tdelta:tpred) {
# prediction from next year
K_B[as.integer((tfur - tcut) / tdelta), i_bern] <-
0 # initialize the predicted number
# Mu_K[tfur - 2020, i_bern] <- 0 # used for CI prediction
# Sig_K[tfur - 2020, i_bern] <- 0 # used for CI prediction
# Ga_K[tfur - 2020, i_bern] <- 0 # used for CI prediction
# mu_K <- 0 # used for CI prediction
# sig_K <- 0 # used for CI prediction
# ga_K <- 0 # used for CI prediction
for (idx_i in 1:nrisk) {
# get the current age of the transformer
tcur_i <- data.matrix(data_set_risk[, "time"])[idx_i]
# get the future age at future year tfur
tfur_i <- tfur - tcut + tcur_i
# get the conditional probability of failure at future year
rou <-
(exp(-(tcur_i / B[i_bern, 2]) ^ B[i_bern, 1]) - exp(-(tfur_i / B[i_bern, 2]) ^
B[i_bern, 1])) / (exp(-(tcur_i / B[i_bern, 2]) ^ B[i_bern, 1]))
# simulate the outcomes at future year with Bernoulli
if (runif(1) < dbern(1, rou, log = FALSE)) {
I <- 1 # simulated as failure at the future year
# mu_K <- mu_K + rou # used for CI prediction
# sig_K <- sig_K + rou * (1 - rou) # used for CI prediction
# ga_K <- ga_K + rou * (1 - rou) * (1 - 2 * rou) # used for CI prediction
} else{
I <- 0 # simulated as survived at the future year
# record the number of failured simulated for the future year
K_B[as.integer((tfur - tcut) / tdelta), i_bern] <-
K_B[as.integer((tfur - tcut) / tdelta), i_bern] + I
}
# sig_K <- sqrt(sig_K) # used for CI prediction
# ga_K <- sig_K ^ (-3) * ga_K # used for CI prediction
# Mu_K[tfur - 2020, i_bern] <- mu_K # used for CI prediction
# Sig_K[tfur - 2020, i_bern] <- sig_K # used for CI prediction
# Ga_K[tfur - 2020, i_bern] <- ga_K # used for CI prediction
}
}
}
View(K_B)
library(Rlab)
# the number of bootstrap samples used for simulation
ndim_bern <- 10 # cannot be greater than numB
tcut <- 2020 # the cutting year, end of current observation
tpred <- 2030 # the end year of prediction
tdelta <-
1 # the accuracy of the prediction like 2021, 2022 or 2021.2, 2021.3..
# the number of samples still at risk (survived)
data_set_risk <- data_set[which(data_set$status == 0), ]
nrisk <- dim(data.matrix(data_set_risk[, "time"]))[1]
# initialize the matrix for saving the simulated number of failures
K_B <-
matrix(nrow = as.integer((tpred - tcut) / tdelta), ncol = ndim_bern)
# Mu_K <- matrix(nrow = 10, ncol = ndim_bern) # used for CI prediction
# Sig_K <- matrix(nrow = 10, ncol = ndim_bern) # used for CI prediction
# Ga_K <- matrix(nrow = 10, ncol = ndim_bern) # used for CI prediction
for (i_bern in 1:ndim_bern) {
# one bootstrap sample one full simulation
for (tfur in seq(tcut + 1, tpred, tdelta) {
# prediction from next year
K_B[as.integer((tfur - tcut) / tdelta), i_bern] <-
0 # initialize the predicted number
# Mu_K[tfur - 2020, i_bern] <- 0 # used for CI prediction
# Sig_K[tfur - 2020, i_bern] <- 0 # used for CI prediction
# Ga_K[tfur - 2020, i_bern] <- 0 # used for CI prediction
# mu_K <- 0 # used for CI prediction
# sig_K <- 0 # used for CI prediction
# ga_K <- 0 # used for CI prediction
for (idx_i in 1:nrisk) {
# get the current age of the transformer
tcur_i <- data.matrix(data_set_risk[, "time"])[idx_i]
# get the future age at future year tfur
tfur_i <- tfur - tcut + tcur_i
# get the conditional probability of failure at future year
rou <-
(exp(-(tcur_i / B[i_bern, 2]) ^ B[i_bern, 1]) - exp(-(tfur_i / B[i_bern, 2]) ^
B[i_bern, 1])) / (exp(-(tcur_i / B[i_bern, 2]) ^ B[i_bern, 1]))
# simulate the outcomes at future year with Bernoulli
if (runif(1) < dbern(1, rou, log = FALSE)) {
I <- 1 # simulated as failure at the future year
# mu_K <- mu_K + rou # used for CI prediction
# sig_K <- sig_K + rou * (1 - rou) # used for CI prediction
# ga_K <- ga_K + rou * (1 - rou) * (1 - 2 * rou) # used for CI prediction
} else{
I <- 0 # simulated as survived at the future year
}
# record the number of failured simulated for the future year
K_B[as.integer((tfur - tcut) / tdelta), i_bern] <-
K_B[as.integer((tfur - tcut) / tdelta), i_bern] + I
}
# sig_K <- sqrt(sig_K) # used for CI prediction
# ga_K <- sig_K ^ (-3) * ga_K # used for CI prediction
# Mu_K[tfur - 2020, i_bern] <- mu_K # used for CI prediction
# Sig_K[tfur - 2020, i_bern] <- sig_K # used for CI prediction
# Ga_K[tfur - 2020, i_bern] <- ga_K # used for CI prediction
}
}
# the number of samples still at risk (survived)
data_set_risk <- data_set[which(data_set$status == 0),]
nrisk <- dim(data.matrix(data_set_risk[, "id"]))[1]
library(Rlab)
# the number of bootstrap samples used for simulation
ndim_bern <- 10 # cannot be greater than numB
tcut <- 2020 # the cutting year, end of current observation
tpred <- 2030 # the end year of prediction
tdelta <-
1 # the accuracy of the prediction like 2021, 2022 or 2021.2, 2021.3..
# the number of samples still at risk (survived)
data_set_risk <- data_set[which(data_set$status == 0),]
nrisk <- dim(data.matrix(data_set_risk[, "id"]))[1]
# initialize the matrix for saving the simulated number of failures
K_B <-
matrix(nrow = as.integer((tpred - tcut) / tdelta), ncol = ndim_bern)
# Mu_K <- matrix(nrow = 10, ncol = ndim_bern) # used for CI prediction
# Sig_K <- matrix(nrow = 10, ncol = ndim_bern) # used for CI prediction
# Ga_K <- matrix(nrow = 10, ncol = ndim_bern) # used for CI prediction
for (i_bern in 1:ndim_bern) {
# one bootstrap sample one full simulation
for (tfur in seq(tcut + 1, tpred, tdelta)) {
# prediction from next year
K_B[as.integer((tfur - tcut) / tdelta), i_bern] <-
0 # initialize the predicted number
# Mu_K[tfur - 2020, i_bern] <- 0 # used for CI prediction
# Sig_K[tfur - 2020, i_bern] <- 0 # used for CI prediction
# Ga_K[tfur - 2020, i_bern] <- 0 # used for CI prediction
# mu_K <- 0 # used for CI prediction
# sig_K <- 0 # used for CI prediction
# ga_K <- 0 # used for CI prediction
for (idx_i in 1:nrisk) {
# get the current age of the transformer
tcur_i <- data.matrix(data_set_risk[, "time"])[idx_i]
# get the future age at future year tfur
tfur_i <- tfur - tcut + tcur_i
# get the conditional probability of failure at future year
rou <-
(exp(-(tcur_i / B[i_bern, 2]) ^ B[i_bern, 1]) - exp(-(tfur_i / B[i_bern, 2]) ^
B[i_bern, 1])) / (exp(-(tcur_i / B[i_bern, 2]) ^ B[i_bern, 1]))
# simulate the outcomes at future year with Bernoulli
if (runif(1) < dbern(1, rou, log = FALSE)) {
I <- 1 # simulated as failure at the future year
# mu_K <- mu_K + rou # used for CI prediction
# sig_K <- sig_K + rou * (1 - rou) # used for CI prediction
# ga_K <- ga_K + rou * (1 - rou) * (1 - 2 * rou) # used for CI prediction
} else{
I <- 0 # simulated as survived at the future year
}
# record the number of failured simulated for the future year
K_B[as.integer((tfur - tcut) / tdelta), i_bern] <-
K_B[as.integer((tfur - tcut) / tdelta), i_bern] + I
}
# sig_K <- sqrt(sig_K) # used for CI prediction
# ga_K <- sig_K ^ (-3) * ga_K # used for CI prediction
# Mu_K[tfur - 2020, i_bern] <- mu_K # used for CI prediction
# Sig_K[tfur - 2020, i_bern] <- sig_K # used for CI prediction
# Ga_K[tfur - 2020, i_bern] <- ga_K # used for CI prediction
}
}
View(K_B)
View(K_B)
library(nleqslv)
nsample <- 100
interval <- matrix(ncol = 2)
for (sample in 1:nsample) {
f <- runif(num)
Tb <- list()
U <- matrix()
t_i <- time[sample]
for (prob in f) {
fn <- function(tx) {
return(prob - (exp(-(t_i / scale_lt) ^ shape_lt) - exp(-(tx / scale_lt) ^
shape_lt)) / (exp(-(t_i / scale_lt) ^ shape_lt)))
}
Tb <- rbind(Tb, nleqslv(100, fn)$x)
}
for (i in 1:num) {
u <-
(exp(-(t_i / B[i, 2]) ^ B[i, 1]) - exp(-(as.numeric(Tb[i]) / B[i, 2]) ^
B[i, 1])) / (exp(-(t_i / B[i, 2]) ^ B[i, 1]))
U <- rbind(U, c(u))
}
Ub <- U[2:num + 1,]
Ub_sd <- sd(Ub)
me <- qt(.95, 9) * sd(Ub) / sqrt(num)
u_l <- mean(Ub) - me
u_u <- mean(Ub) + me
fn_l <- function(tx) {
return(u_l - (exp(-(t_i / scale_lt) ^ shape_lt) - exp(-(tx / scale_lt) ^
shape_lt)) / (exp(-(t_i / scale_lt) ^ shape_lt)))
}
T_l <- nleqslv(100, fn_l)$x
fn_u <- function(tx) {
return(u_u - (exp(-(t_i / scale_lt) ^ shape_lt) - exp(-(tx / scale_lt) ^
shape_lt)) / (exp(-(t_i / scale_lt) ^ shape_lt)))
}
T_u <- nleqslv(100, fn_u)$x
interval <- rbind(interval, c(T_l, T_u))
}
interval_pred <- interval[1:nsample+1,]
View(interval_pred)
library(readxl)
data_set_raw <- read_excel("master_data.xlsx", sheet = "master")
data_set <- data_set_raw[which(data_set_raw$clear == 0),]
id <- data.matrix(data_set[, "id"])
manuf <- as.factor(data.matrix(data_set[, "manuf"]))
time <- data.matrix(data_set[, "time"])
time_lt <- data.matrix(data_set[, "time_lt"])
status <- data.matrix(data_set[, "status"])
trunc <- data.matrix(data_set[, "trunc"])
ndim <- dim(id)[1]
library(survival)
library(ggplot2)
library(survminer)
library(Metrics)
library(maxLik)
logLikFun <- function(param) {
beta <- param[1]
ita <- param[2]
c <- status[1:ndim]
v <- trunc[1:ndim]
t <- time[1:ndim]
tL <- time_lt[1:ndim]
f = log(beta / ita) + (beta - 1) * log(t / ita) - (t / ita) ^ beta
fu = (t / ita) ^ beta
fuL = (tL / ita) ^ beta
sum(c * v * f + c * (1 - v) * (f + fuL) + (1 - c) * v * (-fu) + (1 - c) *
(1 - v) * (fuL - fu))
}
mle <- maxLik(logLik = logLikFun, start = c(beta = 2, ita = 100))
summary(mle)
shape_lt <- mle$estimate[1]
scale_lt <- mle$estimate[2]
# The random weighted bootstrap
# to generate bootstrap samples of the scale and shape parameters
# initialize the matrix for saving the bootstrap samples
B <- matrix(ncol = 2)
numB <- 100 # the number of bootstrap sample
for (val in 1:numB)
{
# Simulate random values Zi that are i.i.d. from a Gamma distribution
z <- rgamma(n = ndim, shape = 1, scale = 1)
# The random weighted likelihood function
logLikFun <- function(param) {
beta <- param[1]
ita <- param[2]
c <- status[1:ndim]
v <- trunc[1:ndim]
t <- time[1:ndim]
tL <- time_lt[1:ndim]
f = log(beta / ita) + (beta - 1) * log(t / ita) - (t / ita) ^ beta
fu = (t / ita) ^ beta
fuL = (tL / ita) ^ beta
sum(z * (
c * v * f + c * (1 - v) * (f + fuL) + (1 - c) * v * (-fu) + (1 - c) * (1 -
v) * (fuL - fu)
))
}
# MLE using maxLik
mle_b <-
maxLik(logLik = logLikFun, start = c(beta = 2, ita = 100))
# obtain the bootstrap samples and store in B
shape_lt_b <- mle_b$estimate[1]
scale_lt_b <- mle_b$estimate[2]
B <- rbind(B, c(shape_lt_b, scale_lt_b))
}
# The final bootstrap samples (empty values removed)
B <- B[1:numB + 1,]
View(B)
library(nleqslv)
# the number of transformer to predict, this can be customized
nsample <- 100
interval <- matrix(ncol = 2)
for (sample in 1:nsample) {
# the probability generated from uniform distribution for generating Tib
f <-
runif(numB) # the number should be aligned with bootstrap samples
# initialize the list for saving Tib
Tb <- list()
# initialize the matrix for saving Uib
U <- matrix()
# the current age of this sample transformer
t_i <- time[sample]
# simulate Tib from distribution the conditional probability of sample i
for (prob in f) {
fn <- function(tx) {
return(prob - (exp(-(t_i / scale_lt) ^ shape_lt) - exp(-(tx / scale_lt) ^
shape_lt)) / (exp(-(t_i / scale_lt) ^ shape_lt)))
}
Tb <- rbind(Tb, nleqslv(100, fn)$x)
}
# compute Uib with Tib and bootstrap samples
for (i in 1:numB) {
u <-
(exp(-(t_i / B[i, 2]) ^ B[i, 1]) - exp(-(as.numeric(Tb[i]) / B[i, 2]) ^
B[i, 1])) / (exp(-(t_i / B[i, 2]) ^ B[i, 1]))
U <- rbind(U, c(u))
}
Ub <- U[1:numB + 1,] # save the computed Uib
# compute the lower and upper alpha/2 sample quantiles of Uib
Ub_sd <- sd(Ub)
me <- qt(.95, 9) * sd(Ub) / sqrt(numB)
u_l <- mean(Ub) - me
u_u <- mean(Ub) + me
# compute the lower bound of the predicted interval
fn_l <- function(tx) {
return(u_l - (exp(-(t_i / scale_lt) ^ shape_lt) - exp(-(tx / scale_lt) ^
shape_lt)) / (exp(-(t_i / scale_lt) ^ shape_lt)))
}
T_l <- nleqslv(100, fn_l)$x
# compute the upper bound of the predicted interval
fn_u <- function(tx) {
return(u_u - (exp(-(t_i / scale_lt) ^ shape_lt) - exp(-(tx / scale_lt) ^
shape_lt)) / (exp(-(t_i / scale_lt) ^ shape_lt)))
}
T_u <- nleqslv(100, fn_u)$x
# save the prediction interval
interval <- rbind(interval, c(T_l, T_u))
}
# the prediction of remaining life with 90% confidence interval for individuals
interval_pred <- interval[1:nsample + 1,]
View(interval_pred)
library(Rlab)
# the number of bootstrap samples used for simulation
ndim_bern <- 100 # cannot be greater than numB
tcut <- 2020 # the cutting year, end of current observation
tpred <- 2030 # the end year of prediction
tdelta <-
1 # the accuracy of the prediction like 2021, 2022 or 2021.2, 2021.3..
# the number of samples still at risk (survived)
data_set_risk <- data_set[which(data_set$status == 0),]
nrisk <- dim(data.matrix(data_set_risk[, "id"]))[1]
# initialize the matrix for saving the simulated number of failures
K_B <-
matrix(nrow = as.integer((tpred - tcut) / tdelta), ncol = ndim_bern)
# Mu_K <- matrix(nrow = 10, ncol = ndim_bern) # used for CI prediction
# Sig_K <- matrix(nrow = 10, ncol = ndim_bern) # used for CI prediction
# Ga_K <- matrix(nrow = 10, ncol = ndim_bern) # used for CI prediction
for (i_bern in 1:ndim_bern) {
# one bootstrap sample one full simulation
for (tfur in seq(tcut + 1, tpred, tdelta)) {
# prediction from next year
K_B[as.integer((tfur - tcut) / tdelta), i_bern] <-
0 # initialize the predicted number
# Mu_K[tfur - 2020, i_bern] <- 0 # used for CI prediction
# Sig_K[tfur - 2020, i_bern] <- 0 # used for CI prediction
# Ga_K[tfur - 2020, i_bern] <- 0 # used for CI prediction
# mu_K <- 0 # used for CI prediction
# sig_K <- 0 # used for CI prediction
# ga_K <- 0 # used for CI prediction
for (idx_i in 1:nrisk) {
# get the current age of the transformer
tcur_i <- data.matrix(data_set_risk[, "time"])[idx_i]
# get the future age at future year tfur
tfur_i <- tfur - tcut + tcur_i
# get the conditional probability of failure at future year
rou <-
(exp(-(tcur_i / B[i_bern, 2]) ^ B[i_bern, 1]) - exp(-(tfur_i / B[i_bern, 2]) ^
B[i_bern, 1])) / (exp(-(tcur_i / B[i_bern, 2]) ^ B[i_bern, 1]))
# simulate the outcomes at future year with Bernoulli
if (runif(1) < dbern(1, rou, log = FALSE)) {
I <- 1 # simulated as failure at the future year
# mu_K <- mu_K + rou # used for CI prediction
# sig_K <- sig_K + rou * (1 - rou) # used for CI prediction
# ga_K <- ga_K + rou * (1 - rou) * (1 - 2 * rou) # used for CI prediction
} else{
I <- 0 # simulated as survived at the future year
}
# record the number of failured simulated for the future year
K_B[as.integer((tfur - tcut) / tdelta), i_bern] <-
K_B[as.integer((tfur - tcut) / tdelta), i_bern] + I
}
# sig_K <- sqrt(sig_K) # used for CI prediction
# ga_K <- sig_K ^ (-3) * ga_K # used for CI prediction
# Mu_K[tfur - 2020, i_bern] <- mu_K # used for CI prediction
# Sig_K[tfur - 2020, i_bern] <- sig_K # used for CI prediction
# Ga_K[tfur - 2020, i_bern] <- ga_K # used for CI prediction
}
}
View(K_B)
View(interval_pred)
library(readxl)
data_set_raw <- read_excel("master_data.xlsx", sheet = "master")
data_set <- data_set_raw[which(data_set_raw$clear == 0),]
id <- data.matrix(data_set[, "id"])
manuf <- as.factor(data.matrix(data_set[, "manuf"]))
time <- data.matrix(data_set[, "time"])
time_lt <- data.matrix(data_set[, "time_lt"])
status <- data.matrix(data_set[, "status"])
trunc <- data.matrix(data_set[, "trunc"])
ndim <- dim(id)[1]