Marcos Larios Santa Rosa
0.3
2017-07-02
Levene's Test for Equality of Variances
C12
This function implements Levene's Test for equality of variance across groups. It can be used to detect problems of heteroskedasticity in a series, e.g., the residuals of a regression.
The calculation is as shown by the National Institue of Standards and Technology's Engineering Statistics Handbook; for more information please visit: http://www.itl.nist.gov/div898/handbook/ .
The function levene(y, x, code, nbins) returns the (scalar) p-value.
The y series is the variable you want to test for homogeneity. The x series gives the basis for dividing the y values into groups. The third argument is an integer code for the variant of the test:
case 1: The mean is used as the measure of central tendency
case 2: The median is used
case 3: The "10 percent trimmed mean" is used, meaning the mean calculated from a sample restricted to the range from the 0.05 quantile to the 0.95 quantile
If this argument is omitted, the median is used.
The final argument gives the number of groups or bins to create. If it is omitted or given as zero a group is created for each distinct value of x, otherwise the y values are grouped corresponding to nbins equally sized ranges for the x series. Note that each group must contain at least two y values, and considerably more values per group are needed if the trimmed mean option is selected.
For more information about this function or if you want to give me some advice or improvement, please do send me an e-mail to ... (Note by Sven in July 2017: The email address given by Marcos Larios Santa Rosa isn't working anymore. Marcos if you read this, please get back to the gretl team and update your contact information. Thanks.)
```
if nbins == 0
matrix xval = values(x)
k = rows(xval)
else
k = nbins
endif
if k < 2
funcerr "You need to set a minimum of 2 categories"
endif
if nbins > 0
x_min = min(x)
binsize = (max(x) - x_min) / k
endif
matrix Ni = zeros(k, 1)
matrix Zbar = zeros(k, 1)
scalar Den = 0
matrix Zi
N = $nobs
loop i=1..k --quiet
if nbins > 0
hi = x_min + i*binsize
lo = hi - binsize
series cond = (i==1)? (x <= hi && x >= lo) : (x <= hi && x > lo)
smpl cond --restrict
else
smpl (x == xval[i]) --restrict
endif
n = $nobs
Ni[i] = n
if code == 1
cent_y = mean(y)
elif code == 2
cent_y = median(y)
else
cent_y = trimmed_mean(y, 10)
endif
Zi = abs(y - cent_y)
Zbar[i] = meanc(Zi)
loop j=1..n --quiet
Den += (Zi[j] - Zbar[i])^2
endloop
smpl --full
endloop
matrix W = Ni/N
scalar Gmean = sumc(W .* Zbar)
scalar Num = sumc(Ni .* (Zbar - Gmean).^2)
dfn = k - 1
dfd = N - k
L = (dfd/dfn) * (Num / Den)
cv95 = critical(F, dfn, dfd, 0.05)
cv99 = critical(F, dfn, dfd, 0.01)
pv = pvalue(F, dfn, dfd, L)
if code == 1
cstr = "Mean"
elif code == 2
cstr = "Median"
else
cstr = "10% trimmed mean"
endif
printf "\nLEVENE TEST FOR HOMOGENEITY OF VARIANCE\n"
printf "NULL HYPOTHESIS: VARIANCES ARE EQUAL, S(1)=S(2)=...=S(k)\n\n"
printf "TEST RESULTS using %s\n", cstr
printf " Degrees of Freedom Numerator \t= %10.6f\n", dfn
printf " Degrees of Freedom Denominator \t= %10.6f\n", dfd
printf " Levene's F statistic \t\t= %9.6f\n", L
printf " Critical Value 95%% \t\t\t= %9.6f\n", cv95
printf " Critical Value 99%% \t\t\t= %9.6f\n", cv99
printf " P-Value \t\t\t\t= %10.6f\n", pv
return pv
```

```
# calculate trimmed mean
scalar a = trimpc / 200.0
scalar ymin = quantile(y, a)
scalar ymax = quantile(y, 1-a)
if (missing(ymin) || missing(ymax))
funcerr "Couldn't find the requested quantile"
endif
smpl (y >= ymin && y <= ymax) --restrict
return mean(y)
```

include levene.gfn
# use NIST data
open gear.gdt
# Is the y homogeneous in variance with respect to
# the level of x?
levene(y, x)
# Now try using the trimmed mean (giving 3 for the third
# argument), and using 4 bins for x (4 in the final argument).
levene(y, x, 3, 4)