Author: Bailey DeBarmore
Do you want to model trajectories by calculating transition probabilities? You can do this in Stata with just a little extension of your longitudinal data analysis skills.
We'll be using xttrans (built in to Stata) or xttrans2 (a module you can download).
Your data will need to be in LONG format. To reshape from wide to long, use:
reshape long stub, i(id) j(time)
Where stub is the stubname of your variable, i is the variable that uniquely identifies observations in your data set, and j is the time variable.
What's a stubname? If you currently have variables status1, status2, status3 with participant status (dead/alive) at years 1, 2, and 3, your stubname would be status and your time variable (j) that we are creating would be year.
reshape long status, i(id) j(year)
The code above will create a new variable (column) called "year", and our long dataset will have 3 rows for each id (observation) and only one variable for status.
It's normal to have your dataset saved in a wide and long format. You can also go back to wide by typing "reshape wide".
First, xtset your data - you're telling Stata what variable uniquely identifies the "panels" since your long data form has repeated rows. For this example, it would be "xtset id".
Then we can use xttrans <var> for our transition probabilities. Default is to just display probabilities. If you also want the frequency counts, you can use the option "freq".
We can also use "by" with xttrans.
xttrans2, a module written by Nicholas Cox, expands on xttab and xttrans. You can specify additional options (the same options as tabulate, except for row and freq) but you can't use "by".
A big advantage of xttrans2 is that you can save the resulting probability matrix using the option matcell.
Practical solutions for conducting great epidemiology methods. Transparency in code. Attitude of constant improvement.
Appreciate our stuff?