Announcement

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

  • Dynamically generate new values of variables using dyngen

    Hi,
    I am working with cohort life tables and generating the values of lx (number of people alive) at age x, dx (number of deaths) at age x based on qx (probability of death before next birthday). I am using dyngen to dynamically update values and would like help/advice to speed it up.

    Three variables: q, l, and d for 90 observations (rows).
    Initial values are as follows:
    q0 to q90 are external inputs.
    l0 or the starting value of lx is external input.
    d0 = l0*q0

    Subsequent rows l1 and d1 are calculated as:
    l1=l0-d0
    d1=l1*q1

    The process continues row by row until l90.
    I use the following Stata code (dyngen) in Stata MP 17:
    Code:
    dyngen {    
        update lx = lx[_n-1] - dx[_n-1] , missval(`intial_lx')
        update dx = lx[_n]     * qx[_n] , missval(`intial_dx')
    }
    My question is, is there a way to speed up this line by line calculation? I use this in a simulation and the dyngen is the only bottle-neck.

    Thank you
    Parth

  • #2
    Solved my own problem using a solution posted to recursive calculations by Clyde Schechter and Carlo Lazzaro https://www.statalist.org/forums/for...ver-a-variable
    Code:
    cap drop B
    g B=sum(ln(1-qx))
    replace B = exp(B)
    replace lx = round(B[_n-1]*lx[1],1) in 2/L
    Last edited by Parth Aks; 29 Apr 2024, 04:14.

    Comment

    Working...
    X