Announcement

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

  • #16
    If you look at the last paragraph my previous reply in #14, you will see that this is exactly what I was trying to explain. In the code
    Code:
    gen byte max_brake = (tot_brake > 0) if !missing(intervention)
    the part (tot_brake > 0) is a logical evaluation, which returns true or false -- respectively, 1 or 0.

    Your code

    Code:
    gen max_brake = 0
    replace max_brake = 1 if tot_brake > 0 if !missing(intervention)
    is different for the following reasons:
    • the first line would produce a value 0 for all observations, including where intervention is missing. I think you want missing values when intervention is missing.
    • the second line is illegal; you cannot have two ifs. If you changed it to
      Code:
      replace max_brake = 1 if tot_brake > 0 & !missing(intervention)
      you would get mostly what I did, except what I said above, i.e. you would still have zeros in observations where intervention is missing.

    Comment


    • #17
      This thread may be converging. Here I bring together

      1. Hemanshu Kumar's results max_brake max_oil from #11 and #14 Hemanshu's logic is that if the cumulative sum is positive then a value of 1 must have appeared in the data before or at present.

      2. A more direct method based on the identity for records that are maxima

      new record = max(old record, new value)

      as written up in Stata terms in this 2006 FAQ https://www.stata.com/support/faqs/d...m-of-sequence/

      with results MAX_brake MAX_oil

      3. using rangestat from SSC as already explained in #6 -- and as corrected in #12 as it now seems clear that Denise's "preceding" was meant to include the current value. -- with results brake_max oil_max

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input int intervention byte(vehicleno episodeno brake oil max_brake max_oil)
        . 1 1 1 0 . .
      110 1 2 1 0 1 0
        . 2 1 0 1 . .
        . 2 2 0 1 . .
      112 2 3 1 1 1 1
        . 3 1 0 1 . .
      113 3 2 0 1 0 1
      114 4 1 1 1 1 1
        . 4 2 1 0 . .
        . 4 3 1 0 . .
      115 4 4 1 0 1 1
      end
      
      bysort vehicleno (episodeno) : gen MAX_brake = brake[1]
      by vehicleno : gen MAX_oil = oil[1]
      by vehicleno : replace MAX_brake = max(brake, MAX_brake[_n-1]) if _n > 1
      by vehicleno : replace MAX_oil = max(oil, MAX_oil[_n-1]) if _n > 1
      
      rangestat (max) brake oil, int(episodeno . 0) by(vehicleno)
      
      list intervention max_brake-oil_max if intervention < ., sepby(vehicleno)
      
           +-------------------------------------------------------------------------+
           | interv~n   max_br~e   max_oil   MAX_br~e   MAX_oil   brake_~x   oil_max |
           |-------------------------------------------------------------------------|
        2. |      110          1         0          1         0          1         0 |
           |-------------------------------------------------------------------------|
        5. |      112          1         1          1         1          1         1 |
           |-------------------------------------------------------------------------|
        7. |      113          0         1          0         1          0         1 |
           |-------------------------------------------------------------------------|
        8. |      114          1         1          1         1          1         1 |
       11. |      115          1         1          1         1          1         1 |
           +-------------------------------------------------------------------------+
      You make your choice according to taste. rangestat gives a one-line solution but people often prefer to use only official commands, which I tend to argue too in other circumstances.
      Last edited by Nick Cox; 09 Oct 2022, 05:37.

      Comment

      Working...
      X