How to find uncertainties in Intercept and Slope of a fitted line
30 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Dinithi Siriwardana Pathiranage
il 29 Giu 2022
Risposto: William Rose
il 30 Giu 2022
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 Commenti
Risposta accettata
William Rose
il 30 Giu 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
Try it. Good luck.
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Linear and Nonlinear Regression in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!