Generates spline basis matrices for fitting to dose-response function

genspline(
  x,
  spline = "bs",
  knots = 1,
  degree = 1,
  max.dose = max(x),
  boundaries = NULL
)

Arguments

x

A numeric vector indicating all time points available in the dataset

spline

Indicates the type of spline function. Can be either a piecewise linear spline ("ls"), natural cubic spline ("ns"), or B-spline ("bs").

knots

The number/location of internal knots. If a single integer is given it indicates the number of knots (they will be equally spaced across the range of doses for each agent). If a numeric vector is given it indicates the quantiles of the knots as a proportion of the maximum dose in the dataset. For example, if the maximum dose in the dataset is 100mg/d, knots=c(0.1,0.5) would indicate knots should be fitted at 10mg/d and 50mg/d.

degree

a positive integer giving the degree of the polynomial from which the spline function is composed (e.g. degree=3 represents a cubic spline).

max.dose

A number indicating the maximum dose between which to calculate the spline function.

boundaries

A positive numeric vector of length 2 that represents the doses at which to anchor the B-spline or natural cubic spline basis matrix. This allows data to extend beyond the boundary knots, or for the basis parameters to not depend on x. The default (boundaries=NULL) is the range of x.

Value

A spline basis matrix with number of rows equal to length(x) and the number of columns equal to the number of coefficients in the spline.

Examples

x <- 0:100

genspline(x)
#>        1    2
#> 0   0.00 0.00
#> 1   0.02 0.00
#> 2   0.04 0.00
#> 3   0.06 0.00
#> 4   0.08 0.00
#> 5   0.10 0.00
#> 6   0.12 0.00
#> 7   0.14 0.00
#> 8   0.16 0.00
#> 9   0.18 0.00
#> 10  0.20 0.00
#> 11  0.22 0.00
#> 12  0.24 0.00
#> 13  0.26 0.00
#> 14  0.28 0.00
#> 15  0.30 0.00
#> 16  0.32 0.00
#> 17  0.34 0.00
#> 18  0.36 0.00
#> 19  0.38 0.00
#> 20  0.40 0.00
#> 21  0.42 0.00
#> 22  0.44 0.00
#> 23  0.46 0.00
#> 24  0.48 0.00
#> 25  0.50 0.00
#> 26  0.52 0.00
#> 27  0.54 0.00
#> 28  0.56 0.00
#> 29  0.58 0.00
#> 30  0.60 0.00
#> 31  0.62 0.00
#> 32  0.64 0.00
#> 33  0.66 0.00
#> 34  0.68 0.00
#> 35  0.70 0.00
#> 36  0.72 0.00
#> 37  0.74 0.00
#> 38  0.76 0.00
#> 39  0.78 0.00
#> 40  0.80 0.00
#> 41  0.82 0.00
#> 42  0.84 0.00
#> 43  0.86 0.00
#> 44  0.88 0.00
#> 45  0.90 0.00
#> 46  0.92 0.00
#> 47  0.94 0.00
#> 48  0.96 0.00
#> 49  0.98 0.00
#> 50  1.00 0.00
#> 51  0.98 0.02
#> 52  0.96 0.04
#> 53  0.94 0.06
#> 54  0.92 0.08
#> 55  0.90 0.10
#> 56  0.88 0.12
#> 57  0.86 0.14
#> 58  0.84 0.16
#> 59  0.82 0.18
#> 60  0.80 0.20
#> 61  0.78 0.22
#> 62  0.76 0.24
#> 63  0.74 0.26
#> 64  0.72 0.28
#> 65  0.70 0.30
#> 66  0.68 0.32
#> 67  0.66 0.34
#> 68  0.64 0.36
#> 69  0.62 0.38
#> 70  0.60 0.40
#> 71  0.58 0.42
#> 72  0.56 0.44
#> 73  0.54 0.46
#> 74  0.52 0.48
#> 75  0.50 0.50
#> 76  0.48 0.52
#> 77  0.46 0.54
#> 78  0.44 0.56
#> 79  0.42 0.58
#> 80  0.40 0.60
#> 81  0.38 0.62
#> 82  0.36 0.64
#> 83  0.34 0.66
#> 84  0.32 0.68
#> 85  0.30 0.70
#> 86  0.28 0.72
#> 87  0.26 0.74
#> 88  0.24 0.76
#> 89  0.22 0.78
#> 90  0.20 0.80
#> 91  0.18 0.82
#> 92  0.16 0.84
#> 93  0.14 0.86
#> 94  0.12 0.88
#> 95  0.10 0.90
#> 96  0.08 0.92
#> 97  0.06 0.94
#> 98  0.04 0.96
#> 99  0.02 0.98
#> 100 0.00 1.00

# Generate a quadratic B-spline with 1 equally spaced internal knot
genspline(x, spline="bs", knots=2, degree=2)
#>           1       2       3      4
#> 0   0.00000 0.00000 0.00000 0.0000
#> 1   0.05865 0.00045 0.00000 0.0000
#> 2   0.11460 0.00180 0.00000 0.0000
#> 3   0.16785 0.00405 0.00000 0.0000
#> 4   0.21840 0.00720 0.00000 0.0000
#> 5   0.26625 0.01125 0.00000 0.0000
#> 6   0.31140 0.01620 0.00000 0.0000
#> 7   0.35385 0.02205 0.00000 0.0000
#> 8   0.39360 0.02880 0.00000 0.0000
#> 9   0.43065 0.03645 0.00000 0.0000
#> 10  0.46500 0.04500 0.00000 0.0000
#> 11  0.49665 0.05445 0.00000 0.0000
#> 12  0.52560 0.06480 0.00000 0.0000
#> 13  0.55185 0.07605 0.00000 0.0000
#> 14  0.57540 0.08820 0.00000 0.0000
#> 15  0.59625 0.10125 0.00000 0.0000
#> 16  0.61440 0.11520 0.00000 0.0000
#> 17  0.62985 0.13005 0.00000 0.0000
#> 18  0.64260 0.14580 0.00000 0.0000
#> 19  0.65265 0.16245 0.00000 0.0000
#> 20  0.66000 0.18000 0.00000 0.0000
#> 21  0.66465 0.19845 0.00000 0.0000
#> 22  0.66660 0.21780 0.00000 0.0000
#> 23  0.66585 0.23805 0.00000 0.0000
#> 24  0.66240 0.25920 0.00000 0.0000
#> 25  0.65625 0.28125 0.00000 0.0000
#> 26  0.64740 0.30420 0.00000 0.0000
#> 27  0.63585 0.32805 0.00000 0.0000
#> 28  0.62160 0.35280 0.00000 0.0000
#> 29  0.60465 0.37845 0.00000 0.0000
#> 30  0.58500 0.40500 0.00000 0.0000
#> 31  0.56265 0.43245 0.00000 0.0000
#> 32  0.53760 0.46080 0.00000 0.0000
#> 33  0.50985 0.49005 0.00000 0.0000
#> 34  0.48020 0.51960 0.00020 0.0000
#> 35  0.45125 0.54750 0.00125 0.0000
#> 36  0.42320 0.57360 0.00320 0.0000
#> 37  0.39605 0.59790 0.00605 0.0000
#> 38  0.36980 0.62040 0.00980 0.0000
#> 39  0.34445 0.64110 0.01445 0.0000
#> 40  0.32000 0.66000 0.02000 0.0000
#> 41  0.29645 0.67710 0.02645 0.0000
#> 42  0.27380 0.69240 0.03380 0.0000
#> 43  0.25205 0.70590 0.04205 0.0000
#> 44  0.23120 0.71760 0.05120 0.0000
#> 45  0.21125 0.72750 0.06125 0.0000
#> 46  0.19220 0.73560 0.07220 0.0000
#> 47  0.17405 0.74190 0.08405 0.0000
#> 48  0.15680 0.74640 0.09680 0.0000
#> 49  0.14045 0.74910 0.11045 0.0000
#> 50  0.12500 0.75000 0.12500 0.0000
#> 51  0.11045 0.74910 0.14045 0.0000
#> 52  0.09680 0.74640 0.15680 0.0000
#> 53  0.08405 0.74190 0.17405 0.0000
#> 54  0.07220 0.73560 0.19220 0.0000
#> 55  0.06125 0.72750 0.21125 0.0000
#> 56  0.05120 0.71760 0.23120 0.0000
#> 57  0.04205 0.70590 0.25205 0.0000
#> 58  0.03380 0.69240 0.27380 0.0000
#> 59  0.02645 0.67710 0.29645 0.0000
#> 60  0.02000 0.66000 0.32000 0.0000
#> 61  0.01445 0.64110 0.34445 0.0000
#> 62  0.00980 0.62040 0.36980 0.0000
#> 63  0.00605 0.59790 0.39605 0.0000
#> 64  0.00320 0.57360 0.42320 0.0000
#> 65  0.00125 0.54750 0.45125 0.0000
#> 66  0.00020 0.51960 0.48020 0.0000
#> 67  0.00000 0.49005 0.50985 0.0001
#> 68  0.00000 0.46080 0.53760 0.0016
#> 69  0.00000 0.43245 0.56265 0.0049
#> 70  0.00000 0.40500 0.58500 0.0100
#> 71  0.00000 0.37845 0.60465 0.0169
#> 72  0.00000 0.35280 0.62160 0.0256
#> 73  0.00000 0.32805 0.63585 0.0361
#> 74  0.00000 0.30420 0.64740 0.0484
#> 75  0.00000 0.28125 0.65625 0.0625
#> 76  0.00000 0.25920 0.66240 0.0784
#> 77  0.00000 0.23805 0.66585 0.0961
#> 78  0.00000 0.21780 0.66660 0.1156
#> 79  0.00000 0.19845 0.66465 0.1369
#> 80  0.00000 0.18000 0.66000 0.1600
#> 81  0.00000 0.16245 0.65265 0.1849
#> 82  0.00000 0.14580 0.64260 0.2116
#> 83  0.00000 0.13005 0.62985 0.2401
#> 84  0.00000 0.11520 0.61440 0.2704
#> 85  0.00000 0.10125 0.59625 0.3025
#> 86  0.00000 0.08820 0.57540 0.3364
#> 87  0.00000 0.07605 0.55185 0.3721
#> 88  0.00000 0.06480 0.52560 0.4096
#> 89  0.00000 0.05445 0.49665 0.4489
#> 90  0.00000 0.04500 0.46500 0.4900
#> 91  0.00000 0.03645 0.43065 0.5329
#> 92  0.00000 0.02880 0.39360 0.5776
#> 93  0.00000 0.02205 0.35385 0.6241
#> 94  0.00000 0.01620 0.31140 0.6724
#> 95  0.00000 0.01125 0.26625 0.7225
#> 96  0.00000 0.00720 0.21840 0.7744
#> 97  0.00000 0.00405 0.16785 0.8281
#> 98  0.00000 0.00180 0.11460 0.8836
#> 99  0.00000 0.00045 0.05865 0.9409
#> 100 0.00000 0.00000 0.00000 1.0000

# Generate a natural cubic spline with 3 knots at selected quantiles
genspline(x, spline="ns", knots=c(0.1, 0.5, 0.7))
#>                1            2          3            4
#> 0   0.000000e+00  0.000000000 0.00000000  0.000000000
#> 1   2.857143e-05 -0.014927620 0.03980699 -0.024879367
#> 2   2.285714e-04 -0.029744212 0.07931790 -0.049573687
#> 3   7.714286e-04 -0.044338747 0.11823666 -0.073897912
#> 4   1.828571e-03 -0.058600197 0.15626719 -0.097666996
#> 5   3.571429e-03 -0.072417534 0.19311342 -0.120695890
#> 6   6.171429e-03 -0.085679729 0.22847928 -0.142799548
#> 7   9.800000e-03 -0.098275754 0.26206868 -0.163792923
#> 8   1.462857e-02 -0.110094580 0.29358555 -0.183490967
#> 9   2.082857e-02 -0.121025179 0.32273381 -0.201708632
#> 10  2.857143e-02 -0.130956524 0.34921740 -0.218260873
#> 11  3.798228e-02 -0.139798290 0.37280779 -0.233004866
#> 12  4.900106e-02 -0.147542979 0.39354671 -0.245966693
#> 13  6.152143e-02 -0.154203797 0.41154346 -0.257214661
#> 14  7.543704e-02 -0.159793949 0.42690732 -0.266817076
#> 15  9.064153e-02 -0.164326642 0.43974759 -0.274842244
#> 16  1.070286e-01 -0.167815083 0.45017355 -0.281358472
#> 17  1.244918e-01 -0.170272477 0.45829451 -0.286434066
#> 18  1.429249e-01 -0.171712030 0.46421973 -0.290137334
#> 19  1.622214e-01 -0.172146949 0.46805853 -0.292536581
#> 20  1.822751e-01 -0.171590439 0.46992018 -0.293700114
#> 21  2.029796e-01 -0.170055707 0.46991398 -0.293696240
#> 22  2.242286e-01 -0.167555959 0.46814922 -0.292593265
#> 23  2.459156e-01 -0.164104401 0.46473519 -0.290459496
#> 24  2.679344e-01 -0.159714240 0.45978118 -0.287363239
#> 25  2.901786e-01 -0.154398680 0.45339648 -0.283372800
#> 26  3.125418e-01 -0.148170929 0.44569038 -0.278556487
#> 27  3.349177e-01 -0.141044193 0.43677217 -0.272982605
#> 28  3.572000e-01 -0.133031677 0.42675114 -0.266719462
#> 29  3.792823e-01 -0.124146589 0.41573658 -0.259835364
#> 30  4.010582e-01 -0.114402133 0.40383779 -0.252398616
#> 31  4.224214e-01 -0.103811516 0.39116404 -0.244477527
#> 32  4.432656e-01 -0.092387945 0.37782464 -0.236140402
#> 33  4.634844e-01 -0.080144625 0.36392888 -0.227455548
#> 34  4.829714e-01 -0.067094763 0.34958603 -0.218491271
#> 35  5.016204e-01 -0.053251564 0.33490541 -0.209315878
#> 36  5.193249e-01 -0.038628235 0.31999628 -0.199997676
#> 37  5.359786e-01 -0.023237982 0.30496795 -0.190604970
#> 38  5.514751e-01 -0.007094011 0.28992971 -0.181206068
#> 39  5.657082e-01  0.009790471 0.27499084 -0.171869276
#> 40  5.785714e-01  0.027402259 0.26026064 -0.162662901
#> 41  5.899585e-01  0.045728147 0.24584840 -0.153655248
#> 42  5.997630e-01  0.064754928 0.23186340 -0.144914626
#> 43  6.078786e-01  0.084469397 0.21841494 -0.136509339
#> 44  6.141989e-01  0.104858346 0.20561231 -0.128507695
#> 45  6.186177e-01  0.125908571 0.19356480 -0.120978000
#> 46  6.210286e-01  0.147606864 0.18238170 -0.113988560
#> 47  6.213251e-01  0.169940020 0.17217229 -0.107607683
#> 48  6.194011e-01  0.192894832 0.16304588 -0.101903675
#> 49  6.151500e-01  0.216458095 0.15511175 -0.096944841
#> 50  6.084656e-01  0.240616602 0.14847918 -0.092799490
#> 51  5.992915e-01  0.265331143 0.14322683 -0.089504269
#> 52  5.877714e-01  0.290458483 0.13931071 -0.086969195
#> 53  5.740989e-01  0.315829385 0.13665621 -0.085072629
#> 54  5.584677e-01  0.341274612 0.13518869 -0.083692931
#> 55  5.410714e-01  0.366624925 0.13483353 -0.082708458
#> 56  5.221037e-01  0.391711086 0.13551612 -0.081997572
#> 57  5.017582e-01  0.416363858 0.13716181 -0.081438631
#> 58  4.802286e-01  0.440414002 0.13969599 -0.080909996
#> 59  4.577085e-01  0.463692281 0.14304404 -0.080290025
#> 60  4.343915e-01  0.486029456 0.14713133 -0.079457079
#> 61  4.104714e-01  0.507256290 0.15188323 -0.078289517
#> 62  3.861418e-01  0.527203544 0.15722512 -0.076665698
#> 63  3.615963e-01  0.545701981 0.16308237 -0.074463982
#> 64  3.370286e-01  0.562582363 0.16938037 -0.071562729
#> 65  3.126323e-01  0.577675451 0.17604448 -0.067840298
#> 66  2.886011e-01  0.590812008 0.18300008 -0.063175049
#> 67  2.651286e-01  0.601822795 0.19017255 -0.057445341
#> 68  2.424085e-01  0.610538576 0.19748725 -0.050529534
#> 69  2.206344e-01  0.616790111 0.20486958 -0.042305988
#> 70  2.000000e-01  0.620408163 0.21224490 -0.032653061
#> 71  1.806593e-01  0.621267271 0.21955147 -0.021478005
#> 72  1.626074e-01  0.619417082 0.22677914 -0.008803628
#> 73  1.458000e-01  0.614951020 0.23393061  0.005318367
#> 74  1.301926e-01  0.607962509 0.24100862  0.020836281
#> 75  1.157407e-01  0.598544974 0.24801587  0.037698413
#> 76  1.024000e-01  0.586791837 0.25495510  0.055853061
#> 77  9.012593e-02  0.572796523 0.26182902  0.075248526
#> 78  7.887407e-02  0.556652457 0.26864036  0.095833107
#> 79  6.860000e-02  0.538453061 0.27539184  0.117555102
#> 80  5.925926e-02  0.518291761 0.28208617  0.140362812
#> 81  5.080741e-02  0.496261980 0.28872608  0.164204535
#> 82  4.320000e-02  0.472457143 0.29531429  0.189028571
#> 83  3.639259e-02  0.446970673 0.30185351  0.214783220
#> 84  3.034074e-02  0.419895994 0.30834649  0.241416780
#> 85  2.500000e-02  0.391326531 0.31479592  0.268877551
#> 86  2.032593e-02  0.361355707 0.32120454  0.297113832
#> 87  1.627407e-02  0.330076946 0.32757506  0.326073923
#> 88  1.280000e-02  0.297583673 0.33391020  0.355706122
#> 89  9.859259e-03  0.263969312 0.34021270  0.385958730
#> 90  7.407407e-03  0.229327286 0.34648526  0.416780045
#> 91  5.400000e-03  0.193751020 0.35273061  0.448118367
#> 92  3.792593e-03  0.157333938 0.35895147  0.479921995
#> 93  2.540741e-03  0.120169463 0.36515057  0.512139229
#> 94  1.600000e-03  0.082351020 0.37133061  0.544718367
#> 95  9.259259e-04  0.043972033 0.37749433  0.577607710
#> 96  4.740741e-04  0.005125926 0.38364444  0.610755556
#> 97  2.000000e-04 -0.034093878 0.38978367  0.644110204
#> 98  5.925926e-05 -0.073593953 0.39591474  0.677619955
#> 99  7.407407e-06 -0.113280877 0.40204036  0.711233107
#> 100 0.000000e+00 -0.153061224 0.40816327  0.744897959

# Generate a piecewise linear spline with 3 equally spaced knots
genspline(x, spline="ls", knots=3)
#>      1  2  3  4
#> 0    0  0  0  0
#> 1    1  0  0  0
#> 2    2  0  0  0
#> 3    3  0  0  0
#> 4    4  0  0  0
#> 5    5  0  0  0
#> 6    6  0  0  0
#> 7    7  0  0  0
#> 8    8  0  0  0
#> 9    9  0  0  0
#> 10  10  0  0  0
#> 11  11  0  0  0
#> 12  12  0  0  0
#> 13  13  0  0  0
#> 14  14  0  0  0
#> 15  15  0  0  0
#> 16  16  0  0  0
#> 17  17  0  0  0
#> 18  18  0  0  0
#> 19  19  0  0  0
#> 20  20  0  0  0
#> 21  21  0  0  0
#> 22  22  0  0  0
#> 23  23  0  0  0
#> 24  24  0  0  0
#> 25  25  0  0  0
#> 26  25  1  0  0
#> 27  25  2  0  0
#> 28  25  3  0  0
#> 29  25  4  0  0
#> 30  25  5  0  0
#> 31  25  6  0  0
#> 32  25  7  0  0
#> 33  25  8  0  0
#> 34  25  9  0  0
#> 35  25 10  0  0
#> 36  25 11  0  0
#> 37  25 12  0  0
#> 38  25 13  0  0
#> 39  25 14  0  0
#> 40  25 15  0  0
#> 41  25 16  0  0
#> 42  25 17  0  0
#> 43  25 18  0  0
#> 44  25 19  0  0
#> 45  25 20  0  0
#> 46  25 21  0  0
#> 47  25 22  0  0
#> 48  25 23  0  0
#> 49  25 24  0  0
#> 50  25 25  0  0
#> 51  25 25  1  0
#> 52  25 25  2  0
#> 53  25 25  3  0
#> 54  25 25  4  0
#> 55  25 25  5  0
#> 56  25 25  6  0
#> 57  25 25  7  0
#> 58  25 25  8  0
#> 59  25 25  9  0
#> 60  25 25 10  0
#> 61  25 25 11  0
#> 62  25 25 12  0
#> 63  25 25 13  0
#> 64  25 25 14  0
#> 65  25 25 15  0
#> 66  25 25 16  0
#> 67  25 25 17  0
#> 68  25 25 18  0
#> 69  25 25 19  0
#> 70  25 25 20  0
#> 71  25 25 21  0
#> 72  25 25 22  0
#> 73  25 25 23  0
#> 74  25 25 24  0
#> 75  25 25 25  0
#> 76  25 25 25  1
#> 77  25 25 25  2
#> 78  25 25 25  3
#> 79  25 25 25  4
#> 80  25 25 25  5
#> 81  25 25 25  6
#> 82  25 25 25  7
#> 83  25 25 25  8
#> 84  25 25 25  9
#> 85  25 25 25 10
#> 86  25 25 25 11
#> 87  25 25 25 12
#> 88  25 25 25 13
#> 89  25 25 25 14
#> 90  25 25 25 15
#> 91  25 25 25 16
#> 92  25 25 25 17
#> 93  25 25 25 18
#> 94  25 25 25 19
#> 95  25 25 25 20
#> 96  25 25 25 21
#> 97  25 25 25 22
#> 98  25 25 25 23
#> 99  25 25 25 24
#> 100 25 25 25 25