| evaluate_spline {castor} | R Documentation |
Given a natural spline function Y:\R\to\R, defined as a series of Y values on a discrete X grid, evaluate its values (or derivative) at arbitrary X points. Supported splines degrees are 0 (Y is piecewise constant), 1 (piecewise linear), 2 (piecewise quadratic) and 3 (piecewise cubic).
evaluate_spline(Xgrid,
Ygrid,
splines_degree,
Xtarget,
extrapolate = "const",
derivative = 0)
Xgrid |
Numeric vector, listing x-values in ascending order. |
Ygrid |
Numeric vector of the same length as |
splines_degree |
Integer, either 0, 1, 2 or 3, specifying the polynomial degree of the spline curve Y between grid points. For example, 0 means Y is piecewise constant, 1 means Y is piecewise linear and so on. |
Xtarget |
Numeric vector, listing arbitrary X values on which to evaluate Y. |
extrapolate |
Character, specifying how to extrapolate Y beyond |
derivative |
Integer, specifying which derivative to return. To return the spline's value, set |
Spline functions are returned by some of castor's fitting routines, so evaluate_spline is meant to aid with the evaluation and plotting of such functions. A spline function of degree D≥q1 has continuous derivatives up to degree D-1. The function evaluate_spline is much more efficient if Xtarget is monotonically increasing or decreasing.
A numeric vector of the same length as Xtarget, listing the values (or derivatives, if derivative>0) of Y on Xtarget.
Stilianos Louca
# specify Y on a coarse X grid Xgrid = seq(from=0,to=10,length.out=10) Ygrid = sin(Xgrid) # define a fine grid of target X values Xtarget = seq(from=0,to=10,length.out=1000) # evaluate Y on Xtarget, either as piecewise linear or piecewise cubic function Ytarget_lin = evaluate_spline(Xgrid,Ygrid,splines_degree=1,Xtarget=Xtarget) Ytarget_cub = evaluate_spline(Xgrid,Ygrid,splines_degree=3,Xtarget=Xtarget) # plot both the piecewise linear and piecewise cubic curves plot(x=Xtarget, y=Ytarget_cub, type='l', col='red', xlab='X', ylab='Y') lines(x=Xtarget, y=Ytarget_lin, type='l', col='blue', xlab='X', ylab='Y')