[R] Help with residuals function.

rkevinburton at charter.net rkevinburton at charter.net
Fri Aug 21 19:56:00 CEST 2009


I have a misunderstanding on the residuals function in 'R'. In the stats package the residuals for the output of a HoltWinters fit is residuals.HoltWinters and the source looks like:

> stats:::residuals.HoltWinters
function (object, ...) 
object$x - object$fitted[, 1]
<environment: namespace:stats>
> 

If I execute the following code (I only include the forecast package for the data set):

library("forecast")
library("expsmooth")
library("fma")
f <- HoltWinters(wineind)

This results in a HoltWinters fit to the data in the variable 'f'. If I look at the first few data points:

 f$x[1:10]
 [1] 15136 16733 20016 17708 18019 19227 22893 23739 21133 22591

And at the fitted values:

 f$fitted[1:10,1]
 [1] 14043.33 16849.90 18885.04 20474.27 18836.84 21665.11 24118.32 25198.27
 [9] 22901.40 24450.89

Then at the residuals:

 residuals(f)[1:10]
 [1]   984.6741  1127.0982  1122.9566   879.7321   661.1564   459.8873
 [7]  1698.6798  3580.7276 -1941.4028 -2196.8865

Take the first element. It is clearly not simply f$x[1] - f$fitted[1,1] (which is 1092.674 not 984.6741). So my question is, "What is actually being subtracted to get the residuals?"

Thank you.

Kevin




More information about the R-help mailing list