Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
drizopoulos committed Jan 3, 2024
1 parent 6789e30 commit f439a78
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
41 changes: 41 additions & 0 deletions R/accuracy_measures.R
Original file line number Diff line number Diff line change
Expand Up @@ -1235,3 +1235,44 @@ create_folds <- function (data, V = 5, id_var = "id", seed = 123L) {
}
list("training" = training, "testing" = testing)
}

create_folds <- function(data, V = 5, id_var = "id", strata = NULL,
seed = 123L) {
if (!exists(".Random.seed", envir = .GlobalEnv))
runif(1L)
RNGstate <- get(".Random.seed", envir = .GlobalEnv)
on.exit(assign(".Random.seed", RNGstate, envir = .GlobalEnv))
set.seed(seed)
data <- as.data.frame(data)
if (!(any(names(data) == id_var))) {
stop("The variable specified in the 'id_var' argument cannot be found in ",
"'data'.\n") }
training <- validation <- vector("list", V)
temp_data <- data[!duplicated(data[[id_var]]) ,]
if (is.null(strata)) {
temp_data$fold <- runif(n = nrow(temp_data))
} else {
if(!(all(strata %in% names(data))))
stop("One or multiple variable(s) specified in the 'strata' argument ",
"cannot be found in 'data'.\n")
for(i in strata) {
temp_data[[i]] <- factor(temp_data[[i]])
}
temp_data$interaction <- interaction(temp_data[strata])
split_data <- split(x = temp_data, f = temp_data$interaction)
split_data[] <- lapply(split_data,
function (x) cbind(x, fold = runif(n = nrow(x))))
temp_data <- do.call("rbind", split_data)
}
init_step <- 0
step_size <- 1 / V
for(j in seq_len(V)){
ind <- temp_data[[id_var]][temp_data$fold >= init_step &
temp_data$fold < init_step + step_size]
training[[j]] <- data[!data[[id_var]] %in% ind, ]
validation[[j]] <- data[data[[id_var]] %in% ind, ]
init_step <- init_step + step_size
}
list("training" = training, "testing" = validation)
}

4 changes: 3 additions & 1 deletion man/accuracy.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ tvEPCE(object, newdata, Tstart, Thoriz = NULL, Dt = NULL, eps = 0.001,
parallel = c("snow", "multicore"),
cores = parallelly::availableCores(omit = 1L), \dots)

create_folds(data, V = 5, id_var = "id", seed = 123L)
create_folds(data, V = 5, id_var = "id", strata = NULL, seed = 123L)
}

\arguments{
Expand Down Expand Up @@ -96,6 +96,8 @@ predicted probabilities used in the Cox model that assess calibration.}

\item{id_var}{character string denoting the name of the subject id variable in \code{data}.}

\item{strata}{character vector with the names of stratifying variables.}

\item{seed}{integer denoting the seed.}

\item{\dots}{additional arguments passed to \code{predict.jm()}.}
Expand Down

0 comments on commit f439a78

Please sign in to comment.