BAILEY DEBARMORE
  • Home
  • Productivity
    • Blog
    • My Recs
  • EPI
    • EPICODE
    • #EpiWritingChallenge >
      • About the EWC
      • All Posts
  • Wellness
    • Health Blog
    • My Recs
  • Freebies

Creating a Transition Matrix in Stata

9/11/2020

0 Comments

 
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". 
Helpful Links
Reshape (Stata): https://www.stata.com/manuals13/dreshape.pdf
xt (Stata): https://www.stata.com/manuals13/xt.pdf
xttrans2 (Nicholas Cox): https://ideas.repec.org/c/boc/bocode/s416301.html (help file here)
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.
xtset id
xttrans status
​xttrans status, freq
xttrans status, by(sex)
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.
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    Picture
    Picture

    Practical solutions for conducting great epidemiology methods. Transparency in code. Attitude of constant improvement.

    Appreciate my stuff?

    Picture

    Picture
    Picture
    Picture
    Picture

    Picture
    Picture

    All
    Bailey DeBarmore
    Data Visualization
    Excel
    IPW
    Paul Zivich
    P Values
    Python
    R
    Regression
    SAS
    Stata
    ZEpid


    Picture

    March 2021
    September 2020
    April 2019
    September 2018
    August 2018
    July 2018
    June 2018
    May 2018


    RSS Feed

BLOGS

Work & Productivity
Health and Nutrition
EPICODE

About

About Bailey
CV and Resume
CONTACT

RD EXAM

Study Smarter Method
RD Exam Resources

FIND ME ON

Facebook
LinkedIn
Twitter
Google Scholar
Research Gate
Terms & Conditions | Privacy Policy | Disclaimers
Copyright Bailey DeBarmore © 2020
  • Home
  • Productivity
    • Blog
    • My Recs
  • EPI
    • EPICODE
    • #EpiWritingChallenge >
      • About the EWC
      • All Posts
  • Wellness
    • Health Blog
    • My Recs
  • Freebies