```
bundle dw
T = $nobs
if T<16
funcerr "Insufficient observations"
endif
ols y xlist --quiet
scalar SCE = $ess
matrix uhat = $uhat
scalar ut = 0
loop i = 5..T --quiet
scalar ui = (uhat[i,1] - uhat[i-4,1])^2
ut = ut + ui
endloop
scalar DeWallis_stat = ut/SCE
#Table for DeWallis Statistic for a significance of five percent
#Regression with constant term and NO dummy variables.
#Critical Values from one up to five regressors (not considering the constant term)
#Row 1
matrix A = {16, 0.774, 0.982, 0.662, 1.109, 0.549, 1.275, 0.435, 1.381, 0.350, 1.532}
#Row 2
matrix A = A | {20, 0.924, 1.102, 0.827, 1.203, 0.728, 1.327, 0.626, 1.428, 0.544, 1.556}
#Row 3
matrix A = A | {24, 1.036, 1.189, 0.953, 1.273, 0.867, 1.371, 0.779, 1.459, 0.702, 1.565}
#Row 4
matrix A = A | {28, 1.123, 1.257, 1.050, 1.328, 0.975, 1.410, 0.898, 1.487, 0.828, 1.576}
#Row 5
matrix A = A | {32, 1.192, 1.311, 1.127, 1.373, 1.061, 1.443, 0.993, 1.511, 0.929, 1.587}
#Row 6
matrix A = A | {36, 1.248, 1.355, 1.191, 1.410, 1.131, 1.471, 1.070, 1.532, 1.013, 1.598}
#Row 7
matrix A = A | {40, 1.295, 1.392, 1.243, 1.442, 1.190, 1.496, 1.135, 1.550, 1.082, 1.609}
#Row 8
matrix A = A | {44, 1.335, 1.423, 1.288, 1.469, 1.239, 1.518, 1.189, 1.567, 1.141, 1.620}
#Row 9
matrix A = A | {48, 1.369, 1.451, 1.326, 1.493, 1.281, 1.537, 1.236, 1.582, 1.191, 1.630}
#Row 10
matrix A = A | {52, 1.399, 1.475, 1.359, 1.513, 1.318, 1.554, 1.276, 1.595, 1.235, 1.639}
#Row 11
matrix A = A | {56, 1.426, 1.496, 1.389, 1.532, 1.351, 1.569, 1.312, 1.608, 1.273, 1.648}
#Row 12
matrix A = A | {60, 1.449, 1.515, 1.415, 1.548, 1.379, 1.583, 1.343, 1.619, 1.307, 1.656}
#Row 13
matrix A = A | {64, 1.470, 1.532, 1.438, 1.563, 1.405, 1.596, 1.371, 1.629, 1.337, 1.664}
#Row 14
matrix A = A | {68, 1.489, 1.548, 1.459, 1.557, 1.427, 1.608, 1.396, 1.639, 1.364, 1.671}
#Row 15
matrix A = A | {72, 1.507, 1.562, 1.478, 1.589, 1.448, 1.618, 1.418, 1.648, 1.388, 1.678}
#Row 16
matrix A = A | {76, 1.522, 1.574, 1.495, 1.601, 1.467, 1.628, 1.439, 1.656, 1.411, 1.685}
#Row 17
matrix A = A | {80, 1.537, 1.586, 1.511, 1.611, 1.484, 1.637, 1.457, 1.663, 1.431, 1.691}
#Row 18
matrix A = A | {84, 1.550, 1.597, 1.525, 1.621, 1.500, 1.646, 1.475, 1.671, 1.449, 1.696}
#Row 19
matrix A = A | {88, 1.562, 1.607, 1.539, 1.630, 1.515, 1.654, 1.490, 1.677, 1.466, 1.702}
#Row 20
matrix A = A | {92, 1.574, 1.617, 1.551, 1.639, 1.528, 1.661, 1.505, 1.684, 1.482, 1.707}
#Row 21
matrix A = A | {96, 1.584, 1.626, 1.563, 1.647, 1.541, 1.668, 1.519, 1.690, 1.496, 1.712}
#Row 22
matrix A = A | {100, 1.594, 1.634, 1.573, 1.654, 1.552, 1.674, 1.531, 1.695, 1.510, 1.717}
#A : DeWallis_stat_table_nd
#Table for DeWallis Statistic for a significance of five percent
#Regression with dummy variables (and constant term, of course).
#Critical Values from one up to five regressors (not considering the dummy variables nor the constant term)
#Row 1
matrix B = {16, 1.156, 1.381, 1.031, 1.532, 0.902, 1.776, 0.777, 2.191, 0.693, 2.238}
#Row 2
matrix B = B | {20, 1.228, 1.428, 1.123, 1.556, 1.013, 1.726, 0.889, 1.954, 0.806, 2.042}
#Row 3
matrix B = B | {24, 1.287, 1.459, 1.199, 1.565, 1.107, 1.694, 1.011, 1.856, 0.928, 1.949}
#Row 4
matrix B = B | {28, 1.337, 1.487, 1.261, 1.576, 1.181, 1.679, 1.099, 1.803, 1.025, 1.889}
#Row 5
matrix B = B | {32, 1.379, 1.511, 1.312, 1.587, 1.243, 1.673, 1.171, 1.773, 1.104, 1.850}
#Row 6
matrix B = B | {36, 1.414, 1.532, 1.355, 1.598, 1.293, 1.672, 1.230, 1.755, 1.170, 1.824}
#Row 7
matrix B = B | {40, 1.445, 1.550, 1.391, 1.609, 1.336, 1.674, 1.279, 1.745, 1.225, 1.807}
#Row 8
matrix B = B | {44, 1.471, 1.567, 1.442, 1.620, 1.373, 1.677, 1.321, 1.739, 1.272, 1.795}
#Row 9
matrix B = B | {48, 1.494, 1.582, 1.450, 1.630, 1.404, 1.681, 1.357, 1.737, 1.312, 1.788}
#Row 10
matrix B = B | {52, 1.514, 1.595, 1.474, 1.639, 1.432, 1.686, 1.389, 1.736, 1.347, 1.782}
#Row 11
matrix B = B | {56, 1.533, 1.608, 1.495, 1.648, 1.456, 1.691, 1.416, 1.736, 1.377, 1.779}
#Row 12
matrix B = B | {60, 1.549, 1.619, 1.514, 1.656, 1.478, 1.696, 1.441, 1.737, 1.404, 1.777}
#Row 13
matrix B = B | {64, 1.564, 1.629, 1.531, 1.664, 1.497, 1.700, 1.463, 1.739, 1.429, 1.776}
#Row 14
matrix B = B | {68, 1.577, 1.639, 1.546, 1.671, 1.515, 1.705, 1.482, 1.741, 1.450, 1.775}
#Row 15
matrix B = B | {72, 1.590, 1.684, 1.560, 1.678, 1.531, 1.710, 1.500, 1.743, 1.470, 1.776}
#Row 16
matrix B = B | {76, 1.601, 1.656, 1.573, 1.685, 1.545, 1.714, 1.517, 1.746, 1.488, 1.776}
#Row 17
matrix B = B | {80, 1.611, 1.663, 1.585, 1.691, 1.559, 1.719, 1.531, 1.748, 1.504, 1.777}
#Row 18
matrix B = B | {84, 1.621, 1.671, 1.596, 1.696, 1.571, 1.723, 1.545, 1.751, 1.519, 1.778}
#Row 19
matrix B = B | {88, 1.630, 1.677, 1.607, 1.702, 1.582, 1.727, 1.558, 1.753, 1.553, 1.779}
#Row 20
matrix B = B | {92, 1.639, 1.684, 1.616, 1.707, 1.593, 1.731, 1.570, 1.756, 1.546, 1.781}
#Row 21
matrix B = B | {96, 1.647, 1.690, 1.625, 1.712, 1.603, 1.735, 1.580, 1.759, 1.558, 1.782}
#Row 22
matrix B = B | {100, 1.654, 1.695, 1.633, 1.717, 1.612, 1.739, 1.591, 1.761, 1.569, 1.784}
#B : DeWallis_stat_table_d
#De Wallis test
if dummy
scalar k = nelem(xlist) - 4
if k>5
funcerr "The number of regressors has to be less or equal to 5 (withot considering the constant term nor the dummy variables)"
endif
if k<1
funcerr "Insufficient number of regressors. Please make sure you input at least one independent variable and the three dummy"
endif
scalar row = T/4 -3
scalar row = floor(row)
if row>22
scalar row = 22
print "--------------------------------------------------------------"
print "The input data includes more observations that those supported"
print "by the De Wallis statistical table included in this function"
print "For that reason, the test has been realized with the value"
print "for 100 observations"
print "--------------------------------------------------------------"
endif
scalar col_left = 2*k
scalar col_right = 2*k +1
if DeWallis_stat < B[row , col_left]
string DeWallis_test_r = "We reject H0, there is evidence of negative autocorrelation"
else
if DeWallis_stat > B[row, col_right]
string DeWallis_test_r = "We reject H0, there is evidence of positive autocorrelation"
else
string DeWallis_test_r = "The test does not provide enough evidence to suggest autocorrelation"
endif
endif
else #De Wallis test without dummy
scalar k = nelem(xlist) - 1
if k>5
funcerr "The number of regressors has to be less or equal to 5 (withot considering the constant term)"
endif
if k<1
funcerr "Insufficient number of regressors. Please make sure you input at least one independent variable"
endif
scalar row = T/4 -3
scalar row = floor(row)
if row>22
scalar row = 22
print "--------------------------------------------------------------"
print "The input data includes more observations that those supported"
print "by the De Wallis statistical table included in this function"
print "For that reason, the test has been realized with the value"
print "for 100 observations"
print "--------------------------------------------------------------"
endif
scalar col_left = 2*k
scalar col_right = 2*k +1
if DeWallis_stat < A[row , col_left]
string DeWallis_test_r = "We reject H0, there is evidence of negative autocorrelation"
else
if DeWallis_stat > A[row, col_right]
string DeWallis_test_r = "We reject H0, there is evidence of positive autocorrelation"
else
string DeWallis_test_r = "The test does not provide enough evidence to suggest autocorrelation"
endif
endif
endif
printf "The De Wallis test statistic is %.6g\n", DeWallis_stat
printf "The result of the test is: %.100s\n", DeWallis_test_r
dw["De Wallis statisitic"] = DeWallis_stat
dw["Test result"] = DeWallis_test_r
return dw
```