Getting errors for equations while linear fitting.
41 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
the cyclist
2022-12-11
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 个评论
the cyclist
2022-12-11
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.
更多回答(1 个)
John D'Errico
2022-12-11
编辑:John D'Errico
2022-12-11
What is hsline?
help hsline
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
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Model Building and Assessment 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!