How to do weighted least square rgression?
Show older comments
Hello
I want to fit an exponential curve to my data using MatLab. Could you please guide me how can I write a code to fit this through weighted least square reression? The weight matrix is as follows. I could not find any instruction or sample code to do so!
weight=[18.5204555170429; 24.8007492441765; 21.4204953742493; 12.0007299687922; 5.17482448096073;2.24987321147564]
x=[1.5;4.5;7.5;10.5;13.5;16.5]
y=[0.466132177250491;0.307694759285704;0.477550022494737;0.489169081512968;0.439027124884140;0.306938063199741]
Thanks
Answers (1)
Star Strider
on 6 Oct 2023
Edited: Star Strider
on 6 Oct 2023
weight=[18.5204555170429; 24.8007492441765; 21.4204953742493; 12.0007299687922; 5.17482448096073;2.24987321147564];
x=[1.5;4.5;7.5;10.5;13.5;16.5];
y=[0.466132177250491;0.307694759285704;0.477550022494737;0.489169081512968;0.439027124884140;0.306938063199741];
mdl = fitlm(x,y, 'Weights',weight)
figure
plot(mdl)
grid
I assume that you want to do a linear regression, since you did not mention a nonllinear model of any sort.
.
6 Comments
Pooneh Shah Malekpoor
on 6 Oct 2023
It would have been nice to know that at the outset.
weight=[18.5204555170429; 24.8007492441765; 21.4204953742493; 12.0007299687922; 5.17482448096073;2.24987321147564];
x=[1.5;4.5;7.5;10.5;13.5;16.5];
y=[0.466132177250491;0.307694759285704;0.477550022494737;0.489169081512968;0.439027124884140;0.306938063199741];
fcn = @(b,x) b(1) .* exp(x-b(2)) - b(3) .* exp(-(x-b(4))); % Nonlinear Exponential Model
mdl = fitnlm(x,y,fcn,[rand; 7; 12; rand], 'Weights',weight)
xv = linspace(min(x), max(x))';
[yp,yci] = predict(mdl, xv);
figure
plot(x, y, 'p')
hold on
plot(xv, yp, '-r')
plot(xv, yci, '--r')
hold off
grid
I have no idea what sort of exponential model you want. This gives an approximation of one. Experiment with others, since you understand the process that created these data, and a mathematical model of that process would be the best option for ‘fcn’ here.
Shiba shankar
on 1 Dec 2023
I have a doubt that if you don't know the weights, how you can get the weights for WLS
I have only two data sets.
Torsten
on 1 Dec 2023
The weights must be known and are usually derived from the "degree of certainty" of the data. The more certain a data point, the higher its weight.
Shiba shankar
on 1 Dec 2023
how to know then,any steps to know
Torsten
on 1 Dec 2023
The explanations given here might help:
Categories
Find more on Linear Predictive Coding in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
