Samuel Granjeaud IR/Inserm
2020-Apr-30 11:47 UTC
[R] stats:: spline's method could not be monoH.FC
Hi, I have just noticed that the argument method of the spline function of the stats package does not allow to specify monoH.FC although the documentation tells it should be possible. I know how to program a workaround. This post intends to alert the maintainers. Stay safe, Samuel [[alternative HTML version deleted]]
I agree, the documentation gives the impression that stats::spline would allow "monoH.FC". My guess is that stats::spline doesn't allow it because the function is designed to return a list with x and y components. This doesn't suit monotonic cubic Hermite splines because the function would also need to return the slopes. Furthermore, additional functions may (or may not) be required depending on what you want to do with x, y and slope vectors. Note that my package kubik, provides a range of functions for working with cubic Hermite splines. On Fri, May 1, 2020 at 4:39 AM Samuel Granjeaud IR/Inserm <samuel.granjeaud at inserm.fr> wrote:> > Hi, > > I have just noticed that the argument method of the spline function of > the stats package does not allow to specify monoH.FC although the > documentation tells it should be possible. > > I know how to program a workaround. This post intends to alert the > maintainers. > > Stay safe, > Samuel > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
>>>>> Abby Spurdle >>>>> on Fri, 1 May 2020 17:21:32 +1200 writes:> I agree, the documentation gives the impression that stats::spline > would allow "monoH.FC". You are right, this is a lapsus, thank you for reporting it! > My guess is that stats::spline doesn't allow it because the function > is designed to return a list with x and y components. This doesn't > suit monotonic cubic Hermite splines because the function would also > need to return the slopes. Furthermore, additional functions may (or > may not) be required depending on what you want to do with x, y and > slope vectors. Well, not quite. AFAIR, the reasons I did not add the option for spline() were mostly - using splinefun() is uniformly more flexible than using spline() {you can always get an (x,y) coordinate list from splinefun()'s result later} - splinefun() is using's R feature of "(non-trivial) closure", i.e, it returns a *function* containing its own state - convenience (less work, notably less maintenance burden) Still, in spite of the above: If somebody provides minimal patches (keeping *elegant* code) to allow spline() to also allow method="monoH.FC", I'd consider applying that. For now, I'd rather amend the documentation (as Samuel proposes) > Note that my package kubik, provides a range of functions for working > with cubic Hermite splines. That's interesting. For years, I had wanted to add a bit more functionality to R's builtin (but not widely known !!) package 'splines', 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 generalizing the usual ( "natural" ) boundary conditions s''(x_1) = 0, s''(x_n) = 0 and I'd "hope" it should really only need a few lines of R code to generalize from natural interpolating splines to such very slightly more general ones. (I don't see very quickly how to do this with 'kubik' either, but I've only looked for 5 minutes). --- Note that our CRAN package 'cobs' is really for (robust, namely quantile) regression splines where the user can also ask for monotonicity, convexity, concavity of s() and for bound constraints in different regions (and the implementation is fast for larger 'n', thanks to using sparse matrices)... which seems partly a generalization of 'kubik' as it's not only (just) for interpolation and still allows many shape and bound constraints.> citation("cobs")To cite the cobs package in publications use: Pin T. Ng and Martin Maechler (2020). COBS -- Constrained B-splines (Sparse matrix based). R package version 1.3-4. URL https://CRAN.R-project.org/package=cobs Ng P, Maechler M (2007). ?A Fast and Efficient Implementation of Qualitatively Constrained Quantile Smoothing Splines.? _Statistical Modelling_, *7*(4), 315-328. <URL:http://smj.sagepub.com/content/7/4/315.abstract>.