lib/linefit.rb
############################################################################
# Return the Durbin-Watson statistic
#
#  durbinWatson = linefit.durbinWatson
#
# The Durbin-Watson test is a test for first-order autocorrelation in the
# residuals of a time series regression. The Durbin-Watson statistic has a
# range of 0 to 4; a value of 2 indicates there is no autocorrelation.
#
# The return value is undefined if the regression fails. If weights are
# input, the return value is the weighted Durbin-Watson statistic.
#
Return the Durbin-Watson statistic
durbinWatson = linefit.durbinWatson

The Durbin-Watson test is a test for first-order autocorrelation in the
residuals of a time series regression. The Durbin-Watson statistic has a
range of 0 to 4; a value of 2 indicates there is no autocorrelation.

The return value is undefined if the regression fails. If weights are
input, the return value is the weighted Durbin-Watson statistic.
Return the Durbin - Watson statistic durbinWatson = linefit . durbinWatson The Durbin - Watson test is a test for first - order autocorrelation in the residuals of a time series regression . The Durbin - Watson statistic has a range of 0 to 4 ; a value of 2 indicates there is no autocorrelation . The return value is undefined if the regression fails . If weights are input the return value is the weighted Durbin - Watson statistic .
def durbinWatson
      unless @durbinWatson
         self.regress or return
         sumErrDiff = 0
         errorTMinus1 = @y[0] - (@intercept + @slope * @x[0])
         1.upto(@numxy-1) do |i|
            error = @y[i] - (@intercept + @slope * @x[i])
            sumErrDiff += (error - errorTMinus1) ** 2
            errorTMinus1 = error
         end
         @durbinWatson = sumSqErrors() > 0 ? sumErrDiff / sumSqErrors() : 0
      end
      return @durbinWatson
   end
def durbinWatson unless @durbinWatson self . regress or return sumErrDiff = 0 errorTMinus1 = @y [ 0 ] - ( @intercept + @slope * @x [ 0 ] ) 1 . upto ( @numxy - 1 ) do | i | error = @y [ i ] - ( @intercept + @slope * @x [ i ] ) sumErrDiff += ( error - errorTMinus1 ) ** 2 errorTMinus1 = error end @durbinWatson = sumSqErrors ( ) > 0 ? sumErrDiff / sumSqErrors ( ) : 0 end return @durbinWatson end
Return the Durbin-Watson statistic
durbinWatson = linefit.durbinWatson
Return the Durbin - Watson statistic durbinWatson = linefit . durbinWatson