-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinearRegression.m
71 lines (50 loc) · 1.49 KB
/
linearRegression.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
clear all
close all
x = [1 2 3 4 5 6 7 8 9 10];
y = [1.1 2.3 4.5 5.1 6.7 7.4 8.9 10.0 11.1 11.9];
% Fit a linear model:
% y = beta0 + beta1 * x
model = fitlm(x, y, 'VarNames', {'x', 'y'});
% Display the results of the linear model fit
disp(model);
% Visualise the residuals
figure
plotResiduals(model, 'fitted');
%%% Calculate 99% Confidence Interval
% Number of Samples
n = model.NumObservations;
% Degrees of Freedom
df = model.DFE;
% Standard Deviation
s = 1;
% Intercept
beta0_hat = model.Coefficients.Estimate(1);
% Production Rate
beta1_hat = model.Coefficients.Estimate(2);
% Data Mean
x_mean = mean(x);
y_mean = mean(y);
% yield_i_hat
y_i_hat = beta0_hat + beta1_hat * x;
% Confidence Interval
confidence = 0.99;
area = 1 - (1 - confidence) / 2;
upper_y_i_hat = y_i_hat + tinv(area, df) .* s .* sqrt(1 / n + (x - x_mean).^2 / sum((x - x_mean).^2));
lower_y_i_hat = y_i_hat - tinv(area, df) .* s .* sqrt(1 / n + (x - x_mean).^2 / sum((x - x_mean).^2));
% beta1 CI99
disp([beta1_hat - tinv(area, df) * model.Coefficients.SE(2), beta1_hat + tinv(area, df) * model.Coefficients.SE(2)])
% Visualise Points
figure
plot(x, y, 'bx');
hold on
fplot(@(x) beta0_hat + beta1_hat * x, [min(x) max(x)], 'r-')
plot(x, upper_y_i_hat, 'r:')
plot(x, lower_y_i_hat, 'r:')
xlabel('x1')
ylabel('y')
title('y vs. x1')
legend('Data', 'Fit', 'Confidence bounds', 'Location', 'southeast')
xPad = (max(x) - min(x)) * 0.1;
yPad = (max(y) - min(y)) * 0.4;
axis([min(x)-xPad, max(x)+xPad, min(y)-yPad, max(y)+yPad]);
hold off