Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I evaluate the derivative of a spline function in R?

R can generate a spline function using splinefun() in the splines library. However, I need to evaluate this function at its first and second derivatives. Is there a way to do this?

for example

library(splines)
x <- 1:10
y <- sin(pi/x) #just an example
f_of_x <- splinefun(x,y)

How can I evaluate f'(x) for a vector of x's?

like image 853
David LeBauer Avatar asked Dec 02 '10 19:12

David LeBauer


People also ask

What does spline function do in R?

Split() is a built-in R function that divides a vector or data frame into groups according to the function's parameters. It takes a vector or data frame as an argument and divides the information into groups.

What is spline analysis?

In the mathematical field of numerical analysis, spline interpolation is a form of interpolation where the interpolant is a special type of piecewise polynomial called a spline.

What is Cubic Spline in R?

Cubic regression spline is a form of generalized linear models in regression analysis. Also known as B-spline, it is supported by a series of interior basis functions on the interval with chosen knots. Cubic regression splines are widely used on modeling nonlinear data and interaction between variables.

How many degrees of freedom does a regression spline have?

The spline has four parameters on each of the K+1 regions minus three constraints for each knot, resulting in a K+4 degrees of freedom.


2 Answers

It is very easy to do since the ability to evaluate the function at its derivatives is built in to the function!

 f_of_x(x, deriv = 1)

Thanks R-core!

like image 198
David LeBauer Avatar answered Sep 22 '22 23:09

David LeBauer


You may also be interested in the TkSpline function in the TeachingDemos package which will plot the spline function along with its derivatives.

like image 24
Greg Snow Avatar answered Sep 19 '22 23:09

Greg Snow