I have done a simple test using R, take a look at the code below:
First of all I have created three samples:
> a = rnorm(10)
> b = rnorm(10)
> c = rnorm(10)
> a
[1] -0.2485833 -1.3077108 0.4019243 0.4453618 0.3024991 -0.4228684
[7] -0.3817301 0.2195161 1.3693408 -1.1030199
> b
[1] 0.1795048 -0.8764778 0.6097460 -1.0405654 0.8688749 -1.5191619
[7] 0.8955941 0.7544877 -1.0576488 1.7317130
> c
[1] -0.29437630 -0.96914835 0.16349019 0.08477029 -0.52696295 0.28241544
[7] -1.49860175 -0.26208560 -0.60898891 -1.67154576
Then, I've done a linear regression on those three samples:
> mod = lm(a ~ b + c + 0)
> mod
Call:
lm(formula = a ~ b + c + 0)
Coefficients:
b c
-0.02748 0.37456
Perfect, now I have the residuals of the linear regression.
(note: I set the intercept to zero)
Now, I print the residuals:
> mod$residuals
1 2 3 4 5 6 7
-0.1333896 -0.9687917 0.3574424 0.3850173 0.5237530 -0.5703937 0.2041941
8 9 10
0.3384146 1.5683807 -0.4293429
If I'm not wrong in this case(without intercept) the residuals are calculated doing:
a - (b*coef_b + c*coef_c)
If I do it manually I have:
> a - ((b*-0.02748) + (c*0.37456))
[1] -0.1333890 -0.9687922 0.3574432 0.3850155 0.5237550 -0.5703965
[7] 0.2041971 0.3384162 1.5683795 -0.4293383
if you look at the numbers closely you will notice a slight difference.
Could someone explain me the reason?
Thank you!