Announcement

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

  • Model that switches between exponential equations based on state variable

    Hi Statalists,

    I have a problem that is more mathematical than Stata-related. I know this is a long shot, but I have to try.
    • I am calculating a score (HS) which runs on a scale from 0% to 100%.
    • HS should increase exponentially while state == 1.
    • HS should decrease exponentially while state == 0
    • HS should be 100% if state == 1, 91 t in a row. In other words if state == 1 for t1 to t91 (meaning the sum of state = 91), HS should be 100%.
    I have the equation which works if state == 1 for t1 to t91, and I have the equation which decreases exponentially from 100% to 0% while state == 0 for t1 to t91.

    Just for context, t = one day, and the 91 represents 3 months (365 / 12 * 3). The state refers to an occurrence that did (1) or did not (0) happen that day. If the occurrence happens 91 days in a row, HS must be 100%.

    I don't know how to explain my problem better - which is probably why I haven't been successful finding a solution via Google.

    Code if state == 1 for t1 to t91:
    Code:
    clear
    insobs 91
    egen t = seq()
    gen state = 1
    gen hs = (state*t^2)/(91^2)*100
    twoway (line hs t)
    Code if state == 0 for t1 to t91:
    Code:
    clear
    insobs 91
    egen t = seq()
    gen state = 0
    gen hs = (91^2-(-t-(1))^2)/(91^2)*100
    twoway (line hs t)
    In the end, the data, when plotted, should look something like this, assuming a period where state ==1, followed by a period where state ==0, followed by a period where state == 1 again:
    Click image for larger version

Name:	Screenshot 2021-12-16 at 21.51.26.png
Views:	1
Size:	22.4 KB
ID:	1641281

    Thank you so much in advance.

  • #2
    Would this serve your purposes?

    Code:
    clear*
    set obs 91
    
    gen int t = _n
    sort t
    
    //  SAMPLE CASES
    gen state1 = 1 // SHOULD RISE TO 100%
    
    gen state2 = (t < 60 | t > 80) // SHOULD RISE TO PEAK AT 60, FALL, THEN RISE AFTER 80
    
    gen state3 = 0
    
    forvalues i = 1/3 {
        gen cum_state`i' = (sum(state`i' == 1) - sum(state`i' == 0))/t
        gen hs`i' = max(100*(cum_state`i'*t^2)/(91^2), 0)
    }
    
    graph twoway line hs* t, sort
    By the way, your code suggests that you do not intend to have exponential evolution of hs, as your code uses a quadratic function. In fact, you can't have exponential change because an exponential can never be zero.

    Comment


    • #3
      Thanks a lot, Clyde.

      That serves my purpose.

      Thank you for pointing out my equation is quadratic and not exponential. I will look into that later.

      Comment

      Working...
      X