Linear regression: confidence interval

301 Views Asked by At

How to compute the confidence interval of a linear regression, please?

I found: $$ V = a \pm da $$ $$ da = t(percentile, N-2) . \sigma $$ $$\sigma = a \sqrt{ \frac{1/R^2 - 1}{N-2}} $$

Are these formulas correct?

I have plotted data using matlab. The polyval function outputs the delta is proportional to the confidence interval: $$ CI = k \; Delta $$ This coefficient $k$ can be calculated with the t-distribution (tinv? function on Matlab + Statistical Toolbox). For the moment, I used $k=1.96$ because I'm not really sure about the value to choose.

Linear regression example

From https://www.real-statistics.com/regression/confidence-and-prediction-intervals/: Formula

Here is the matlab code to generate the figure:

%%
% fake data

n=10; %n points

xs = linspace(0,50,n);
ys = 3 * xs + 2;

noise = 15*randn(1, n); %generate noise    
ys = ys + noise;   %add noise


%%
[p, S] = polyfit(xs,ys,1);
[yhat, delta] = polyval(p, xs, S); 

% residues
residues = yhat - ys;
squarredResidues = residues.^2;
stdResidues = std(residues);

k=1.96; % student's t-distribution => HOW?
yhatmin = yhat - k*delta;
yhatmax = yhat + k*delta;

% plot data
figure(1); clf

plot(xs, ys, 'kx'); hold on;
xlabel('x');
ylabel('y');

plot(xs, yhat, 'b-');
plot(xs, yhatmin, 'r--');
plot(xs, yhatmax, 'r--');