rcs_plot {clinpubr}R Documentation

Plot restricted cubic spline

Description

Plot restricted cubic spline based on package rms. Support both logistic and cox model.

Usage

rcs_plot(
  data,
  x,
  y,
  time = NULL,
  covars = NULL,
  knot = 4,
  add_hist = TRUE,
  ref = "x_median",
  ref_digits = 3,
  group_by_ref = TRUE,
  group_title = NULL,
  group_labels = NULL,
  group_colors = NULL,
  breaks = 20,
  rcs_color = "#e23e57",
  print_p_ph = TRUE,
  trans = "identity",
  save_plot = FALSE,
  filename = NULL,
  y_lim = NULL,
  hist_max = NULL,
  xlim = NULL,
  height = 6,
  width = 6,
  return_details = FALSE
)

Arguments

data

A data frame.

x

A character string of the predictor variable.

y

A character string of the outcome variable.

time

A character string of the time variable. If NULL, logistic regression is used. Otherwise, Cox proportional hazards regression is used.

covars

A character vector of covariate names.

knot

The number of knots. If NULL, the number of knots is determined by AIC minimum.

add_hist

A logical value. If TRUE, add histogram to the plot.

ref

The reference value for the plot. Could be "x_median", "x_mean", "ratio_min", or a numeric value. If "x_median", the median of the predictor variable is used. If "ratio_min", the value of the predictor variable that has the minium predicted risk is used. If a numeric value, that value is used.

ref_digits

The number of digits for the reference value.

group_by_ref

A logical value. If TRUE, split the histogram at the reference value from ref into two groups.

group_title

A character string of the title for the group. Ignored if group_by_ref is FALSE.

group_labels

A character vector of the labels for the group. If NULL, the labels are generated automatically. Ignored if group_by_ref is FALSE.

group_colors

A character vector of colors for the plot. If NULL, the default colors are used. If group_by_ref is FALSE, the first color is used as fill color.

breaks

The number of breaks for the histogram.

rcs_color

The color for the restricted cubic spline.

print_p_ph

A logical value. If TRUE, print the p-value of the proportional hazards test (survival::cox.zph()) in the plot.

trans

The transformation for the y axis in the plot. Passed to ggplot2::scale_y_continuous(transform = trans).

save_plot

A logical value indicating whether to save the plot.

filename

A character string specifying the filename for the plot. If NULL, a default filename is used.

y_lim

The range of effect value of the plot. If NULL, the numbers are determined automatically.

hist_max

The maximum value for the histogram. If NULL, the maximum value is determined automatically.

xlim

The x-axis limits for the plot. If NULL, the limits are the 0.025 and 0.975 quantiles. The actual plot range might be slightly larger than this range to fit the histogram.

height

The height of the saved plot.

width

The width of the saved plot.

return_details

A logical value indicating whether to return the details of the plot.

Value

A ggplot object, or a list containing the ggplot object and other details if return_details is TRUE.

Examples

data(cancer, package = "survival")
# coxph model with time assigned
rcs_plot(cancer, x = "age", y = "status", time = "time", covars = "ph.karno", save_plot = FALSE)

# logistic model with time not assigned
cancer$dead <- cancer$status == 2
rcs_plot(cancer, x = "age", y = "dead", covars = "ph.karno", save_plot = FALSE)

[Package clinpubr version 1.0.1 Index]