Adds spline terms calculated via Hmisc::rcspline.eval() to a data frame.

add_splines(data, variable, knots = NULL, nk = 5, norm = 2, new_names = NULL)

Arguments

data

a data frame

variable

name of column in data

knots

knot locations. If not given, knots will be estimated using default quantiles of x. For 3 knots, the outer quantiles used are 0.10 and 0.90. For 4-6 knots, the outer quantiles used are 0.05 and 0.95. For \(\code{nk}>6\), the outer quantiles are 0.025 and 0.975. The knots are equally spaced between these on the quantile scale. For fewer than 100 non-missing values of x, the outer knots are the 5th smallest and largest x.

nk

number of knots. Default is 5. The minimum value is 3.

norm

0 to use the terms as originally given by Devlin and Weeks (1986), 1 to normalize non-linear terms by the cube of the spacing between the last two knots, 2 to normalize by the square of the spacing between the first and last knots (the default). norm=2 has the advantage of making all nonlinear terms beon the x-scale.

new_names

Optionally specify names of new spline columns

Value

data frame

Knot Locations

Knot locations are returned in attr(data[[variable]], "knots")

Examples

trial %>%
  add_splines(age)
#> # A tibble: 200 × 11
#>    trt      age spage1   spage2  spage3 marker stage grade respo…¹ death ttdeath
#>    <chr>  <dbl>  <dbl>    <dbl>   <dbl>  <dbl> <fct> <fct>   <int> <int>   <dbl>
#>  1 Drug A    23  0      0        0       0.16  T1    II          0     0    24  
#>  2 Drug B     9  0      0        0       1.11  T2    I           1     0    24  
#>  3 Drug A    31  0.178  0        0       0.277 T1    II          0     0    24  
#>  4 Drug A    NA NA     NA       NA       2.07  T3    III         1     1    17.6
#>  5 Drug A    51  9.59   1.05e+0  0.0305  2.77  T4    III         1     1    16.4
#>  6 Drug B    39  1.67   4.77e-4  0       0.613 T4    I           0     1    15.6
#>  7 Drug A    37  1.10   0        0       0.354 T1    II          0     0    24  
#>  8 Drug A    32  0.263  0        0       1.74  T1    I           0     1    18.4
#>  9 Drug A    31  0.178  0        0       0.144 T1    II          0     0    24  
#> 10 Drug B    34  0.506  0        0       0.205 T3    I           0     1    10.5
#> # … with 190 more rows, and abbreviated variable name ¹​response