How to calculate the standard error estimation when using fit from curve fitting toolbox?

191 views (last 30 days)
Is is possible to calculate the standard error estimation when using fit from curve fitting toolbox as in polyfit?
Suppose I have 2 vector (x, y). Using polyfit and polyval gives the standard error estimation for all predictions.
How to calculate delta in fit? I need the prediction interval like examples below.
I assume the delta in polyval is not a scalar but varies with x. (Purhaps it is not?)
Example from the documention,
x = 1:100;
y = -0.3*x + 2*randn(1,100);
[p,S] = polyfit(x,y,1);
[y_fit,delta] = polyval(p,x,S);
plot(x,y,'bo')
hold on
plot(x,y_fit,'r-')
plot(x,y_fit+2*delta,'m--',x,y_fit-2*delta,'m--')
title('Linear Fit of Data with 95% Prediction Interval')
legend('Data','Linear Fit','95% Prediction Interval')

Accepted Answer

Star Strider
Star Strider on 22 Jul 2021
Yes. Use the predint function.
x = linspace(0, 100, 100);
y = -0.3*x + 2*randn(1,100);
[f,gof,out] = fit(x(:), y(:), 'poly1')
f =
Linear model Poly1: f(x) = p1*x + p2 Coefficients (with 95% confidence bounds): p1 = -0.2946 (-0.3081, -0.2811) p2 = -0.5298 (-1.311, 0.251)
gof = struct with fields:
sse: 384.9559 rsquare: 0.9504 dfe: 98 adjrsquare: 0.9499 rmse: 1.9819
out = struct with fields:
numobs: 100 numparam: 2 residuals: [100×1 double] Jacobian: [100×2 double] exitflag: 1 algorithm: 'QR factorization and solve' iterations: 1
ci = predint(f, x);
figure
plot(f, x, y)
hold on
plot(x, ci, '--')
hold off
grid
hl = legend;
hl.String{3} = 'Lower 95% CI';
hl.String{4} = 'Upper 95% CI';
.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!