On Mon, Jun 20, 2022 at 11:11 AM Cottrell, Allin <cottrell(a)wfu.edu> wrote:
One way to allow access to things inside a session file without
terminating the current session might be to implement opening such a
file as a bundle. Saved models would appear as bundles inside the main
bundle. That approach could work OK in a loop.
Perhaps Fred could tell us if the following is useful (it's now in git
and snapshots, but not yet documented).
You can open a gretl session file as a bundle, via the syntax
open foo.gretl --bundle=bname
where "bname" is the name of the bundle to be created. At present the
only relevant content of the bundle is an array named "models",
holding the models defined in the session. In what I take to be the
spirit of Fred's usage, here's a trivial example. A first script
simulates production of a number of session files by students.
Notionally, this is in response to a directive along the lines of:
"Open Ramanathan data4-1 in gretl and estimate two models via OLS. In
each case the dependent variable is price. In the first case the sole
regressor is sqft and in the second the two regressors are sqft and
bedrms. A y-intercept should be included in both cases."
<hansl>
/* construct a presumed correct session file, plus a few
correct or incorrect "student" session files
*/
# authorized version
open data4-1
m1 <- ols 1 0 2
m2 <- ols 1 0 2 3
store authorized.gretl
# student 1 (all correct)
clear
open data4-1
m1 <- ols 1 0 2
m2 <- ols 1 0 2 3
store student1.gretl
# student 2 (incorrect: const missing)
clear
open data4-1
m1 <- ols 1 2
m2 <- ols 1 2 3
store student2.gretl
# student 3 (incorrect; model 2 missing)
clear
open data4-1
m1 <- ols 1 0 2
store student3.gretl
</hansl>
A second script opens the session files in sequence and checks the
"students'" results:
<hansl>
set verbose off
clear
# the presumed correct results
open authorized.gretl --bundle="b0"
scalar nm0 = nelem(b0.models)
# number of session files to check
scalar n_students = 3
loop i=1..n_students
# construct name of session file to read
sname = sprintf("student%d.gretl", i)
# construct name of bundle to write
bname = sprintf("b%d", i)
# open the session file as bundle
open @sname --bundle="@bname"
# get the array of models from bundle
bundles ma = @bname.models
# and determine the number of models
scalar nm = nelem(ma)
string s = (nm == nm0)? "OK" : "wrong"
printf "student %d, nmodels=%d, %s\n", i, nm, s
# compare models in terms of number/values of coeffs
scalar nmi = nm < nm0 ? nm : nm0
loop j=1..nmi
scalar nc = b0.models[j].ncoeff
if ma[j].ncoeff == nc
matrix c0 = b0.models[j].coeff
matrix ci = ma[j].coeff
printf " model %d: ncoeff correct\n", j
printf " max diff = %g\n", max(diff(abs(c0-ci)))
else
printf " model %d: ncoeff wrong\n", j
endif
endloop
endloop
</hansl>
I get the following results after running the two script above:
<output>
student 1, nmodels=2, OK
model 1: ncoeff correct
max diff = 0
model 2: ncoeff correct
max diff = 0
student 2, nmodels=2, OK
model 1: ncoeff wrong
model 2: ncoeff wrong
student 3, nmodels=1, wrong
model 1: ncoeff correct
max diff = 0
</output>
Allin