Announcement

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

  • FVREVAR doesn't work with IF

    Hi everyone,
    I am using fvrevar in my program in combination with if `touse' but it doesn't seem to work, causing a bug in my program.
    I had to use keep if `touse' to avoid this bug, any other solution is appreciated.

    Here is the code for illustration:
    Code:
    sysuse auto, replace
    gen t=_n
    tsset t
    local varlist price l.price
    
    sum `varlist'
    
    fvrevar `varlist' if t>=3        // "if t>=3" did not work
    sum `r(varlist)'
    Code:
    . local varlist price l.price
    
    . 
    . sum `varlist'
    
        Variable |        Obs        Mean    Std. dev.       Min        Max
    -------------+---------------------------------------------------------
           price |
             --. |         74    6165.257    2949.496       3291      15906
             L1. |         73    6085.397    2888.228       3291      15906
    
    . 
    . fvrevar `varlist' if t>=3               // "if t>=3" did not work
    
    . sum `r(varlist)'
    
        Variable |        Obs        Mean    Std. dev.       Min        Max
    -------------+---------------------------------------------------------
           price |         74    6165.257    2949.496       3291      15906
        __000000 |         73    6085.397    2888.228       3291      15906
    Manh Hoang-Ba,
    Facebook,
    Eureka! Uni - YouTube,
    ManhHB94 (Manh Hoang Ba),
    Hoàng Bá Mạnh – Kinh tế lượng: Lý thuyết và ứng dụng

  • #2
    The command -fvrevar if t >= 3- doesn't make any sense in terms of what -fvrevar- does, so it's not surprising that it doesn't work. It would, of course, be better if it just exited with an error message.

    The command -fvrevar- creates new variables that correspond to factor variables, and you can then later use those variables in commands that don't support factor-variable notation. But it is not possible to create a variable for only some observations in a data set. A variable either exists in all observations, or it does not exist at all. I suspect what you intended is that the variables created should have missing values in the observations that don't satisfy the -if- condition. Here's how you could do that:

    Code:
    fvrevar `varlist'
    foreach v of varlist `r(varlist)' {
        replace `v' = . if t < 3
    }
    By the way, if you look at the help file or PDF documentation for -fvrevar- you will see that its syntax does not allow -if- or -in- conditions, as is consistent with the semantics of what the command does.

    Comment


    • #3
      Actually, at least in Stata 19.5 (StataNow), both the help file and the PDF documentation for fvrevar claim that the syntax permits both [if] and [in]. If indeed the command behaves as above, I believe that ether StataCorp should amend the documentation to reflect this, or alter it to behave as Clyde Schechter outlined in #2.

      Comment


      • #4
        Hemanshu Kumar is right. I had tested the behavior and documentation of -fvrevar- in version 17 when I replied to #1. But, seeing #3, I tried it again, this time in version 19.5, and it does, indeed, allow -if- and -in- conditions, and produces missing values for the newly created variables in observations not satisfying those conditions. So it is apparently a version specific issue that has been changed recently.

        Comment


        • #5
          fvrevar applied to variables with time-series operators behaves exactly like tsrevar.

          From the documentation
          fvrevar creates a variable list that includes equivalent, temporary variables in place of the factor variables, interactions, or time-series-operated variables in varlist.
          This means the generated variables are intended to be replacements for the original variable list specification for the entire dataset.

          The optional if/in conditions are there to ensure that the automatically determined factor variable levels are respected like they are in the original specification.

          Here is an example showing this with the auto data.
          Code:
          . sysuse auto
          (1978 automobile data)
          
          . summarize bn.rep78 if foreign
          
              Variable |        Obs        Mean    Std. dev.       Min        Max
          -------------+---------------------------------------------------------
                 rep78 |
                    3  |         21    .1428571    .3585686          0          1
                    4  |         21    .4285714    .5070926          0          1
                    5  |         21    .4285714    .5070926          0          1
          
          . summarize bni(3/5).rep78
          
              Variable |        Obs        Mean    Std. dev.       Min        Max
          -------------+---------------------------------------------------------
                 rep78 |
                    3  |         69    .4347826    .4993602          0          1
                    4  |         69    .2608696    .4423259          0          1
                    5  |         69    .1594203    .3687494          0          1
          
          . fvrevar bn.rep78 if foreign, stub(xx_)
          
          . char list
            _dta[note1]:                From Consumer Reports with permission
            _dta[note0]:                1
            _dta[_lang_list]:           default
            _dta[_lang_c]:              default
            xx_1[fvrevar]:              3bn.rep78
            xx_2[fvrevar]:              4.rep78
            xx_3[fvrevar]:              5.rep78
          
          . summarize xx_* if foreign
          
              Variable |        Obs        Mean    Std. dev.       Min        Max
          -------------+---------------------------------------------------------
                  xx_1 |         21    .1428571    .3585686          0          1
                  xx_2 |         21    .4285714    .5070926          0          1
                  xx_3 |         21    .4285714    .5070926          0          1
          
          . summarize xx_*
          
              Variable |        Obs        Mean    Std. dev.       Min        Max
          -------------+---------------------------------------------------------
                  xx_1 |         69    .4347826    .4993602          0          1
                  xx_2 |         69    .2608696    .4423259          0          1
                  xx_3 |         69    .1594203    .3687494          0          1
          Last edited by Jeff Pitblado (StataCorp); 09 Nov 2025, 16:27.

          Comment


          • #6
            Thanks to Clyde Schechter and Jeff Pitblado (StataCorp) for suggesting and clarifying the mechanism of action of fvrevar.
            Manh Hoang-Ba,
            Facebook,
            Eureka! Uni - YouTube,
            ManhHB94 (Manh Hoang Ba),
            Hoàng Bá Mạnh – Kinh tế lượng: Lý thuyết và ứng dụng

            Comment

            Working...
            X