I used the following data and tried to compare the Residuals values provided by the model and calculated manually. Both results are same by visual inspection but R is behaving in a different manner.
library(MASS)
lm.fit=lm(medv~lstat,data=Boston)
Boston$lmedv[1:10] #Actual values
[1] 24.0 21.6 34.7 33.4 36.2 28.7 22.9 27.1 16.5 18.9
lm.fit$fitted.values[1:10] #Predicted values
1 2 3 4 5 6 7
29.822595 25.870390 30.725142 31.760696 29.490078 29.604084 22.744727
8 9 10
16.360396 6.118864 18.307997
a<-Boston$medv[1:10]-lm.fit$fitted.values[1:10] #Manual Cal. of Residual
1 2 3 4 5 6 7
-5.8225951 -4.2703898 3.9748580 1.6393042 6.7099222 -0.9040837 0.1552726
8 9 10
10.7396042 10.3811363 0.5920031
lm.fit$residuals[1:10] #Residual cal. Provided by lm model
1 2 3 4 5 6 7
-5.8225951 -4.2703898 3.9748580 1.6393042 6.7099222 -0.9040837 0.1552726
8 9 10
10.7396042 10.3811363 0.5920031
class(a)
[1] "numeric"
class(lm.fit$residual[1:10])
[1] "numeric"
a==lm.fit$residuals[1:10]
1 2 3 4 5 6 7 8 9 10
TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE FALSE
table(a==lm.fit$residuals[1:10]
FALSE TRUE
6 4
The values are same and also their class is also same . But why R is behaving in a strange manner and listed them as FALSE in the comparison. Did i miss something ? Please tell me .