Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Choosing the minimum from several scalars (Information criteria for model with forvalues)

    Hello.
    How can I choose the min value between several scalars?
    I'm trying to select the best model based on the information criteria. For this, I do a forvalue and I'm asking STATA to store the values of the BIC for each model. Then I want that STATA show me which one has the lowest BIC. I have found some examples trying to do similar things using MATA code. I'm not familiar with MATA. I tryed to replicate the example, but didn't work for me.
    So far, this is what I got and I don't know how to continue.


    forvalues i = 1/5 {
    forvalues j =1/5 {
    qui reg PIB_var L(1/`i').PIB_var L(1/`j').HH_total_var
    qui estat ic
    mat stats = r(S)
    scalar BIC`i'_`j' = el(stats, 1, 6)
    qui actest, lags(`i') small robus
    scalar AC`i'_`j' = r(p)
    }
    }
    scalar list

    I want STATA to show me the minimum BIC and tell me to which model does it belong. Same for the autocorrelation test.
    Can anyone help me please?

    Thanks!!

  • #2
    An easy if inelegant way is to write another pair of loops to search through your answers. Below is some untested code that might do the work. A more elegant way might be to use postfile to store your results in a Stata dataset that you can then use the usual commands to find the minimum.

    Code:
    scalar minBIC = 9999999 // some large number
    forvalues i = 1/5 {
    forvalues j =1/5 {
    if BIC`i'_`j' < minBIC {
    scalar minBIC = BIC`i'_`j'
    local minBICij "`i' `j'"
    }
    }
    }
    display minBIC
    display `minBICij'

    Comment


    • #3
      Just as a quick note, "min()" accepts scalars. E.g.

      Code:
      . scalar A = 5
      . scalar B = 3
      . di min(A,B)
      3
      Displaying which model it corresponds to is a bit more complicated, I don't know of an "argmin" function. A loop could do it I suppose.

      Comment


      • #4
        As a footnote to William's helpful code note that a Stataish initialization could be

        Code:
         
         scalar minBIC = .

        Comment


        • #5
          Thank you all very much. This has been very usefull.

          Comment

          Working...
          X