How can I find where to split a piecewise regression and find the parameters involved in the function?
2 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (1 件)
Matt J
2021 年 12 月 10 日
編集済み: Matt J
2021 年 12 月 10 日
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.,
参考
カテゴリ
Help Center および File Exchange で Interpolation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!