Hi all. I've run into some interesting and (I think) undocumented
behavior by Stata and Mata with respect to tsrevar and st_tsrevar.
1. Mata's st_tsrevar doesn't require sorted data
This is surprising to me. Here is an example. First, take a tsset
dataset, create a tsset temporary variable, then scramble the sort:
webuse tsrevarex, clear
tsset t
tsrevar l.gnp
sort cpi
tsrevar l.gnp
The last line, in which tsrevar is called again, generates an error:
. tsrevar l.gnp
not sorted
r(5);
But Mata is more forgiving. Set up the dataset as above, but after
-sort cpi- go into Mata and use st_tsrevar:
webuse tsrevarex, clear
tsset t
tsrevar l.gnp
sort cpi
mata: stokens = st_tsrevar("l.gnp")
mata: stokens
The call to st_tsrevar doesn't generate an error. Instead, the output
is
. mata: stokens = st_tsrevar("l.gnp")
. mata: stokens
6
where 6 is the Stata variable index of the temporary variable created by
the original Stata call to tsrevar. However, if st_tsrevar is asked to
create a new temporary variable, e.g.,
mata: stokens = st_tsrevar("l.cpi")
the output is is an error:
. mata: stokens = st_tsrevar("l.cpi")
not sorted
st_tsrevar(): 3598 Stata returned error
<istmt>: - function returned error
r(3598);
I've confirmed this behavior in both Stata 10 and Stata 11.
My immediate reaction is that this is rather handy for Mata programming,
if used carefully. I can run tsrevar in Stata at the start of my code,
change the sort later if I need to, and when I go into Mata the correct
tsrevar-ed temporary variables will still exist if I need them.
I can't find this documented anywhere. Does anyone know if this is by
design, so that I can count on this behavior when I program in Mata?
And a related question - should tsrevar behave the same way?
2. Stata's tsrevar reuses temporary variables created by tsrevar under
a previous sort order
This is surprising, at least to me. Here is what I mean. Load the
dataset, tsset it, and create the tsrevar-ed temporary variable:
webuse tsrevarex, clear
tsset t
tsrevar l.gnp
local tsrv "`r(varlist)'"
di "`tsrv'"
list t gnp l.gnp `tsrv' in 9/11
The output of the last two commands is:
. di "`tsrv'"
__000000
. list t gnp l.gnp `tsrv' in 9/11
+-----------------------------+
| t gnp L.gnp __000000 |
|-----------------------------|
9. | 9 122 102 102 |
10. | 10 134 122 122 |
11. | 11 110 134 134 |
+-----------------------------+
which is all fine. L.gnp and __000000 are the same.
But watch what happens if we then jumble the data by tsset-ing on a
different variable:
webuse tsrevarex, clear
tsset t
tsrevar l.gnp
local tsrv "`r(varlist)'"
di "`tsrv'"
list t gnp l.gnp `tsrv' in 9/11
sort cpi
gen t2=_n
tsset t2
tsrevar l.gnp
local tsrv "`r(varlist)'"
list t t2 gnp l.gnp `tsrv' in 9/11
list t t2 gnp l.gnp `tsrv' if t>=9 & t<=11
The output from the last two commands is:
. list t t2 gnp l.gnp `tsrv' in 9/11
+----------------------------------+
| t t2 gnp L.gnp __000000 |
|----------------------------------|
9. | 4 9 138 101 132 |
10. | 18 10 122 138 108 |
11. | 2 11 135 122 128 |
+----------------------------------+
. list t t2 gnp l.gnp `tsrv' if t>=9 & t<=11
+----------------------------------+
| t t2 gnp L.gnp __000000 |
|----------------------------------|
7. | 11 7 110 116 134 |
15. | 10 15 134 108 122 |
18. | 9 18 122 135 102 |
+----------------------------------+
Stata has reused the temporary variable name __000000, but not only
that, it has also reused the contents. Even though the data have been
re-tsset using a different variable, the call to tsrevar didn't create a
new temporary variable. The variable __000000 still has the lag of gnp
created when the data were tsset under t.
Can anybody see a logical reason for this behavior? It seems
inconsistent with what I showed above in the Mata example - if the data
are unsorted, tsrevar complains, even if the temporary variable exists.
Resort the data and now tsrevar doesn't complain, even if the sort is on
a different variable.
I admit this is pretty obscure, though - how often would we have a
dataset where we want to work with two different time orderings?
Cheers,
Mark
--
Heriot-Watt University is a Scottish charity
registered under charity number SC000278.
*
* 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/