Announcement

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

  • Running two trajectory models while preserving previous trajectory models output

    "I first run the trajectory model using logit and then rename the variables produced by the first trajectory model. However, when I run the second trajectory model using ZIP, it overrides the _traj_Group_redlining variable from the first model. How can I preserve the _traj_Group_redlining variable after running the second trajectory model?"







    traj, model(logit) var(redlining_11-redlining_43) indep(zage_11-zage_43) order(0,1,0) weight(gsw1345)
    mat P = e(plot1)
    svmat P, names(col)

    replace trajT = ((trajT+1)*(43-11)/2)+11


    *trajplot, xtitle(Age) ytitle("Redlining") plotvars(trajT-U953)

    foreach var of varlist _traj_Group-U953{

    rename `var' `var'_redlining
    }



    traj, model(zip) var(vict_11-vict_43) indep(zage_11-zage_43) order(0,1,0) weight(gsw1345)

  • #2
    When I run my toy example, all _traj variables are overwritten, not just the Group variable. My guess is that every time you run traj it automatically cleans up the global environment by looking for variables that start with _traj using the wildcard syntax behind the scenes. Not the most Stata friendly behavior. I would expect the command to stop and print an error message or require an option flagging that you want to replace the data in memory.

    Regardless, prepending "redlining_" or in my case "rl_" seems to be a viable workaround. If you really need "_redlining" at the end and no other variable name will work, you could also save the data to a frame or to the filesystem and merge datasets when you are done using the traj command.

    Code:
    clear
    set obs 1000
    forv i = 1/10 {
        gen y_`i' = runiformint(0,1)
        gen x_`i' = runiformint(0,1)
    }
    traj, model(logit) var(y_1-y_5) indep(x_1-x_5) order(0,1,0)
    
    foreach var of varlist _traj_*{
        display "`var'"
        rename `var' rl_`var'
    }
    
    traj, model(zip) var(y_6-y_10) indep(x_6-x_10) order(0,1,0)
    
    describe, simple
    Code:
    . describe, simple
    y_1           y_4           y_7           y_10          _traj_Group
    x_1           x_4           x_7           x_10          _traj_ProbG1
    y_2           y_5           y_8           rl__traj_G~p  _traj_ProbG2
    x_2           x_5           x_8           rl__traj_P~1  _traj_ProbG3
    y_3           y_6           y_9           rl__traj_P~2
    x_3           x_6           x_9           rl__traj_P~3

    Comment


    • #3
      When I run this, it keeps the first models variables. (v18)

      Code:
      clear all
      
      use https://www.andrew.cmu.edu/user/bjones/traj/data/montreal_sim.dta, clear
      traj if scolmer<11, var(qcp*op) indep(age*) model(cnorm) min(0) max(10) order(1 3 2)
      rename _traj* first_*
      
      traj if scolmer>=11, var(qcp*op) indep(age*) model(cnorm) min(0) max(10) order(1 3 2)

      Comment

      Working...
      X