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!