plotHazardRatio {casebase}  R Documentation 
Plot Hazards and Hazard Ratios
Description
Plot method for objects returned by the fitSmoothHazard
function. Current plot types are hazard function and hazard ratio. The
visreg
package must be installed for type="hazard"
. This
function accounts for the possible timevarying exposure effects.
Usage
plotHazardRatio(x, newdata, newdata2, ci, ci.lvl, ci.col, rug, xvar, ...)
## S3 method for class 'singleEventCB'
plot(
x,
...,
type = c("hazard", "hr"),
hazard.params = list(),
newdata,
exposed,
increment = 1,
var,
xvar = NULL,
ci = FALSE,
ci.lvl = 0.95,
rug = !ci,
ci.col = "grey"
)
incrVar(var, increment = 1)
Arguments
x 
Fitted object of class 
newdata 
Required for 
newdata2 

ci 
Logical; if TRUE confidence bands are calculated. Only available
for 
ci.lvl 
Confidence level. Must be in (0,1), Default: 0.95. Only used
for 
ci.col 
Confidence band color. Only used if argument 
rug 
Logical. Adds a rug representation (1d plot) of the event times
(only for 
xvar 
Variable to be used on xaxis for hazard ratio plots. If NULL,
the function defaults to using the time variable used in the call to

... 
further arguments passed to 
type 
plot type. Choose one of either 
hazard.params 
Named list of arguments which will override the defaults
passed to 
exposed 
function that takes 
increment 
Numeric value indicating how much to increment (if positive)
or decrement (if negative) the 
var 
specify the variable name for the exposed/unexposed (name is given
as a character variable). If this argument is missing, then the

Details
This function has only been thoroughly tested for family="glm"
. If
the user wants more customized plot aesthetics, we recommend saving the
results to a data.frame
and using the graphical package of their choice.
Value
a plot of the hazard function or hazard ratio. For type="hazard"
, a
data.frame
(returned invisibly) of the original data used in the fitting
along with the data used to create the plots including predictedhazard
which is the predicted hazard for a given covariate pattern and time.
predictedloghazard
is the predicted hazard on the log scale. lowerbound
and upperbound
are the lower and upper confidence interval bounds on the
hazard scale (i.e. used to plot the confidence bands). standarderror
is
the standard error of the log hazard or log hazard ratio (only if
family="glm"
or family="gam"
). For type="hr"
, log_hazard_ratio
and
hazard_ratio
is returned, and if ci=TRUE
, standarderror
(on the log
scale) and lowerbound
and upperbound
of the hazard_ratio
are
returned.
References
Mark Clements and XingRong Liu (2019). rstpm2: Smooth Survival Models, Including Generalized Survival Models. R package version 1.5.1. https://CRAN.Rproject.org/package=rstpm2
Breheny P and Burchett W (2017). Visualization of Regression Models Using visreg. The R Journal, 9: 5671.
See Also
modifyList
, fitSmoothHazard()
,
graphics::par()
, visreg::visreg()
Examples
if (requireNamespace("splines", quietly = TRUE)) {
data("simdat") # from casebase package
library(splines)
simdat < transform(simdat[sample(1:nrow(simdat), size = 200),],
treat = factor(trt, levels = 0:1,
labels = c("control","treatment")))
fit_numeric_exposure < fitSmoothHazard(status ~ trt*bs(eventtime),
data = simdat,
ratio = 1,
time = "eventtime")
fit_factor_exposure < fitSmoothHazard(status ~ treat*bs(eventtime),
data = simdat,
ratio = 1,
time = "eventtime")
newtime < quantile(fit_factor_exposure[["data"]][[fit_factor_exposure[["timeVar"]]]],
probs = seq(0.05, 0.95, 0.01))
par(mfrow = c(1,3))
plot(fit_numeric_exposure,
type = "hr",
newdata = data.frame(trt = 0, eventtime = newtime),
exposed = function(data) transform(data, trt = 1),
xvar = "eventtime",
ci = TRUE)
#by default this will increment `var` by 1 for exposed category
plot(fit_factor_exposure,
type = "hr",
newdata = data.frame(treat = factor("control",
levels = c("control","treatment")), eventtime = newtime),
var = "treat",
increment = 1,
xvar = "eventtime",
ci = TRUE,
ci.col = "lightblue",
xlab = "Time",
main = "Hazard Ratio for Treatment",
ylab = "Hazard Ratio",
lty = 5,
lwd = 7,
rug = TRUE)
# we can also decrement `var` by 1 to give hazard ratio for control/treatment
result < plot(fit_factor_exposure,
type = "hr",
newdata = data.frame(treat = factor("treatment",
levels = c("control","treatment")),
eventtime = newtime),
var = "treat",
increment = 1,
xvar = "eventtime",
ci = TRUE)
# see data used to create plot
head(result)
}