Nothing
#' Takeuchi information criterion over the transformation parameter
#'
#' Fits the Box-Cox and logarithmic transformation models over grids of the
#' transformation parameter and plots TIC and AIC on the log-likelihood scale,
#' relative to their optimum: \eqn{-\Delta\mathrm{TIC}/2} (solid, filled
#' circles) and \eqn{-\Delta\mathrm{AIC}/2} (dashed, open circles), as in
#' Figure 2 of Bellach and Kosorok (2026); see \code{\link{TIC}}. The maximum
#' of the solid curve is the model with the smallest TIC. \eqn{r \to 0} and
#' \eqn{\rho = 1} give the Ghosh-Lin model (the Fine-Gray model for
#' \code{type = "cmprsk"}), and \eqn{\rho \to 0} and \eqn{r = 1} the
#' proportional odds model. Further arguments such as \code{type = "cmprsk"}
#' are passed to \code{\link{wnpmle_fit}} via \code{...}.
#'
#' Every grid point needs a fit with the censoring-corrected sandwich and a
#' fit with the inverse Fisher information, so this is much slower than
#' \code{\link{plot_loglik}}; the default grids are therefore coarser.
#'
#' @inheritParams plot_loglik
#' @param rho_grid Box-Cox parameter values (default \code{seq(0.05, 1.2, by = 0.05)}).
#' @param r_grid Logarithmic parameter values (default \code{seq(0.05, 1.2, by = 0.05)}).
#' @param show_loglik Logical; if \code{TRUE} (default), AIC (the ordinary
#' log-likelihood, as in \code{\link{plot_loglik}}) is added in grey.
#'
#' @return A data frame of class \code{"wnpmle_profile"} with columns
#' \code{model}, \code{param}, \code{loglik}, \code{penalty}, \code{p},
#' \code{AIC} and \code{TIC}, invisibly. Save it (\code{res <- plot_tic(...)})
#' to redraw the figure with \code{\link{plot.wnpmle_profile}} without
#' refitting.
#'
#' @seealso \code{\link{TIC}}, \code{\link{plot_loglik}}
#'
#' @examples
#' \donttest{
#' dat <- bladder_prep()
#' res <- plot_tic(Surv(time, status) ~ treat + num + size, data = dat,
#' id = "id", rho_grid = c(0.5, 1), r_grid = c(0.5, 1))
#' res
#' }
#'
#' @export
plot_tic <- function(formula, data, id = "id",
rho_grid = seq(0.05, 1.2, by = 0.05),
r_grid = seq(0.05, 1.2, by = 0.05),
tau = NULL,
mark_points = TRUE,
show_loglik = TRUE,
layout = NULL,
legend = TRUE,
file = NULL,
width = NULL,
height = NULL,
verbose = TRUE,
...) {
# TIC (with details) for one model; NA row if a fit fails
one_tic <- function(model, p_val, init_beta) {
fs <- tryCatch(wnpmle_fit(formula, data = data, id = id, model = model,
rho = p_val, tau = tau, se = "sandwich_adj",
init_beta = init_beta, ...),
error = function(e) NULL)
if (is.null(fs)) return(list(res = rep(NA_real_, 5), beta = init_beta))
if (!is.null(fs$tic_penalty) && is.finite(fs$tic_penalty)) {
d <- tryCatch(suppressWarnings(TIC(fs, details = TRUE)),
error = function(e) rep(NA_real_, 5))
return(list(res = as.numeric(d[c("loglik", "penalty", "p", "AIC", "TIC")]),
beta = fs$coefficients))
}
ff <- tryCatch(wnpmle_fit(formula, data = data, id = id, model = model,
rho = p_val, tau = tau, se = "fisher",
init_beta = fs$coefficients, ...),
error = function(e) NULL)
if (is.null(ff)) return(list(res = rep(NA_real_, 5), beta = fs$coefficients))
d <- tryCatch(suppressWarnings(TIC(fs, fisher = ff, details = TRUE)),
error = function(e) rep(NA_real_, 5))
list(res = as.numeric(d[c("loglik", "penalty", "p", "AIC", "TIC")]),
beta = fs$coefficients)
}
run_grid <- function(model, grid, label) {
if (verbose) cat("Fitting", label, "grid (", length(grid), "models)...\n")
out <- matrix(NA_real_, length(grid), 5,
dimnames = list(NULL, c("loglik", "penalty", "p", "AIC", "TIC")))
init_beta <- NULL
for (k in seq_along(grid)) {
o <- one_tic(model, grid[k], init_beta)
out[k, ] <- o$res
init_beta <- o$beta # warm start
if (verbose && k %% 5 == 0) cat(" ", label, ":", k, "/", length(grid), "\n")
}
out
}
res_BC <- run_grid("boxcox", rho_grid, "Box-Cox")
res_log <- run_grid("log", r_grid, "Log")
# ---- report optima (smallest TIC) ----
out <- data.frame(
model = c(rep("log", length(r_grid)), rep("boxcox", length(rho_grid))),
param = c(r_grid, rho_grid),
rbind(res_log, res_BC),
row.names = NULL
)
best <- function(m) {
s <- out[out$model == m & is.finite(out$TIC), ]
s[which.min(s$TIC), ]
}
b_bc <- best("boxcox"); b_lg <- best("log")
cat("\nTransformation parameters with the smallest TIC:\n")
print(data.frame(
Model = .model_labels(list(...)$type),
Parameter = c(paste("rho =", b_bc$param), paste("r =", b_lg$param)),
TIC = round(c(b_bc$TIC, b_lg$TIC), 3),
AIC = round(c(b_bc$AIC, b_lg$AIC), 3)
), row.names = FALSE)
best_ll <- function(m) {
s <- out[out$model == m & is.finite(out$loglik), ]
s$param[which.max(s$loglik)]
}
cat(sprintf("For comparison, largest log-likelihood (smallest AIC): rho = %s, r = %s\n",
best_ll("boxcox"), best_ll("log")))
type <- list(...)$type
if (is.null(layout)) layout <- "joint"
out <- .as_profile(out, layout, type)
plot(out, layout = layout, show_loglik = show_loglik, mark_points = mark_points,
legend = legend, file = file, width = width, height = height)
invisible(out)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.