Skip to content
This repository has been archived by the owner on Jan 21, 2025. It is now read-only.

Develop surv estimating functions #1

Open
wants to merge 55 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
cb088d7
Create surv_estimating_functions.R
May 27, 2024
2568e84
added survival_treatment_level_estimating_functions: type = "prob"
May 29, 2024
440c051
Update surv_estimating_functions.R
May 31, 2024
438e8e8
init bug fixes in cumhaz
May 31, 2024
9d6b2f6
bug fixes for coxph.null in cumhaz + testthat
Jun 4, 2024
c4bc0b9
Update test-cumhaz.R
Jun 11, 2024
3949a4c
updated cumhaz - transposed chf outcome
Jun 14, 2024
95320cc
updated surv_estimating_functions to account for updates on cumhaz
Jun 14, 2024
161a9a5
Index bug in surv_estimating_functions
Jun 14, 2024
919890d
Create test-surv_estimating_functions.R
Jun 14, 2024
bd72e74
In surv_estimating_functions: fixed use of factor levels
Jun 14, 2024
2b5bf26
added tests for surv_estimating_functions
Jun 14, 2024
05acc35
init roxy: survival_treatment_estimating_functions
Jun 18, 2024
138826a
update cumhaz test
Jun 18, 2024
09008ba
init estimating functions for the rmst
Jun 18, 2024
d148641
init intsurv3
Jun 18, 2024
3e9e264
cumhaz for survfit objects
Jun 19, 2024
a51dc05
fixed bug in cumhaz for coxph.null objects
Jun 19, 2024
5d16218
Merge branch 'develop_cumhaz' into develop_intsurv
Jun 19, 2024
8fb8673
update int_surv
Jun 21, 2024
5d1532a
Update cumhaz.R
Jun 21, 2024
ef9ff35
Merge branch 'develop_cumhaz' into develop_surv_estimating_functions
Jun 21, 2024
1ba633b
Merge branch 'develop_intsurv' into develop_surv_estimating_functions
Jun 21, 2024
6d0d4b6
H_constructor
Jun 24, 2024
1b231fb
H_constructor_rmst
Jun 25, 2024
5160315
bug fix H_constructor_rmst
Jun 25, 2024
25bb933
bug fix int_surv
Jun 25, 2024
3dc4f3f
included sample option in H_constructor_rmst
Jun 25, 2024
e5725f8
develop: switch from `testthat` to `tinytest` for unit testing of R p…
benesom Nov 26, 2024
9635e54
Merge branch 'develop' of gh-private:kkholst/target into feature/chan…
benesom Nov 27, 2024
2e69b12
Merge branch 'feature/change-testthat-to-tinytest' into feature/repla…
benesom Nov 27, 2024
5e1e33f
wip
benesom Nov 27, 2024
185dd9f
adding packages used in test_cumhaz to description file
benesom Nov 27, 2024
0b3ea9c
remove expect_no_error because test will fail if error occurs in unde…
benesom Nov 27, 2024
027a87c
some tests in test_rcai.R seem to be missing
benesom Nov 27, 2024
a4a839a
done
benesom Nov 27, 2024
7def1b8
Merge branch 'develop' of gh-private:kkholst/target into develop_surv…
benesom Nov 28, 2024
dbea1f7
minor
benesom Nov 28, 2024
d0dd437
Merge branch 'develop' of gh-private:kkholst/target into feature/repl…
benesom Nov 28, 2024
0aec169
Merge branch 'develop_surv_estimating_functions' of gh-private:kkhols…
benesom Nov 28, 2024
b1b9a2c
adding suppressPackageStartupMessages
benesom Nov 28, 2024
9fed94e
minor
benesom Nov 28, 2024
cb9690b
minor
benesom Nov 28, 2024
93d6e30
updating R package manuals
benesom Nov 28, 2024
29ae1e1
Merge branch 'develop_surv_estimating_functions' of gh-private:kkhols…
benesom Nov 28, 2024
8181ce4
use ::: for internal functions
benesom Nov 29, 2024
50e8278
develop: removing `devtools::load_all` from test rule and use only `t…
benesom Nov 29, 2024
8c76b87
Merge branch 'feature/change-rtest-make-rule' into feature/replace-te…
benesom Nov 29, 2024
3437108
export cumhaz and perform minor formatting to cumhaz.R
benesom Nov 29, 2024
7a85064
Merge branch 'develop_surv_estimating_functions' into feature/replace…
benesom Nov 29, 2024
4dc25a2
minor
benesom Nov 29, 2024
308140c
all test run
benesom Nov 29, 2024
da5be56
reset against base branch
benesom Nov 29, 2024
3aef1da
remove double hashes
benesom Jan 3, 2025
24ae70a
Merge pull request #7 from kkholst/feature/replace-testthat-on-develo…
benesom Jan 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion R-package/targeted/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ Suggests:
SuperLearner (>= 2.0-28),
knitr,
xgboost,
viridisLite
viridisLite,
ranger,
randomForestSRC
BugReports: https://github.com/kkholst/targeted/issues
URL: https://kkholst.github.io/targeted/
License: Apache License (== 2.0)
Expand Down
2 changes: 2 additions & 0 deletions R-package/targeted/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export(calibration)
export(cate)
export(cate_link)
export(crr)
export(cumhaz)
export(cv)
export(design)
export(expand.list)
Expand Down Expand Up @@ -84,6 +85,7 @@ import(Rcpp)
import(methods)
importFrom(R6,R6Class)
importFrom(data.table,data.table)
importFrom(data.table,dcast)
importFrom(data.table,is.data.table)
importFrom(grDevices,nclass.Sturges)
importFrom(graphics,abline)
Expand Down
131 changes: 131 additions & 0 deletions R-package/targeted/R/H_constructor.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
## H(u|X_i, A_i) = E[I\{T_i \leq \tau\} | T_i > u, X_i, A_i] = I\{u \leq \tau \} \frac{S(u|X_i, A_i) - S(\tau|X_i, A_i)}{S(u|X_i, A_i)}
H_constructor_risk <- function(T_model, tau, individual_time, ...) {
force(T_model)
force(tau)
force(individual_time)

H <- function(u, data) {
S <- cumhaz(
T_model,
newdata = data,
times = u,
individual.time = individual_time
)$surv

S_tau <- cumhaz(
T_model,
newdata = data,
times = tau,
individual_time = individual_time
)$surv
S_tau <- as.vector(S_tau)


if (individual_time == FALSE) {
res <- apply(S, 2, function(x) x - S_tau)
res <- res / S
indicator <- (u <= tau)
res <- apply(res, 1, function(x) x * indicator)
res <- t(res)
} else {
res <- (S - S_tau) / S * (u <= tau)
res <- as.vector(res)
}
return(res)
}
return(H)
}

## Hu: H(u|X_i, A_i) = E[I\{T_i > \tau\} | T_i \geq u, X_i, A_i] = I\{u \leq \tau \} \frac{S(\tau|X_i, A_i)}{S(u|X_i, A_i)} + I\{u > \tau \}
H_constructor_surv <- function(T_model, tau, individual_time, ...) {
force(T_model)
force(tau)
force(individual_time)

H <- function(u, data) {
S <- cumhaz(
T_model,
newdata = data,
times = u,
individual.time = individual_time
)$surv

S_tau <- cumhaz(
T_model,
newdata = data,
times = tau,
individual_time = individual_time
)$surv
S_tau <- as.vector(S_tau)

if (individual_time == FALSE) {
res <- 1 / S
res <- apply(res, 2, function(x) x * S_tau)
indicator_1 <- (u <= tau)
indicator_2 <- (u > tau)
res <- apply(res, 1, function(x) x * indicator_1 + indicator_2)
res <- t(res)
} else {
res <- S_tau / S * (u <= tau) + (u > tau)
res <- as.vector(res)
}

return(res)
}
return(H)
}

## Hu: H_\tau(u|X_i, A_i) = E[\min(T, \tau) | T_i \geq u, X_i, A_i] = u + \frac{1}{S(u|X,A)} \int_u^\tau S(t|X,A) dt
H_constructor_rmst <- function(T_model, time, event, tau, individual_time, sample = 0) {
force(T_model)
force(tau)
force(individual_time)
force(time)
force(event)
force(sample)

H <- function(u, data) {
S <- cumhaz(
T_model,
newdata = data,
times = u,
individual.time = individual_time
)$surv

tt <- time[event == 1]
if (sample > 0) {
tt <- subjumps(tt, size = sample, tau = tau)
}

S_T <- cumhaz(
T_model,
newdata = data,
times = tt,
individual.time = FALSE
)$surv
if (individual_time == FALSE) {
int_S <- apply(
S_T,
1,
function(x) {
int_surv(times = tt, surv = x, start = u, stop = tau, extend = FALSE)
},
simplify = FALSE
)
int_S <- do.call(what = "rbind", int_S)
res <- 1 / S
res <- res * int_S
res <- apply(res, 1, function(x) x + pmin(u, tau))
res <- t(res)
} else {
int_S <- numeric(length = length(u))
for (k in seq_along(u)) {
int_S[k] <- int_surv(times = tt, surv = S_T[k, ], start = u[k], stop = tau, extend = FALSE)
}
res <- pmin(u, tau) + 1 / S * int_S
}
return(res)
}

return(H)
}
44 changes: 4 additions & 40 deletions R-package/targeted/R/RATE.R
Original file line number Diff line number Diff line change
Expand Up @@ -328,53 +328,17 @@ RATE.surv <- function(response, post.treatment, treatment, censoring,
return(out)
}

cumhaz <- function(object, newdata, times=NULL, ...) {
if (inherits(object, "phreg")) {
if (is.null(times)) times <- object$times
pp <- predict(object, newdata=newdata,
times=times,
individual.times=FALSE, ...)
chf <- t(pp$cumhaz)
tt <- pp$times
} else if (inherits(object, "rfsrc")) {
pp <- predict(object, newdata=newdata, oob=TRUE, ...)
chf <- t(rbind(pp$chf))
tt <- pp$time.interest
if (!is.null(times)) {
idx <- mets::fast.approx(tt, times)
chf <- chf[idx,,drop=FALSE]
tt <- times
}
} else if (inherits(object, "ranger")) {
num.threads <- object$call$num.threads
pp <- predict(object, type="response", data=newdata, num.threads = num.threads, ...)
chf <- t(rbind(pp$chf))
tt <- pp$unique.death.times
if (!is.null(times)) {
idx <- mets::fast.approx(tt, times)
chf <- chf[idx,,drop=FALSE]
tt <- times
}
} else if (inherits(object, "coxph")) {
pp <- survfit(object, newdata=newdata)
pp <- summary(pp, time=times)
chf <- rbind(pp$cumhaz)
tt <- pp$time
}
list(time=tt, chf=chf, surv=exp(-chf), dchf=diff(rbind(0,chf)))
}

F.tau <- function(T.est, D.est, data, tau, a, treatment, post.treatment){

data[lava::getoutcome(treatment)] <- a
pred.D <- predict(D.est, type = "response", data)

data[lava::getoutcome(post.treatment)] <- 1
surv.T.D1 <- cumhaz(T.est, newdata = data, times = tau)$surv[1,]
surv.T.D1 <- cumhaz(T.est, newdata = data, times = tau)$surv[ , 1]

data[lava::getoutcome(post.treatment)] <- 0
surv.T.D0 <- cumhaz(T.est, newdata = data, times = tau)$surv[1,]

surv.T.D0 <- cumhaz(T.est, newdata = data, times = tau)$surv[ , 1]
g
surv <- pred.D * surv.T.D1 + (1 - pred.D) * surv.T.D0

f.tau <- 1 - surv
Expand All @@ -392,7 +356,7 @@ HMc.tau <- function(T.est, C.est, data, time, event, tau){
time.C <- time[event == 0]

S <- diag(cumhaz(T.est, newdata = data.C, times = time.C)$surv)
S.tau <- cumhaz(T.est, newdata = data.C, times = tau)$surv[1,]
S.tau <- cumhaz(T.est, newdata = data.C, times = tau)$surv[ , 1]
Sc <- diag(cumhaz(C.est, newdata = data.C, times = time.C)$surv)
stopifnot(all(S * Sc> 0))

Expand Down
Loading
Loading