Announcement

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

  • Odd Stata/Mata bug when using xtreg, fe in a program together with Mata views under version control

    Under Stata 15.1, I experienced an odd behavior in a program that I have written. I first created a Mata view on a variable in the data set and subsequently run a fixed-effects regression with xtreg. The effect was that the content of the Mata view has changed although the data set itself remained unchanged.
    Code:
    clear all
    webuse abdata
    
    program define bugtest
        version 13
        syntax varlist [if]
        marksample touse
        gettoken depvar indepvars : varlist
    
        mata: st_view(y = ., ., "`depvar'", "`touse'")
        mata: y
        quietly xtreg `depvar' `indepvars', fe
        mata: y
    end
    
    bugtest n w if id <= 2
    The output is:
    Code:
                      1
         +---------------+
       1 |  1.617604494  |
       2 |  1.722766638  |
       3 |  1.612433434  |
       4 |  1.550748944  |
       5 |  1.409278154  |
       6 |   1.15246892  |
       7 |  1.077048182  |
       8 |    4.2671628  |
       9 |  4.257638931  |
      10 |    4.2615242  |
      11 |  4.277096748  |
      12 |  4.299853802  |
      13 |  4.282468796  |
      14 |  4.227096558  |
         +---------------+
                      1
         +---------------+
       1 |   95.7071991  |
       2 |  97.35690308  |
       3 |  99.60829926  |
       4 |  100.5501022  |
       5 |  99.55809784  |
       6 |  98.61509705  |
       7 |   100.030098  |
       8 |   95.7071991  |
       9 |  97.35690308  |
      10 |  99.60829926  |
      11 |  100.5501022  |
      12 |  99.55809784  |
      13 |  98.61509705  |
      14 |   100.030098  |
         +---------------+
    The second call to the Mata view gave me the content of the variable indoutpt instead of the variable n, i.e. the variable index was shifted by -1.

    This problem does not occur if I use the random-effects estimator (i.e. xtreg without option fe) and it also does not occur if I replace version 13 by version 14 (or version 15) in the first line of the program. Interestingly, the problem does not occur either under Stata 14 even if I keep the version 13 statement.

    Can anyone replicate this problem?
    Last edited by Sebastian Kripfganz; 14 Mar 2019, 08:48. Reason: Edit: "webuse abdata" added
    https://twitter.com/Kripfganz

  • #2
    Sebastian Kripfganz --

    I have been bitten by this bug before - while I haven't replicated the exact problem in your data, it might be because a fixed effects estimator requires grouping the data in some way, which in turn involves sorting the variables. The random effects estimator doesn't involve a prior sort. So, is your data sorted according to groups before running the command?

    Matthew J. Baker

    Comment


    • #3
      The data is already sorted by groups (xtset) when you load it from the web (webuse abdata). The problem is not that the Mata view points to different observations of the same variable but to the same observation numbers of a completely different variable. That should not happen even if there is any sorting. The present bug must have something to do with Mata views and variable indices.

      I wonder whether it is related to a bug fix in the 20feb2019 update (quoted from help whatsnew), although I cannot see a direct link to the problem above:
      4. ereturn post and ereturn repost with option esample() failed to update variable indices in the existing Mata views. For Mata views that were created using variables with an index greater than that of the variable specified in option esample(), those columns were referencing variables with a higher index than they should have. This has been fixed.
      https://twitter.com/Kripfganz

      Comment

      Working...
      X