Skip to content

Commit

Permalink
added "Aalen" "Johansen" to wordlist
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuenzel committed Feb 6, 2024
1 parent 85a1c90 commit e2441fc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
21 changes: 12 additions & 9 deletions R/aalen_johansen.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,19 @@ aalen_johansen <- function(data,
help$to <- ifelse(type2 != 2, "cens", type2 - 1)
etmmm <- etm::etm(help, state_names, tra, "cens", s = 0)
setmm <- summary(etmmm)[[2]]
ce_prob <- setmm[sum(setmm$time <= tau), ]$P
ce_prob_var <- setmm[sum(setmm$time <= tau), ]$var
trans_mat_tau <- setmm[sum(setmm$time <= tau), ]
ce_prob <- trans_mat_tau$P
ce_prob_var <- trans_mat_tau$var
}


if (c1 != 0 && c2 == 0) {
help$to <- ifelse(type2 != 1, "cens", type2)
etmmm <- etm::etm(help, state_names, tra, "cens", s = 0)
setmm <- summary(etmmm)[[2]]

ae_prob <- setmm[sum(setmm$time <= tau), ]$P
ae_prob_var <- setmm[sum(setmm$time <= tau), ]$var
trans_mat_tau <- setmm[sum(setmm$time <= tau), ]
ae_prob <- trans_mat_tau$P
ae_prob_var <- trans_mat_tau$var
}

if (c1 != 0 && c2 != 0) {
Expand All @@ -106,11 +107,13 @@ aalen_johansen <- function(data,
etmmm <- etm::etm(help, state_names, tra, "cens", s = 0)
setmm <- summary(etmmm)

ae_prob <- setmm[[2]][sum(setmm[[2]]$time <= tau), ]$P
ae_prob_var <- setmm[[2]][sum(setmm[[2]]$time <= tau), ]$var
trans_mat_tau_ae <- setmm[[2]][sum(setmm[[2]]$time <= tau), ]
ae_prob <- trans_mat_tau_ae$P
ae_prob_var <- trans_mat_tau_ae$var

ce_prob <- setmm[[3]][sum(setmm[[3]]$time <= tau), ]$P
ce_prob_var <- setmm[[3]][sum(setmm[[3]]$time <= tau), ]$var
trans_mat_tau_ce <- setmm[[3]][sum(setmm[[3]]$time <= tau), ]
ce_prob <- trans_mat_tau_ce$P
ce_prob_var <- trans_mat_tau_ce$var
}


Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-aalen_johansen.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,20 @@ test_that("Aalen Johansen works without competing events", {
expected <- c(ae_prob = 0.5897, ae_prob_var = 0.0404, ce_prob = 0, ce_prob_var = 0)
expect_equal(result, expected, tolerance = 1e-4)
})

test_that("Aalen Johansen works without any events", {
set.seed(23)
df <- generate_data(
n = 25,
cens = c(0.2, 3),
haz_ae = 0.2,
haz_death = 0.3,
haz_soft = 0.5
)
df <- df[df$type_of_event != 1, ]
df <- df[df$type_of_event != 2, ]
df <- df[df$type_of_event != 3, ]
result <- aalen_johansen(data = df, ce = 2, tau = 4)
expected <- c(ae_prob = 0, ae_prob_var = 0, ce_prob = 0, ce_prob_var = 0)
expect_equal(result, expected, tolerance = 1e-4)
})

0 comments on commit e2441fc

Please sign in to comment.