Announcement

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

  • collect with named expressions: _r_b not found error

    Hi,
    I am having issues with the ability to collect named expressions.
    If I try to collect an expression such as -1*_r_b, Stata only produces an error, "_r_b not found". Yet it can find _r_b alone. Various additions of () have not helped.
    It seems that it should work: the help for -collect get- states that named expressions may include result identifiers such as _r_b.

    Here is code to reproduce in a toy example.
    I use version 17.0

    Code:
    clear all
    
    //(I am forced to work on a remote server with no internet access, hence cannot easily use the various web data sets for example and will create a toy set first).
    set obs 1000
    set seed 1000
    gen y = runiform() > 0.33
    gen x1 = rnormal()
    gen x2 = rnormal(2)
    
    cap collect drop c1
    collect create c1
    collect _r_b (copyb = _r_b) : logit y x1 x2         // this works:
    collect layout (colname)(result[_r_b copyb])
    
    cap collect drop c2
    collect create c2
    collect _r_b (copyb = _r_b) (invb= -1*_r_b) : logit y x1 x2   //error: _r_b not found
    collect layout (colname)(result[_r_b copyb invb ])
    Although I could use e(b) here instead of _r_b the pre-made 'result identifiers' such as _r_ci are very useful, and it would be great to be able to easily apply expressions to them using the -collect- named expression feature.

    Perhaps I am missing something in the syntax, or maybe only the 'result identifiers' such as e(b) and not _r_b are available for expressions with calculations?

    Thanks,
    Scott


  • #2
    _r_b and friends are system variables like _b (in Stata type help system variables) and are only allowed in scalar expressions in Stata. They are not allowed in matrix expressions in Stata.

    In collect get, they are also identifiers for estimation results; convenient shortcuts for the coefficients, standard errors, p-values, ..., to be specified as automatic results levels for your layout.

    The documentation for collect get seems to imply that _r_b can be used in named expressions, which is only partially true. They can be used in scalar expressions, where you must specify an e(b) stripe element (column name), like ex1=(exp(_r_b[x1])). They cannot be used in matrix expressions. In addition, _r_ci is not supported in any expressions.

    There is a special case where collect get allows you to name your own copy of _r_b (and friends).

    You can create your own custom results using matrix expressions involving rows of matrix r(table).
    Here is an example, using the above data setup.
    Code:
    . collect create c3, replace
    (current collection is c3)
    
    . collect minuz=(-r(table)["z",1...]) : quietly logit y x1 x2
    
    . collect layout (colname) (result[_r_b _r_z minuz])
    
    Collection: c3
          Rows: colname
       Columns: result[_r_b _r_z minuz]
       Table 1: 3 x 3
    
    ---------------------------------------
              | Coefficient     z     minuz
    ----------+----------------------------
    x1        |    -.035697 -0.52  .5165254
    x2        |     .081788  1.22 -1.224363
    Intercept |    .5252212  3.55 -3.546533
    ---------------------------------------

    Comment

    Working...
    X