Page 1 of 1

Why does this code not overwrite the model on each loop?

Posted: Wed Oct 28, 2015 4:20 pm
by macro
This code

Code: Select all

calendar(q) 1947 1

compute t0 = 1970:1
compute tT = 2002:4

open data "data.csv"
    data(format = cdf, org = columns) /
close data

smpl t0 tT
declare string model_name
dofor model_name = "level" "diff"
    if model_name == "level" {
        system(model = full_model)
            variables lgdpk lcdk
            lags 1 to 4
            deterministic fcm10c{1 to 4} constant
        end(system)
    }
    else {
        system(model = full_model)
            variables dlgdpk dlcdk
            lags 1 to 4
            deterministic fcm10c{1 to 4} constant
        end(system)
    }
    
    dofor dep_var = %modeldepvars(full_model)
        compute eq = %modelfind(full_model, dep_var)
        compute d = model_name + " " + %l(dep_var) + " maps to " + %l(%eqndepvar(eq)) + " with eq # " + eq
        display d
    end dofor dep_var
end dofor model_name
displays

Code: Select all

level LGDPK maps to LGDPK with eq # 1
level LCDK maps to LCDK with eq # 2
diff DLGDPK maps to LGDPK with eq # 1
diff DLCDK maps to LCDK with eq # 2
On each iteration of the outer loop ("dofor model_name = ...") it should overwrite -full_model- and then iterate over the dependent variables in this model. Instead, it doesn't overwrite the model each time and keeps pulling the dependent variables from -full_model- as it was assigned on the first iteration of the outer loop. Is this something to do with the compiler?

If I swap "level" and "diff" in the declaration of the outer loop, the output is reversed:

Code: Select all

diff DLGDPK maps to DLGDPK with eq # 1
diff DLCDK maps to DLCDK with eq # 2
level LGDPK maps to DLGDPK with eq # 1
level LCDK maps to DLCDK with eq # 2
so the error is still there.

Re: Why does this code not overwrite the model on each loop?

Posted: Wed Oct 28, 2015 7:49 pm
by TomDoan
In this, %MODELFIND returns the position in full_model (thus either 1 or 2).

compute eq = %modelfind(full_model, dep_var)

However, in %l(%eqndepvar(eq))

eq is intended (I assume) to be a handle to an equation. That will be most likely 3 or 4 for the second time through. Use %MODELEQN(full_model,eq) rather than eq instead to get the actual equation handle for the equation in position eq of full_model.

Re: Why does this code not overwrite the model on each loop?

Posted: Thu Oct 29, 2015 9:34 am
by macro
Ah, that makes sense. Thank you. This line fixes the problem:

Code: Select all

compute eq = %modeleqn(full_model, %modelfind(full_model, dep_var))