[R] stats:: spline's method could not be monoH.FC

Martin Maechler m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Mon May 4 16:25:02 CEST 2020


>>>>> Abby Spurdle 
>>>>>     on Sun, 3 May 2020 16:15:17 +1200 writes:

    >> and just today a colleague asked me about spline interpolation
    >> with general 2nd derivative boundary conditions
    >> s''(x_1) = s2_1,  s''(x_n) = s2_2

actually I was wrong... I *read* it as the above, but what he
really wanted was what the wikipedia page  class "clamped"
boundary conditions, i.e., for the *first* derivative

   s'(x_1) = s1_1,  s'(x_n) = s1_2
   

    > It should possible via cubic Hermite splines.

and indeed that is available via cubic Hermite splines available
in R via stats package's  splinefunH()

{which I wrote when implementing  "monoH.FC"}

    > A nontrivial design decision in my package was the computation of
    > slopes at the endpoints.
    > (Something which I got wrong, twice...)

The "wikiversity" has a very nice small math lecture on this
 https://en.wikiversity.org/wiki/Cubic_Spline_Interpolation

which derives *both* cases (first and 2nd derivative boundary conditions),
the only draw back to quickly do it with ('base R') is that I
need to "translate" that (2nd derivative values $M_i$) parametrization
into either the one used into the (a,b,c,y)-parametrizat of the
default spline() / splinefun() methods or the Hilbert spline
form for  splinefunH(x[],y[],m[]).

Martin

    > My guess is that I could write a function in about 60 to 90 minutes,
    > including all the testing and calculus.

    > However, I need to note two things:
    > (1) Cubic Hermite splines do not have a continuous second derivative.

well, many don't if you allow any slopes at node. However, if
you only set 2 boundary conditions *instead* of the natural
spline ones  f''(x_1) = f''(x_1) = 0, 
you can still remain in C_2 (i.e. continuous 2nd derivative).

(And that's also what the above wikiversity lecture provides).

    > (2) Specifying the second derivatives (at the endpoints) would prevent
    > the user from specifying the first derivatives (aka the slopes).

    > Could you please confirm if the function would still be of interest...?

Well, as I know spent enough time reading and thinking, I'd
really like to add   method = "clamped" to splinefun() and also
the other one where fix the 2nd derivatives (to arbitrary values
instead of zero).

So if you already have the R code leading up to one of the 2
"parametrizations" we use in spline() / splinefun(),  I'd be
grateful, if you have the time.

Martin



More information about the R-help mailing list