Generates spline basis matrices for fitting to time-course function
genspline.Rd
Generates spline basis matrices for fitting to time-course function
Usage
genspline(
x,
spline = "bs",
knots = 1,
degree = 1,
max.time = 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 knots. If a single integer is given it indicates the number of knots (they will be equally spaced across the range of time-points). If a numeric vector is given it indicates the quantiles of the knots as a proportion of the maximum study follow-up in the dataset. For example, if the maximum follow-up time in the dataset is 10 months,
knots=c(0.1,0.5)
would indicate knots should be fitted at 1 and 5 months follow-up.- 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.time
A number indicating the maximum time between which to calculate the spline function.
- boundaries
A positive numeric vector of length 2 that represents the time-points 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 ofx
.
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 spline with 2 knots at selected quantiles
genspline(x, spline="ns", knots=c(0.1, 0.5))
#> 1 2 3
#> 0 0.000000000 0.00000000 0.00000000
#> 1 -0.014156667 0.03969467 -0.02551800
#> 2 -0.028112452 0.07916287 -0.05089041
#> 3 -0.041666470 0.11817812 -0.07597165
#> 4 -0.054617838 0.15651395 -0.10061611
#> 5 -0.066765674 0.19394389 -0.12467821
#> 6 -0.077909094 0.23024146 -0.14801237
#> 7 -0.087847215 0.26518020 -0.17047299
#> 8 -0.096379153 0.29853363 -0.19191448
#> 9 -0.103304027 0.33007527 -0.21219125
#> 10 -0.108420951 0.35957866 -0.23115771
#> 11 -0.111576347 0.38686644 -0.24869787
#> 12 -0.112805841 0.41195769 -0.26481407
#> 13 -0.112192366 0.43492063 -0.27953826
#> 14 -0.109818852 0.45582345 -0.29290238
#> 15 -0.105768230 0.47473438 -0.30493837
#> 16 -0.100123432 0.49172161 -0.31567818
#> 17 -0.092967387 0.50685335 -0.32515374
#> 18 -0.084383028 0.52019781 -0.33339701
#> 19 -0.074453285 0.53182320 -0.34043991
#> 20 -0.063261088 0.54179771 -0.34631440
#> 21 -0.050889370 0.55018957 -0.35105242
#> 22 -0.037421061 0.55706697 -0.35468591
#> 23 -0.022939091 0.56249812 -0.35724681
#> 24 -0.007526393 0.56655123 -0.35876706
#> 25 0.008734103 0.56929451 -0.35927861
#> 26 0.025759467 0.57079616 -0.35881340
#> 27 0.043466767 0.57112439 -0.35740338
#> 28 0.061773071 0.57034740 -0.35508047
#> 29 0.080595450 0.56853341 -0.35187663
#> 30 0.099850972 0.56575061 -0.34782381
#> 31 0.119456707 0.56206722 -0.34295393
#> 32 0.139329723 0.55755144 -0.33729894
#> 33 0.159387088 0.55227149 -0.33089080
#> 34 0.179545874 0.54629555 -0.32376143
#> 35 0.199723147 0.53969185 -0.31594278
#> 36 0.219835978 0.53252859 -0.30746679
#> 37 0.239801435 0.52487398 -0.29836542
#> 38 0.259536588 0.51679622 -0.28867059
#> 39 0.278958505 0.50836352 -0.27841425
#> 40 0.297984256 0.49964408 -0.26762834
#> 41 0.316530909 0.49070612 -0.25634481
#> 42 0.334515533 0.48161784 -0.24459560
#> 43 0.351855199 0.47244744 -0.23241264
#> 44 0.368466973 0.46326314 -0.21982789
#> 45 0.384267926 0.45413314 -0.20687329
#> 46 0.399175127 0.44512564 -0.19358077
#> 47 0.413105645 0.43630886 -0.17998228
#> 48 0.425976548 0.42775100 -0.16610977
#> 49 0.437704905 0.41952026 -0.15199517
#> 50 0.448207787 0.41168486 -0.13767043
#> 51 0.457420444 0.40430009 -0.12316276
#> 52 0.465350863 0.39736958 -0.10848045
#> 53 0.472025210 0.39088408 -0.09362707
#> 54 0.477469654 0.38483430 -0.07860618
#> 55 0.481710364 0.37921098 -0.06342134
#> 56 0.484773507 0.37400485 -0.04807613
#> 57 0.486685252 0.36920663 -0.03257410
#> 58 0.487471767 0.36480705 -0.01691882
#> 59 0.487159219 0.36079685 -0.00111385
#> 60 0.485773778 0.35716676 0.01483724
#> 61 0.483341610 0.35390749 0.03093090
#> 62 0.479888885 0.35100979 0.04716355
#> 63 0.475441771 0.34846438 0.06353163
#> 64 0.470026435 0.34626198 0.08003158
#> 65 0.463669046 0.34439334 0.09665984
#> 66 0.456395772 0.34284917 0.11341283
#> 67 0.448232780 0.34162021 0.13028700
#> 68 0.439206240 0.34069719 0.14727879
#> 69 0.429342320 0.34007084 0.16438462
#> 70 0.418667187 0.33973188 0.18160094
#> 71 0.407207009 0.33967104 0.19892417
#> 72 0.394987955 0.33987906 0.21635076
#> 73 0.382036194 0.34034666 0.23387715
#> 74 0.368377892 0.34106457 0.25149976
#> 75 0.354039218 0.34202352 0.26921504
#> 76 0.339046341 0.34321424 0.28701941
#> 77 0.323425429 0.34462747 0.30490933
#> 78 0.307202649 0.34625392 0.32288121
#> 79 0.290404170 0.34808433 0.34093151
#> 80 0.273056159 0.35010942 0.35905664
#> 81 0.255184786 0.35231993 0.37725306
#> 82 0.236816218 0.35470659 0.39551719
#> 83 0.217976624 0.35726012 0.41384548
#> 84 0.198692171 0.35997126 0.43223435
#> 85 0.178989027 0.36283072 0.45068025
#> 86 0.158893362 0.36582925 0.46917961
#> 87 0.138431342 0.36895757 0.48772886
#> 88 0.117629137 0.37220642 0.50632445
#> 89 0.096512914 0.37556651 0.52496280
#> 90 0.075108841 0.37902858 0.54364036
#> 91 0.053443087 0.38258336 0.56235356
#> 92 0.031541819 0.38622157 0.58109883
#> 93 0.009431207 0.38993395 0.59987262
#> 94 -0.012862583 0.39371123 0.61867135
#> 95 -0.035313382 0.39754414 0.63749147
#> 96 -0.057895021 0.40142339 0.65632941
#> 97 -0.080581333 0.40533973 0.67518160
#> 98 -0.103346149 0.40928388 0.69404449
#> 99 -0.126163302 0.41324658 0.71291450
#> 100 -0.149006623 0.41721854 0.73178808
# 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