Announcement

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

  • Identifying time it takes to reach pre-event value

    Dear Statalist members,
    I have monthly data listing case id, month, and incidence of an event. Sometimes an event spans across two months, as in events 1 and 3 below:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(id month) float event int score
    1  1 . 100
    1  2 . 100
    1  3 1  90
    1  4 1  90
    1  5 . 110
    1  6 . 120
    1  7 . 140
    1  8 2 140
    1  9 . 150
    1 10 3 160
    1 11 3 170
    1 12 . 160
    2  1 .  90
    2  2 . 100
    2  3 1  90
    2  4 1 100
    2  5 . 110
    2  6 . 110
    2  7 . 120
    2  8 2 110
    2  9 . 100
    2 10 3 120
    2 11 3  90
    2 12 . 100
    3  1 . 100
    3  2 . 120
    3  3 1 100
    3  4 1  90
    3  5 .  80
    3  6 . 120
    3  7 . 130
    3  8 2 120
    3  9 . 130
    3 10 3 140
    3 11 3 130
    3 12 . 140
    end
    I would like to create a variable like -back- below, which indicates the number of months it takes for the value of -score- to be greater than or equal to the value recorded in the month before each event. Worth noting is the fact that sometimes the lower value only happens in the second month of an event that spans across two months (eg id == 2 & event == 3).
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(id month) float event int score byte back
    1  1 . 100 .
    1  2 . 100 .
    1  3 1  90 3
    1  4 1  90 3
    1  5 . 110 .
    1  6 . 120 .
    1  7 . 140 .
    1  8 2 140 0
    1  9 . 150 .
    1 10 3 160 0
    1 11 3 170 0
    1 12 . 160 .
    2  1 .  90 .
    2  2 . 100 .
    2  3 1  90 2
    2  4 1 100 2
    2  5 . 110 .
    2  6 . 110 .
    2  7 . 120 .
    2  8 2 110 3
    2  9 . 100 .
    2 10 3 120 3
    2 11 3  90 3
    2 12 . 100 .
    3  1 . 100 .
    3  2 . 120 .
    3  3 1 100 4
    3  4 1  90 4
    3  5 .  80 .
    3  6 . 120 .
    3  7 . 130 .
    3  8 2 120 2
    3  9 . 130 .
    3 10 3 140 0
    3 11 3 130 0
    3 12 . 140 .
    end
    Thanks in advance for any help you can provide!

  • #2
    I managed to write a clunky solution that seems to work, pasted below for reference. I would still appreciate suggestions for a more direct approach.

    Code:
    rename back want
    levelsof event, local(eventsnu)
    foreach n of local eventsnu {
        su month if event == `n', meanonly
        gen temp1 = r(min) - 1
        bys id (month) : gen temp2 = score if month == temp1
        bys id (month) : egen temp3 = mean(temp2)
        bys id (month) : egen temp3a = max(score)
        gen temp4 = score if month > temp1
        bys id event : egen temp5 = min(score) if event == `n'
        bys id (month) : gen temp5a = (sum(inrange(temp4, temp3, temp3a)) == 1 & sum(inrange(temp4[_n - 1], temp3, temp3a)) == 0)
        bys id event (month) : gen temp6 = _n
        replace temp6 = . if missing(event)
        gen temp7 = temp4
        replace temp7 = temp5 if temp5a == 1 & temp6 == 1 & event == `n'
        bys id (month) : gen temp8 = (sum(inrange(temp7, temp3, temp3a)) == 1 & sum(inrange(temp7[_n - 1], temp3, temp3a)) == 0)
        gen temp9 = month if temp8 == 1
        gen temp10 = temp9 - temp1
        bys id (month) : egen temp11 = mean(temp10)
        bys id (month) : gen temp12 = 1 if score < temp3 & event == `n'
        bys id (month) : gen temp13 = sum(temp12) if event == `n'
        bys id (month) : egen temp14 = mean(temp13) if event == `n'
        gen dura`n' = temp11 if temp14 > 0 & temp14 < . & event == `n'
        replace dura`n' = 0 if temp14 == 0
        drop temp*
    }
    egen back = rowtotal(dura*)
    drop dura*    
    replace back = . if missing(event)
    assert back == want

    Comment

    Working...
    X