How to find uncertainties in Intercept and Slope of a fitted line
조회 수: 28 (최근 30일)
이전 댓글 표시
Hi everyone,
I am trying to calculate the uncertainties in the intercept and the slope of a fitted line seperately. Here is my code to fitting a linear line. Can anyone help me to find the uncertainties using polyfit function or any other function?
Thank you.
for bands = 1:7
% Fit line to data using polyfit
c(bands,:) = polyfit(Sun_Zenith_AA_ASD,ratio(:,bands),1);
% Evaluate fit equation using polyval
y_est(bands,:) = polyval(c(bands,:),Sun_Zenith_AA_ASD);
% Add trend line to plot
p_1= plot(Sun_Zenith_AA_ASD,y_est(bands,:),'b--','LineWidth',2)
hold on
% calculating r-sq and RMSE
mdl{:,bands} = fitlm(Sun_Zenith_AA_ASD,ratio(:,bands));
r_sq(bands,:) = round((mdl{:,bands}.Rsquared.Adjusted),4);
RMSE(bands,:) = round((mdl{:,bands}.RMSE),4);
end
댓글 수: 0
채택된 답변
William Rose
2022년 6월 30일
The uncertainties in the fitted parameters are available in the model structure which is created when you call fitlm(). Use coefCI(mdl) to get the 95% confidence intervals for the estimated a0 and estimated a1. Here is what I mean.
Sun_Zenith_AA_ASD=[1:20]'; %x-values to demonstrate
%Next: generate 7 sets of y values: y=a0 + a1*x + noise
a0=70:-10:10; %intercept values
a1=-3:3; %slope values
ratio=a0.*ones(20,1)+a1.*Sun_Zenith_AA_ASD + 2*randn(20,7); %y-values
%Next: Fit columns 1 and 2 of y-data.
%Column 1: a0=70, a1=-3. Column 2: a0=60, a1=-2. Etc.
for i=1:7
mdl=fitlm(Sun_Zenith_AA_ASD,ratio(:,i)); %fit the linear model
a0est=mdl.Coefficients{1,1};
a1est=mdl.Coefficients{2,1};
confInt=coefCI(mdl);
%Display results on console.
fprintf('Model %d: Est.intercept=%.2f, 95%% C.I.=%.2f,%.2f\n',...
i,a0est,confInt(1,:));
fprintf(' Est. slope =%.2f, 95%% C.I.=%.2f,%.2f\n',...
a1est,confInt(2,:));
end
Try it. Good luck.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!