-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctions.R
More file actions
76 lines (56 loc) · 1.87 KB
/
Functions.R
File metadata and controls
76 lines (56 loc) · 1.87 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
# This document contains functios neccessary to run the main analysis
# life table simulation
sim_lt <- function(eqx,eta,n){
# period life table starting quantities
dat <- data.frame(
age = eta,
qx = eqx,
nr = length(eqx),
ax = c(rep(n/2,length(eta)-1), NA),
lx = c(500000,rep(0,(length(eta)-1))),
px = 1 - eqx,
Lx = NA)
# simulate lifetimes (elimination by death)
for (i in 1:(nrow(dat)-1)) {
## draw random prob for each individual from the cohort
threshold <- runif(dat$lx[i])
## compare with age-spec prob from the models
lx.test <- as.numeric(dat$qx[i] > threshold)
## survived to the next age interval is the subtraction of deaths
dat$lx[i + 1] <- dat$lx[i] - sum(lx.test)
## Number of person-years lived between ages x and x+n
dat$Lx[i] <- n*(dat$lx[i + 1]) + dat$ax[i] * sum(lx.test)
}
dat$Lx[nrow(dat)] <- 0
dat$Tx <- rev(cumsum(rev(dat$Lx)))
dat$ex <- dat$Tx/dat$lx
return(dat)
}
# DFLE formula, given Sullivan method
exDF <- function(lx, wx, Lx){
dfle <- 1/lx[1]*sum((1-wx)*Lx)
return(dfle)
}
# re-sampling for bootstraps
## data needs to be in data.table format
long_resample <- function(dat, id, time) {
id = substitute(id)
time = substitute(time)
tmp <- split(dat, by = id)
idx <- unique(dat[[id]])
resample_id <- sample(idx, length(idx), replace = TRUE)
resample_dat <- lapply(seq_along(resample_id), function(x) {
res_dat <- tmp[[as.character(resample_id[x])]]
res_dat$new_id <- x
return(res_dat)
})
resample_dat <- do.call(rbind, resample_dat)
resample_dat
}
# confidence intervals
confidence_interval <- function(vector) {
estimate <- mean(vector)
ci <- quantile(vector, probs = c(.025,.975),type=8)
result <- c(estimate, ci)
return(result)
}