How to find uncertainties in Intercept and Slope of a fitted line

31 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

William Rose
William Rose am 30 Jun. 2022
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
Model 1: Est.intercept=71.46, 95% C.I.=69.67,73.25
Est. slope =-3.14, 95% C.I.=-3.29,-2.99
Model 2: Est.intercept=58.84, 95% C.I.=56.54,61.14
Est. slope =-1.91, 95% C.I.=-2.10,-1.72
Model 3: Est.intercept=49.07, 95% C.I.=46.40,51.75
Est. slope =-0.93, 95% C.I.=-1.16,-0.71
Model 4: Est.intercept=38.90, 95% C.I.=37.13,40.66
Est. slope =0.05, 95% C.I.=-0.10,0.19
Model 5: Est.intercept=30.77, 95% C.I.=29.26,32.29
Est. slope =0.99, 95% C.I.=0.86,1.11
Model 6: Est.intercept=20.62, 95% C.I.=18.54,22.70
Est. slope =1.94, 95% C.I.=1.76,2.11
Model 7: Est.intercept=10.13, 95% C.I.=7.98,12.28
Est. slope =3.02, 95% C.I.=2.84,3.19
Try it. Good luck.

Weitere Antworten (0)

Kategorien

Mehr zu Linear and Nonlinear Regression finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by