clear;

x = [ 1 2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19  20]';

y = [ 2.6888   -0.4724    3.7286    5.6471    1.1164    4.3160    5.2080  -0.3875    0.0181    4.1423    2.3002    4.5800    4.9312    4.8238    6.0805    4.9372    6.0817    1.3951    3.8604    3.6866]';

n = length(x);
plot(x,y);
hold on;
title('Regresión lineal');
xlabel('x');
ylabel('y');

M = [x, ones(n, 1)];

p = inv(M'*M)*M'*y;
m = p(1)
b = p(2)

yp = mean(y)

v1 = 1;
v2 = n-2;

plot(x, M*p, 'r');
plot(x, ones(n,1)*yp, 'g');

SSR = (yp -m*x - b)'*(yp -m*x - b)
SSE = (y -m*x - b)'*(y -m*x - b);
s2_SSE = SSE/v2
s2_SSR = SSR/v1
