Skip to contents

Generates spline basis matrices for fitting to time-course function

Usage

genspline(
  x,
  spline = "bs",
  knots = NULL,
  nknots = 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

A numeric vector indicating the location of spline internal knots (specified on the same scale as time in the dataset). Specifying knots overrides nknots.

nknots

The number of spline internal knots. If knots is not specified then these will by default be evenly spaced between 0 and the maximum follow-up time in the dataset (max.time).

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 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", nknots=1, degree=2)
#>          1      2      3
#> 0   0.0000 0.0000 0.0000
#> 1   0.0394 0.0002 0.0000
#> 2   0.0776 0.0008 0.0000
#> 3   0.1146 0.0018 0.0000
#> 4   0.1504 0.0032 0.0000
#> 5   0.1850 0.0050 0.0000
#> 6   0.2184 0.0072 0.0000
#> 7   0.2506 0.0098 0.0000
#> 8   0.2816 0.0128 0.0000
#> 9   0.3114 0.0162 0.0000
#> 10  0.3400 0.0200 0.0000
#> 11  0.3674 0.0242 0.0000
#> 12  0.3936 0.0288 0.0000
#> 13  0.4186 0.0338 0.0000
#> 14  0.4424 0.0392 0.0000
#> 15  0.4650 0.0450 0.0000
#> 16  0.4864 0.0512 0.0000
#> 17  0.5066 0.0578 0.0000
#> 18  0.5256 0.0648 0.0000
#> 19  0.5434 0.0722 0.0000
#> 20  0.5600 0.0800 0.0000
#> 21  0.5754 0.0882 0.0000
#> 22  0.5896 0.0968 0.0000
#> 23  0.6026 0.1058 0.0000
#> 24  0.6144 0.1152 0.0000
#> 25  0.6250 0.1250 0.0000
#> 26  0.6344 0.1352 0.0000
#> 27  0.6426 0.1458 0.0000
#> 28  0.6496 0.1568 0.0000
#> 29  0.6554 0.1682 0.0000
#> 30  0.6600 0.1800 0.0000
#> 31  0.6634 0.1922 0.0000
#> 32  0.6656 0.2048 0.0000
#> 33  0.6666 0.2178 0.0000
#> 34  0.6664 0.2312 0.0000
#> 35  0.6650 0.2450 0.0000
#> 36  0.6624 0.2592 0.0000
#> 37  0.6586 0.2738 0.0000
#> 38  0.6536 0.2888 0.0000
#> 39  0.6474 0.3042 0.0000
#> 40  0.6400 0.3200 0.0000
#> 41  0.6314 0.3362 0.0000
#> 42  0.6216 0.3528 0.0000
#> 43  0.6106 0.3698 0.0000
#> 44  0.5984 0.3872 0.0000
#> 45  0.5850 0.4050 0.0000
#> 46  0.5704 0.4232 0.0000
#> 47  0.5546 0.4418 0.0000
#> 48  0.5376 0.4608 0.0000
#> 49  0.5194 0.4802 0.0000
#> 50  0.5000 0.5000 0.0000
#> 51  0.4802 0.5194 0.0004
#> 52  0.4608 0.5376 0.0016
#> 53  0.4418 0.5546 0.0036
#> 54  0.4232 0.5704 0.0064
#> 55  0.4050 0.5850 0.0100
#> 56  0.3872 0.5984 0.0144
#> 57  0.3698 0.6106 0.0196
#> 58  0.3528 0.6216 0.0256
#> 59  0.3362 0.6314 0.0324
#> 60  0.3200 0.6400 0.0400
#> 61  0.3042 0.6474 0.0484
#> 62  0.2888 0.6536 0.0576
#> 63  0.2738 0.6586 0.0676
#> 64  0.2592 0.6624 0.0784
#> 65  0.2450 0.6650 0.0900
#> 66  0.2312 0.6664 0.1024
#> 67  0.2178 0.6666 0.1156
#> 68  0.2048 0.6656 0.1296
#> 69  0.1922 0.6634 0.1444
#> 70  0.1800 0.6600 0.1600
#> 71  0.1682 0.6554 0.1764
#> 72  0.1568 0.6496 0.1936
#> 73  0.1458 0.6426 0.2116
#> 74  0.1352 0.6344 0.2304
#> 75  0.1250 0.6250 0.2500
#> 76  0.1152 0.6144 0.2704
#> 77  0.1058 0.6026 0.2916
#> 78  0.0968 0.5896 0.3136
#> 79  0.0882 0.5754 0.3364
#> 80  0.0800 0.5600 0.3600
#> 81  0.0722 0.5434 0.3844
#> 82  0.0648 0.5256 0.4096
#> 83  0.0578 0.5066 0.4356
#> 84  0.0512 0.4864 0.4624
#> 85  0.0450 0.4650 0.4900
#> 86  0.0392 0.4424 0.5184
#> 87  0.0338 0.4186 0.5476
#> 88  0.0288 0.3936 0.5776
#> 89  0.0242 0.3674 0.6084
#> 90  0.0200 0.3400 0.6400
#> 91  0.0162 0.3114 0.6724
#> 92  0.0128 0.2816 0.7056
#> 93  0.0098 0.2506 0.7396
#> 94  0.0072 0.2184 0.7744
#> 95  0.0050 0.1850 0.8100
#> 96  0.0032 0.1504 0.8464
#> 97  0.0018 0.1146 0.8836
#> 98  0.0008 0.0776 0.9216
#> 99  0.0002 0.0394 0.9604
#> 100 0.0000 0.0000 1.0000

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