How can I find where to split a piecewise regression and find the parameters involved in the function?

2 visualizzazioni (ultimi 30 giorni)
Dear matlab community:
I am trying to fit several data sets with three distinct regions. Essentially, I want to fit the coefficients (a1,a2,a3,c1,c3, delta) of the following piecewise function, where I also want to determine the best kinks location (d,e):
I uploaded my x and y vectors. Graphically, this I what I would like to have:
So far I have tried to define the following functions, but without success:
function y = piecewise1(x,a1,a2,a3,c1,c3,delta,e,d)
y = zeros(size(x));
for i = 1:length(x)
if x(i) < e,
y(i) = a1*(x(i)^(c1-1))-delta;
elseif e <= x(i) < d,
y(i) = (a1*(e^(c1-1))-delta)+ (a2*(x(i)-e));
else
y(i) = (a2*(d)) + (a3*((x(i)-d)^(c3-1)));
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y = piecewise2(x,a1,a2,a3,c1,c3,delta,e,d)
y = zeros(size(x));
for i = 1:length(x)
if x(i) < e,
y(i) = a1*(x(i)^(c1-1))-delta;
elseif e <= x(i) < d,
y(i) = (a2*(x(i)));
else
y(i) = (a3*((x(i))^(c3-1)));
end
end
end
First, between the piecewise1 and piecewise2 functions, I do not know which one is the correct one to obtain the piecewise function that I want. The first function considers the translations on the x-axis and the second one doesn't. Should I consider them?
Second, applying both functions to my data, neither one fits the function correctly (you can see this from my plot graphs):
ft1 = fittype( 'piecewise1(x,a1,a2,a3,c1,c3,delta,e,d)' )
f1 = fit( x',y', ft1, 'StartPoint', [0.1, 0.1, 0.1, 1.1, 2.1,-50,10000,23000] )
plot( f1, x, y )
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ft2 = fittype( 'piecewise2(x,a1,a2,a3,c1,c3,delta,e,d)' )
f2 = fit( x',y', ft2, 'StartPoint', [0.1, 0.1, 0.1, 1.1, 2.1,-50,10000,23000] )
plot( f2, x, y )
In fact, the fit is very bad. is there something I am doing wrong? Maybe in kinks definition on my function? I notice that the fit does not match the convex curvature at the end of the curve.
Thank you in advance!

Risposte (1)

Matt J
Matt J il 10 Dic 2021
Modificato: Matt J il 10 Dic 2021
Neither of your models look right. It looks, at minimum, that you need a additional translation parameters:
a_i*(x-t_i)^(c_i-1), i=1,2,3
Also, your choice of StartPoints, Lower, and Upper don't look right. Clearly a1 has to be negative and the convexity of the curves require that the c_1 and c_3 are both greater than 2.
Also, you probably want to enforce constraints so that the left and right derivatives are equal at the kink points. This would probably require fmincon() instead of fit().
Since your model seems unfinalized, I would recommend a free-knot spline fitting tool instead, e.g.,
  1 Commento
Angelavtc
Angelavtc il 10 Dic 2021
@Matt J thank you very much for your answer, what do you mean by "additional translation parameters"? I was graphing the piecewise function I need and I think the correct one would be to estimate the parameters [a1,a3,c1,c3,d,e,delta] of the following function (as you can see I add a translation parameter "e" in the last region):
Graphically, it looks something like this:
For this form to be satisfied, I have corroborated the initial conditions of a1 and c1. Which force that c1 must be concave (i.e., be between 1 and 2) while a1 must be positive.
Unfortunately, my piecewise function comes from an economic model process that prevents me from using the spline fitting tool. So any help to improve my piecewise estimation will be more than welcome. I will check the fmincon() option and return to you once I understand how it works. Thank you for helping me with this @Matt J

Accedi per commentare.

Categorie

Scopri di più su Interpolation in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by