On Thu, 11 Oct 2007, Marcos Larios wrote:
Hi all, I'd been usng Gretl for over 3 years now and I need it
to do two tests: Bartlett's Test and Levene's test for
Homogeanity of Variance, to use them in the residuals of some
models...
The thing is that when I do the "genr sample =
(POPM<=maxband&POPM>=lowband)" code in Gretl's console it does
it allright but when I do it as a function "?
bartlett(RESID01,POPM,5)" it does nothing, it only shows the
printf output, but it doesn't generates the sample series and
much less changes the sample size of the workfile.
The design of user-defined functions in gretl is intended to
ensure that such functions don't have any surprising side-effects
-- that is, they are "safe to use" for people who don't know
exactly what goes on inside them.
The implications of this design decision are spelled out in
Chapter 10 of the Gretl User's Guide. Two points are particularly
relevant here:
1) Any variables defined inside a function are thrown away when
the function exits, unless they are made available as return
values, and those return values are assigned by the caller of the
function.
2) You can change the sample all you like inside a function, but
when the function exits the sample reverts to what it was before
the function was called.
For the purposes of a function that implements Bartlett's test, I
don't think these things should be problematic. Within the
function, you can reset the sample repeatedly and calculate
variances. All you need return is a test statistic and/or p-value
-- or just print the test result.
In case it's helpful, I'm putting below a script that solves a
simpler but related problem, namely printing an F-test for the
hypothesis that the variance is the same, for some given variable,
in the first and second halves of the sample. A couple of
comments follow.
<gretlscript>
function vfun (series y)
set messages on
set echo on
scalar n2 = $nobs/2
smpl ; -n2
n1 = $nobs - 1
v1 = var(y)
smpl --full
smpl +n2 ;
n2 = $nobs - 1
v2 = var(y)
printf "v1 = %g, v2 = %g\n", v1, v2
Ft = (v1 > v2)? v1/v2 : v2/v1
pval = pvalue(F, n1, n2, Ft)
printf "F(%d,%d) = %g, p-value %g\n", n1, n2, Ft, pval
return scalar pval
end function
open data9-7
scalar pv = vfun(QNC)
</gretlscript>
Comments:
(1) You should use
set echo on
set messages on
inside your function when debugging. Then remove these lines when
you're sure things are working right.
(2) Unfortunately there's an annoying bug in smpl, whereby a "-"
indicating retard-the-sample-end-or-start gets confused with the
"-" that precedes an option flag. The form I used above, "-n2",
happens to be a lucky string that doesn't trigger the problem.
(I think the solution to this is to insist that the options to
smpl be given in their full form, with two dashes. I'll fix that
for gretl 1.6.6.)
Allin Cottrell