Getting errors for equations while linear fitting.

47 views (last 30 days)
I have two sets of data x and y. After performing a scatter plot of the data I performed a linear fit on the plot using hsline and obtained the equation y = ax + b. But I want to obtain the errors in a and b such that the equation looks like y = (a+ delta a)*x + (b + delta b). How do I do this? I would also like to display the equation on the top left side of the graph.

Accepted Answer

the cyclist
the cyclist on 11 Dec 2022
I assume you meant lsline rather than hsline.
lsline does not compute the confidence intervals of the fit parameters. To do that, I would recommend using the fitlm function.
You have a couple options for adding text to a plot. Take a look at text or annotation. (The latter command is a little more challenging to use, but it the more flexible tool.)
  3 Comments
the cyclist
the cyclist on 11 Dec 2022
Here is an example:
% Set random number seed, for reproducibility
rng default
% Generate some simulated data
N = 10;
x = rand(N,1);
y = 2 + 3*x + 0.05*randn(N,1);
% Fit the model
mdl = fitlm(x,y);
% Extract coefficients and standard errors
coef = mdl.Coefficients.Estimate;
se = mdl.Coefficients.SE;
% Make a string that expresses equation
eqString = sprintf("Fit: y = (%4.2f +/- %4.2f) + (%4.2f +/- %4.2f)*x + error",coef(1),se(1),coef(2),se(2));
% Plot data and fit line
figure
hold on
plot(x,y,'*')
plot([0; 1],predict(mdl,[0; 1]),'r-')
% Annotate the plot with the equation
annotation('textbox',[0.17 0.6 0.5 0.3],'String',eqString,'FitBoxToText','on');
You will undoubtedly need to adjust several of the constants here (e.g. the placement of the textbox), and the format of the string that expresses the equation. Both sprintf and annotation can be a bit tricky to learn and use. But they will be very good tools for you, if you learn them.

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 11 Dec 2022
Edited: John D'Errico on 11 Dec 2022
What is hsline?
help hsline
hsline not found. Use the Help browser search field to search the documentation, or type "help help" for help command options, such as help for methods.
It is not found in any MATLAB toolbox. So if hsline is code you found somewhere, or whatever, then you will need to write the code to compute the confidence intervals you want to see.
Perhaps you are actually using lsline, not hsline, which does not seem to exist. I also did a quick check on the File Exchange, but found nothing with that name.
help lsline
LSLINE Add least-squares fit line to scatter plot. LSLINE superimposes the least squares line in the current axes for plots made using PLOT, LINE, SCATTER, or any plot based on these functions. Any line objects with LineStyles '-', '--', or '.-' are ignored. LSLINE(AX) plots into AX instead of GCA. H = LSLINE(...) returns the handle to the line object(s) in H. See also POLYFIT, POLYVAL, REFLINE. Documentation for lsline doc lsline
Anyway, lsline does not return the confidence intervals. Simplest is probably to use fit (fro mthe curve fitting toolbox, if you have it), which DOES generate confidence intervals for you easily, and also will allow you to plot the line trivially too. Displaying the equation itself will require you to format it as text, and then you can use a tool like text to place it on the figure. Or you could put it in the title as I often do.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!