Tell me more ×
Cross Validated is a question and answer site for statisticians, data analysts, data miners and data visualization experts. It's 100% free, no registration required.

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!

share|improve this question

1 Answer

It looks like you're grabbing coef_b and coef_c from what you see from the model print out. R stores more digits than that so you should use coef(mod) to get the actual stored values.

So you should be able to do this:

a - coef(mod)[1]*b - coef(mod)[2]*c

Edit: To clarify what is going on. To get the prediction you could do something like

apred = coef(mod)[1]*b + coef(mod)[2]*c

If that seems wrong to you then you probably need to take a step back and review a few things about regression. The residual is just defined as the actual data point minus the prediction.

a - apred

replacing apred with how we defined it we get

a - (coef(mod)[1]*b + coef(mod)[2]*c)

distributing the negative into both terms in the parenthesis we get

a - coef(mod)[1]*b - coef(mod)[2]*c
share|improve this answer
2  
To expand slightly on the answer (+1), your typed-in coefficients have only 4 and 5 digits of accuracy, but R stores them to a lot more; thus, you cannot expect the residuals to match beyond about the 5th decimal place. – jbowman Dec 31 '11 at 0:51
@Dason, one moment, your formula is wrong (i think) it should be: a - (coef(mod)[1]*b + coef(mod)[2]*c) why did you always use - (minus) in the formula? – Dail Dec 31 '11 at 12:14
@Dason, I mean we have to ADD the regressors and multiply them by their coefficients, no? The result it is the same but A - B+C it's "more" correct in "writing" ? – Dail Dec 31 '11 at 12:23
Why are you subtracting one and not the other? I'll edit in some more details... – Dason Dec 31 '11 at 16:37
But really it doesn't matter at all. You're not being careful enough with your algebra. Note that A - B+C is not the same as A - (B+C). I believe you made that mistake in your original post. – Dason Dec 31 '11 at 16:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.