Announcement

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

  • Unexpected behavior (suspected bug) when using Mata views with temporary variables created by fvrevar

    I encountered a very strange behavior when I use Mata views on temporary variables created by fvrevar. I tried to keep the following example as simple as possible:
    Code:
    clear all
    webuse hsng2
    
    mata:
    void mydemean(string scalar varlist, string scalar newvars, string scalar touse) {
        real matrix X, Xn
    
        st_view(X, ., varlist, touse)
        st_view(Xn, ., newvars, touse)
        Xn[., .] = X :- mean(X)
    
        X :- mean(X)
        Xn
    }
    end
    
    tempvar touse
    gen byte `touse' = 0
    replace `touse' = 1 in 34/37
    loc varlist "1b.region 2.region 3.region 4.region"
    foreach var in `varlist' {
        fvrevar `var'
        loc var "`r(varlist)'"
        qui replace `var' = .
        loc newvars "`newvars' `var'"
    }
    mata: mydemean("`varlist'", "`newvars'", "`touse'")
    The two Mata matrices displayed by running this code should be identical but they are not:
    Code:
              1      2      3      4
        +-----------------------------+
      1 |     0   -.25   -.25   -.25  |
      2 |     0    .75   -.25   -.25  |
      3 |     0   -.25    .75   -.25  |
      4 |     0   -.25   -.25    .75  |
        +-----------------------------+
    [symmetric]
           1   2   3   4
        +-----------------+
      1 |  0              |
      2 |  0   0          |
      3 |  0   0   0      |
      4 |  0   0   0   0  |
        +-----------------+
    I do not understand why the second matrix is full of zeros. This does not make any sense to me. Does anybody have an idea where I am going wrong? Or is this indeed some weird bug?

    I get the same results in all versions from Stata 12 until the most recent update of Stata 16.
    Last edited by Sebastian Kripfganz; 02 Oct 2020, 12:15. Reason: Information about Stata versions added.
    https://twitter.com/Kripfganz

  • #2
    Incorrect answer withdrawn.
    Last edited by William Lisowski; 02 Oct 2020, 12:43.

    Comment


    • #3
      The temporary variables created by fvrevar are created as byte variables. The output of help mf_st_view tells us

      Cautions when using views 2: Assignment

      The ability to assign to a view and so change the underlying data can be either convenient or dangerous, depending on your goals. When making such assignments, there are two things you need be aware of.

      The first is more of a Stata issue than it is a Mata issue. Assignment does not cause promotion. Coding

      V[1,2] = 4059.125

      might store 4059.125 in the first observation of the second variable of the view. Or, if that second variable is an int, what will be stored is 4059, or if it is a byte, what will be stored is missing.
      Adding
      Code:
          qui recast float `var'
      to your loop will solve this problem

      Comment


      • #4
        Terrific. Thanks a ton! That's why I love Statalist.
        https://twitter.com/Kripfganz

        Comment


        • #5
          Maybe a small comment on the code and the solution. fvrevar creates temporary factor variables as byte, but if time series operators used those will be double (or float, depending on the setting).

          Comment

          Working...
          X