Marcos Larios Santa Rosa
0.3
2017-07-02
Bartlett's test for Homogeneity of Variance
C12
This function implements Bartlett'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 Institute of Standards and Technology's Engineering Statistics Handbook; for more information please visit: http://www.itl.nist.gov/div898/handbook/ .
The function bartlett(y, x, nbins) does not return anything, but prints output.
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 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.
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
k = nbins
scalar lo, hi
scalar x_max = max(x)
scalar x_min = min(x)
scalar binsize = (x_max - x_min) / k
else
matrix xvals = values(x)
k = rows(xvals)
endif
if k < 2
funcerr "There must be at least two groups"
endif
N = $nobs
matrix s2 = zeros(k, 1)
matrix Ni = zeros(k, 1)
scalar s2p = 0
scalar sum1 = 0
scalar sum2 = 0
scalar err = 0
# get variance and N for each group
loop i=1..k --quiet
if nbins > 0
hi = x_min + i * binsize
lo = hi - binsize
if (i == 1)
smpl (x <= hi && x>=lo) --restrict
else
smpl (x <= hi && x>lo) --restrict
endif
else
smpl (x == xvals[i]) --restrict
endif
if $nobs < 2
err = 1
break
endif
s2[i] = var(y)
Ni[i] = $nobs
s2p += (Ni[i] - 1) * s2[i]
sum1 += (Ni[i] - 1) * log(s2[i])
sum2 += 1 / (Ni[i] - 1)
smpl --full
endloop
if err
funcerr "Each group must have at least 2 observations"
endif
s2p /= (N - k)
Tnum = (N - k) * log(s2p) - sum1
Tden = 1 + (1/(3*(k-1))) * (sum2 - 1/(N - k))
T = Tnum / Tden
df = k - 1
cv95 = critical(X, df, 0.05)
cv99 = critical(X, df, 0.01)
Tpval = pvalue(X, df, T)
matrix show = seq(1, k)' ~ sqrt(s2) ~ Ni
colnames(show, "Group S(i) N(i)")
printf "BARTLETT TEST FOR HOMOGENEITY OF VARIANCES\n"
printf "NULL HYPOTHESIS: VARIANCES ARE EQUAL\n"
printf "H0: S(1)=S(2)=...=S(k)\n"
printf "\n%12.5g\n", show
printf "TEST RESULTS\n"
printf " Degrees of Freedom \t\t= %10.6f\n", df
printf " Bartlett's Test Statistic \t= %9.6f\n", T
printf " Critical Value 95%% \t\t= %9.6f\n", cv95
printf " Critical Value 99%% \t\t= %9.6f\n", cv99
printf " Chi-Square(%d) P-Value \t= %10.6f\n\n", df, Tpval
```

include bartlett.gfn
# use the NIST illustrative dataset
open gear.gdt
# Is the variance of y homogeneous with respect to x?
bartlett(y, x)
# Now try using 4 bins for x
bartlett(y, x, 4)