[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Odd behaviour in mata -st_varindex()- or -st_addvar()- ... a possible bug?

From   "Glenn Goldsmith" <>
To   <>
Subject   st: Odd behaviour in mata -st_varindex()- or -st_addvar()- ... a possible bug?
Date   Sun, 8 Mar 2009 19:29:16 -0000

Dear list,

I think I may have found a problem (dare I call it a bug?) in the way mata's
-st_varindex()- and -st_addvar()- commands interact with the e(sample)
function returned by Stata regression commands. 

The code below demonstrates the problem. The following is my guess as to
what is going on:

  1. Following a regression command, Stata creates a variable called "(e)",
which is invisible in Stata, but is visible in Mata. 

  2. If -st_addvar()- is called when the "(e)" variable is in memory, it
simply adds what it is asked to add after the "(e)", and returns the indices

  3. If -st_varindex()- is called when the "(e)" variable is in memory, it
reorders the variables, such that "(e)" is moved to the last variable
position, and then returns the index of the requested variable.

The upshot of this is that, with "(e)" in memory, if you add variables and
obtain their indices using -st_addvar()-, these indices are invalidated by a
subsequent call to -st_varindex(). This isn't actually much of a problem in
the toy code below, because -st_varindex()- has just given you the right
indices anyway. But it's more or an issue if you use -st_varindex()- to
obtain the index of an unrelated variable, and don't realize that it's
reordering the variables, which is how the problem initially arose for me. 

Workarounds are easy enough, once you're aware of the issue, and perhaps
there is a reason why -st_varindex()- needs to reorder things. But if so,
shouldn't -st_addvar()- do the same, so that this sort of inconsistency
doesn't arise?

************ Begin Code ************
sysuse auto
qui reg price weight mpg
************ End code **************

Best wishes,


*   For searches and help try:

© Copyright 1996–2021 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index