Skip to contents

smooth_hr() shows how the effect of a continuous covariate on the hazard varies over its range, which a single linear coefficient can't. It refits the cox_ph() model with the covariate's linear term replaced by a cubic B-spline, keeping the other covariates, the strata, the weights, the ties method, and the variance type. The curve is the log hazard ratio of each value of the covariate against reference, with pointwise confidence limits at the fit's conf_level.

Usage

smooth_hr(fit, term, df = 4, n_grid = 200, reference = NULL)

# S3 method for class 'smooth_hr'
as.data.frame(
  x,
  row.names = NULL,
  optional = FALSE,
  scale = c("log_hr", "hr"),
  ...
)

Arguments

fit

A cox_ph() fit without frailty.

term

The name of the covariate's coefficient, which must enter the model linearly.

df

The degrees of freedom of the spline, at least 3. The default of 4 gives one interior knot, at the median.

n_grid

The number of evenly spaced values at which the curve is evaluated, over the covariate's range.

reference

The value of the covariate at which the log hazard ratio is zero. The default is its (weighted) mean.

x

A smooth_hr object.

row.names, optional

Unused, for compatibility with the generic.

scale

The scale of the table: "log_hr" (the default) or "hr".

...

Unused.

Value

A smooth_hr object holding term, reference, grid (the covariate values), log_hr with its limits log_hr_lower and log_hr_upper, the same on the hazard ratio scale (hr, hr_lower, and hr_upper), df, the interior knots, conf_level, and adjustment (the mean of each other covariate). as.data.frame() gives the curve as a table, with scale = "log_hr" (the default) or "hr".

Details

The spline has df - 3 interior knots, at evenly spaced quantiles of the covariate, and boundary knots at its range.

See also

plot_smooth_hr() to plot the curve.

Examples

fit <- cox_ph(Surv(time, status) ~ age + sex, data = lung)
curve <- smooth_hr(fit, term = "age")
curve
#> Smooth hazard ratio for age: a cubic spline with 4 df, against age = 62.4474
#> Interior knots: 63
#> Other covariates held at their means: sex = 1.395
#> 
#>    age    log_hr conf_low conf_high
#>  39.00 -1.250662 -3.09238   0.59105
#>  47.64 -0.027541 -0.59130   0.53622
#>  56.29  0.092187 -0.18447   0.36884
#>  64.71  0.003307 -0.07763   0.08425
#>  73.36  0.314295 -0.12483   0.75342
#>  82.00  1.064115 -0.32812   2.45635
#> (6 of 200 grid points shown)
head(as.data.frame(curve, scale = "hr"))
#>        age        hr   conf_low conf_high
#> 1 39.00000 0.2863152 0.04539384  1.805892
#> 2 39.21608 0.3013670 0.05145765  1.764987
#> 3 39.43216 0.3168178 0.05811921  1.727029
#> 4 39.64824 0.3326537 0.06540581  1.691876
#> 5 39.86432 0.3488591 0.07334142  1.659399
#> 6 40.08040 0.3654169 0.08194603  1.629481