Statalist


[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" <[email protected]>
To   <[email protected]>
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 ************
clear
sysuse auto
qui reg price weight mpg
mata:
st_varname(range(1,13,1)') 
st_addvar("byte","newvar")
st_varname(range(1,14,1)') 
st_varindex("newvar")
st_varname(range(1,14,1)') 
end
************ End code **************

Best wishes,

Glenn.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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