csaps() in matlab makes a cubic spline according to the specific definition of the smoothing parameter p . Here is some matlab code and its result:
% x variable age = 75:99 % y variable diffs = [-39 -2 -167 -21 -13 32 -37 -132 -143 -91 -93 -88 -62 -112 -95 -28 -90 -40 -27 -23 -28 -11 -8 -6 1] % 0.0005 is the parameter p, and the later specification of % age are the desired x for prediction csaps(age,diffs,0.0005,age) % result (column headers removed): -63.4604 -64.0474 -64.6171 -65.1397 -65.6111 -66.0165 -66.3114 -66.4123 -66.2229 -65.6726 -64.7244 -63.3582 -61.5676 -59.3568 -56.7364 -53.7382 -50.4086 -46.7922 -42.9439 -38.9183 -34.7629 -30.5180 -26.2186 -21.8912 -17.5532
I would like to get the same result in R. I tried base::smooth.spline() , but the spar smoothing option is specified differently, which I don't seem to be matlab p (can you?). The closest result I could get was with the smooth.Pspline() function of the pspline package. Here is some code to make things roll in R:
age <- 75:99 diffs <- c(-39L, -2L, -167L, -21L, -13L, 32L, -37L, -132L, -143L, -91L, -93L, -88L, -62L, -112L, -95L, -28L, -90L, -40L, -27L, -23L, -28L, -11L, -8L, -6L, 1L) predict(pspline::smooth.Pspline( x = age, y = diffs, norder = 2, method = 1, spar = 1 / 0.0005
Csaps () help page here
smooth.spline() help can be found here (the code is not listed because I think the connection between spar and p quite hairy, so maybe you shouldn't go this way)
pspline::smooth.Pspline() help here
This quest from another person since 2008 seems to have remained unanswered, making me feel like this guy .
R is crowded with splines, so if the saavy among you can point me to one that does the same thing as matlab csaps() (or the trick along these lines), I would be very grateful.
[EDIT 19-8-2013] spar must be specified as (1-p)/p (not 1/p ), and then the results will be accurate to numerical precision. See answer below.