MATLAB Answers

Brandon
2

Save cubic spline coefficients to use as response in regression

Asked by Brandon
on 24 Feb 2015
Latest activity Commented on by tzina kokkala on 15 Feb 2018
Hi community,
I have two vectors and I would like to fit a cubic spline to:
y=[18.93000031 19.42000008 19.51000023 19.67000008 19.68000031 19.71999931];
x=[58.61111111 67.32055556 70.56194444 74.22694444 78.39388889 85.11555556];
I would also like to save the parameters of the fit at every point to use in regression analysis as my response (I want to change other variables at the points where I take a measurement in an experimental design.) How can I do this. I've not a very advanced matlab user but I hope this is an easier question for a pro in the community to answer.

  2 Comments

(Despite my being reasonably knowledgable about splines and regression analysis) I have absolutely no idea what you mean by this statement:
"save the parameters of the fit at every point to use in regression analysis as my response"
Please clarify.
So between each x y pair as I understand there is a different cubic spline model or function that is used to generate the curve. This model has like 4 parameters I believe. I would like to save these parameters.

Sign in to comment.

2 Answers

Answer by John D'Errico
on 24 Feb 2015
Edited by John D'Errico
on 24 Feb 2015
 Accepted Answer

Ok. Why did you not say that? :)
x=[58.61111111 67.32055556 70.56194444 74.22694444 78.39388889 85.11555556];
y=[18.93000031 19.42000008 19.51000023 19.67000008 19.68000031 19.71999931];
S = spline(x,y)
S =
form: 'pp'
breaks: [58.611 67.321 70.562 74.227 78.394 85.116]
coefs: [5x4 double]
pieces: 5
order: 4
dim: 1
S.coefs
ans =
0.00053154 -0.013366 0.13235 18.93
0.00053154 0.00052218 0.020489 19.42
-0.0013274 0.005691 0.040628 19.51
0.00061302 -0.0089032 0.028855 19.67
0.00061302 -0.00124 -0.013411 19.68
Each row of S.coefs is the set of coefficients of one cubic polynomial segment. It can be evaluated by polyval, but be CAREFUL here. Those cubic polynomials are defined to be evaluated relative to the break point at the beginning of that interval.
So if I wanted to evaluate the first cubic segment at the point xhat = 59, I would do this:
xhat = 59;
polyval(S.coefs(1,:),xhat - S.breaks(1))
ans =
18.979
ppval(S,59)
ans =
18.979
As you can see, it yields the same prediction as ppval did. The reason for this offset is it makes the polynomial evaluation more stable with respect to numerical problems.
As part of my SLM toolbox (found on the File Exchange), I do provide a tool that allows you to extract the polynomials in a symbolic form.
polys = slmpar(S,'symabs')
polys =
[1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double]
[1x1 sym ] [1x1 sym ] [1x1 sym ] [1x1 sym ] [1x1 sym ]
So in symbolic form I removed the break point offset that was built into the polynomial, then return it as a symbolic "function" of x.
polys{2,1}
ans =
0.00053154425392522449377030735462313*x^3 - 0.1068293870770472518768171460159*x^2 + 7.1771491067641370730246290387668*x - 141.76724752093537873401498012911
And if I now substitute 59 into that polynomial, you also get the value you should expect.
subs(polys{2,1},59)
ans =
18.979480689855906097289602061928
I'm still not positive exactly what you intend as a goal, so if you need more help, please add further clarification. For example, my own SLM toolbox gives you a tool that does regression spline modeling.

  3 Comments

Feel free to ask for more, as I am not certain what you will be doing with those cubic polynomial segments.
Are you trying now to use that spline itself in a regression? For example, if S(x) is the spline, are you trying to find calibration coefficients of some sort, so perhaps estimating the coefficients a and b below to fit some new set of data?
a + b*S(x)
ive been trying to get the coefficients from my cubic spline command thank you so much for this!
May I ask, how can one plot the cubic spline S(x) from the beginning until the end point of x, when all that is available is x ,y and S.coefs?

Sign in to comment.


Answer by Shoaibur Rahman on 24 Feb 2015

pp = spline(x,y);
NewValue = [1 2]; % may be scaler or vector
out = ppval(pp,NewValue)

  3 Comments

How do I read this? It gives me two values, which is which coefficient? What is NewValue? Is this for cubic splines as it only gives 2 values?
Perhaps, you have already got it! coefficients are in pp. NewValue is any value used for interpolation using the coefficients in pp. Thanks.
How would you find a polynomial equation from the given x and y points?

Sign in to comment.