Announcement

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

  • #16
    Clyde,

    Thanks for this we are on the same page I am after the piece of code you have written to identify the lowest aic of the different regressions. I've got this working but unfortunately my macros are not conveniently named 1-25. They are as follows: - I can't get this working can you please correct this?

    Code:
    local models `idf' `iidf' `iiidf' `ivdf' `vdf' `vidf' `viidf'
            foreach x of local models{
            xtreg `x', fe
            estat ic
            matrix S =r(S)
            scalar aic = S [1, 5]
            if aic <`lowest_aic'{
                local result `x'
                scalar lowest_aic = aic
            }
        }

    Comment


    • #17
      So you've got the names of the models and the references to the variables in those models reversed in your code.

      When you write
      Code:
      local models `idf' `iidf' `iiidf' `ivdf' `vdf' `viidf'
      Stata looks at local macro idf to see what it contains: which presumably is some list of variables, and then it places that list of variables in local macro models. THen it does the same for iidf and the others. So the above command produces not a list of models but a list of all the variables appearing in those models.

      What you want is:
      Code:
      local models idf iidf iiidf ivdf vdf vidf viidf
      scalar lowest_aic = c(maxdouble)
      foreach x of local models{
          xtreg ``x'', fe
          estat ic
          matrix S =r(S)
          scalar aic = S [1, 5]
          if aic < lowest_aic {
              local result `x'
              scalar lowest_aic = aic
          }
      }
      Changes italicized and in red.

      Notice also the use of nested macro quotes ``x'' in the -xtreg- command. That's because `x' alone will refer to the name of a model, and -xtreg idf- will not fly. You want -xtreg `idf'-, and that corresponds to -xtreg ``x''-.

      Here's a trick you can learn that will help you with these issues. Even experienced Stata users are sometimes confused by local macros and their references. The way to know what Stata sees is to include
      Code:
      set trace on
      set tracedepth 1
      in front of a section of code that is giving you trouble and contains macros. The output when you run that will show you the commands with all of the macros replaced by what Stata expands them to. That would show you that your original definition of -local models- is not what you intended.

      Note also that I have removed the macro-dereferencing quotes from around lowest_aic in the -if- command. lowest_aic in this code is a scalar, not a local macro. So it does not need, nor tolerate, macro quotes. If you have seen Stata code where scalar names are dereferenced with `', it is because the name of the scalar is itself a local macro (typically a -tempname-).

      Added, I have left -local result `x'- alone, in the spirit of changing as few things as possible. As a result, the output when you ultimately display "`result'" is that you will see something like iiidf, not a list of variables. That is, as written, local macro result will contain the name of the model selected, not the variables in the model. If you want the latter, make it -local result ``x''-.

      One more thing: in the unlikely event that two models are tied for lowest AIC, this code will give you the first one.

      By the way, using Roman numerals is usually asking for unnecessary complications in Stata, and I think in programming in general. Certainly there is no convenient way to loop over Roman numerals. I recommend you avoid them.
      Last edited by Clyde Schechter; 05 Feb 2018, 13:54.

      Comment


      • #18
        Thanks Clyde I have got this working now I appreciate your help. I have noted your comments about roman numerals and will make sure to not use these in future. I will also use the set trace on command to help troubleshoot.

        Giles
        Last edited by Giles German; 05 Feb 2018, 14:06.

        Comment

        Working...
        X