How can I find where to split a piecewise regression and find the parameters involved in the function?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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!
0 Kommentare
Antworten (1)
Matt J
am 10 Dez. 2021
Bearbeitet: Matt J
am 10 Dez. 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.,
Siehe auch
Kategorien
Mehr zu Interpolation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!