Announcement

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

  • Graph question for growth model "xtmixed"

    Hi, I'm having a problem in making a line graph for my growth model.
    The outcome variable is depression level: dep_ (categorical)
    The main independent variable is gender: sex_ (coded as 0/1)
    One time-varying variable is alcohol use: alcohol_new (coded as 0/1)
    Time variable is: year_new (4 waves coded as 0, 1, 2 3)
    My original command is: xtmixed dep_ sex_ alcohol_new year_new i.sex_#c.year_new i.alcohol_new#c.year_new|| id: year , cov(un) variance mle
    I'm wondering how could I do the predict function and then make a line graph which fits this model the best.
    Thank you!

  • #2
    Originally posted by Mengyao Hu View Post
    Hi, I'm having a problem in making a line graph for my growth model. . . . I'm wondering how could I do the predict function and then make a line graph which fits this model the best.
    Run the code below to see how you could use the margins and marginsplot postestimation commands to approach your objective a little more easily than making the predictions manually and plotting those. (Begin at the "Begin here" comment; the beginning of the code is just to make a phony dataset for illustration.) I've used different names for variables corresponding to those in your dataset, but you'll be able to see the parallels to your own variable names.
    Code:
    version 16.1
    
    clear *
    
    set seed `=strreverse("1543967")'
    quietly set obs 250
    
    generate int pid = _n
    drawnorm pid_u tim_u, double corr(1 -0.25 \ -0.25 1)
    generate byte sex = mod(_n, 2)
    
    quietly expand 4
    bysort pid: generate byte tim = _n - 1
    generate byte alc = rbinomial(1, 0.5)
    
    generate double lat = ///
        cond(sex, -1, 1) * (tim - 1.5) + ///
            pid_u + (tim - 1.5) * tim_u + ///
                rnormal()
    egen byte ham = cut(lat), group(61)
    
    *
    * Begin here
    *
    mixed ham i.sex##c.tim i.alc##c.tim || pid: tim, covariance(unstructured) ///
        nolrtest nolog
    
    quietly margins sex, at(tim = (0 1 2 3))
    marginsplot , title("") level(50) ///
        ytitle(HAM-D) ylabel( , angle(horizontal) nogrid) ///
            xtitle(Wave) ///
                note(Women Red and Men Blue) legend(off)
    
    exit

    Comment

    Working...
    X