Announcement

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

  • Extracting random intercepts and slopes from mixed effects model run on multiply imputed data

    Good morning! I am trying to extract random slopes and intercepts from a mixed effects model (of participants with cognition scores measured over time) run on multiply imputed data, but can't get Stata to do it. I am running ver 16.1 on an institutional server that does allow me to install user-created packages.

    I tried this way but got an error:
    Code:
     
    .  mi estimate, saving(miest,replace): mixed depvar indepvars || id: timeyrs, vce(cluster clustervar)
    
    <<bunch of expected model output>>
    
    .  mi predict randeffects using miest, reffects
    option reffects not allowed
    r(198);
    One prior post had suggested using mi xeq to run the model sequentially on each imputed dataset, but:

    I tried this and got an error after a reasonable calculation time (i.e., it probably ran the model but failed at the "saving" step):
    Code:
    .  mi xeq 0: mixed depvar indepvars || id: timeyrs,  vce(cluster clustervar) saving(miest0)
    option saving() not allowed
    r(198);
    And, in case that's the wrong place for saving(), I tried putting it elsewhere but it errored out immediately:
    Code:
    .  mi xeq 0, saving(miest0): mixed depvar indepvars || id: timeyrs,  vce(cluster clustervar) 
    invalid numlist
    r(121);
    Code that works to get these values OUTSIDE the mi environment (i.e., on non-imputed data):
    Code:
    .  mixed depvar indepvars || id: timeyrs, vce(cluster clustervar)
    .  predict u*, reffects
    .  gen intercept = _b[_cons] + u1
    .  gen slope = _b[timeyrs] + u2
    It sounds like others have had this problem -- e.g.,
    This thread from 2013: https://www.stata.com/statalist/arch.../msg00002.html
    And this one from 2016: https://www.statalist.org/forums/for...=1643997013987

    Does anyone know of a workaround for this, or have any other suggestions? Thank you in advance for any help.

  • #2
    With the caveat that I have never used the -mi- commands, looking that the syntax for -mi xeq-, this appears to work:

    Code:
    clear*
    webuse mjsps5
    forv i = 1/5 {
        qui mi xeq  `i': mixed math5 math3 || school: ; /// 
            predict re_hat`i', reffects;  /// 
            save re_hat`i',replace
    }
    use re_hat1,clear
    forv i = 2/5 {
        qui merge 1:1 _n using re_hat`i', nogenerate
    }
    sum re_hat*

    Comment


    • #3
      Scott Merryman thank you for your response! I finally got a chance to test it.

      Trying to predict after running the mixed model using that code yielded

      predict, reffects requires that the data used in estimation be in memory
      r(459);


      and adding "saving" was, uh, politely declined as above, but I borrowed your loop code (which, thank you for that - loops are my bugbear!) and used mi extract instead:
      Code:
      use "mi_dataset.dta", clear
      gen obs = _n
      save "mi_dataset.dta", replace
      
      forvalues i = 0/25 {
          use "mi_dataset.dta", clear
          mi extract `i'
          gen whichmi = `i'
          qui mixed depvar indepvars || id: timeyrs, vce(cluster clustervar) 
          predict randeffect`i'_*, reffects
          keep newid obs whichmi randeffect`i'_*
          save miest`i', replace
      }
      
      use miest0, clear
      
      forvalues i = 1/25 {
          qui merge 1:1 obs using miest`i', nogenerate
      }
      
      gen intercept=.
      gen slope=.
      
      forvalues i = 0/25 {
          replace intercept = randeffect`i'_2 if whichmi==`i'
          replace slope = randeffectt`i'_1 if whichmi==`i'
      }
      
      keep id obs whichmi intercept slope
      save randeffects, replace
      use "mi_dataset.dta", clear
      merge 1:1 obs using "randeffects.dta"
      That way, the whichmi variable serves as a check that the slope and intercept value is matched with the correct _mi_m - I am a little nervous matching on obs (rather than id - I think I could have created a new id variable and used that as a second-line check but this whole thing takes over an hour to run and I didn't want to redo it before posting since I had no suspicion of any nefarious mismatching...

      So, thank you! I hope I will not find any issues working with this dataset from here, and I'm really grateful for the help!

      Comment


      • #4
        Liz Whitlock

        I am glad you found a solution.

        The syntax for -mi xeq- indicates that multiple commands are on one-line separated by semi-colons, not separate commands on separate lines.
        Code:
                mi xeq [numlist]:  command [; command [; ...]]
        It appears that both return the same predicted random effects:
        Code:
        . webuse mjsps5,clear
        (LEA Junior School Project data (Mortimore et al., 1988) with missing values)
        
        . mi xeq 0: qui mixed math5 math3 || school: ;  ///
        >         predict randomeffects , reffects ; /// 
        >         sum randomeffect
        
        m=0 data:
        -> qui mixed math5 math3 || school:
        -> predict randomeffects , reffects
        -> sum randomeffect
        
            Variable |        Obs        Mean    Std. dev.       Min        Max
        -------------+---------------------------------------------------------
        randomeffe~s |        887   -.0061503    1.468736  -3.344107   2.764939
        
        . 
        . webuse mjsps5,clear
        (LEA Junior School Project data (Mortimore et al., 1988) with missing values)
        
        . mi extract 0
        
        . qui mixed math5 math3 || school:
        
        . predict randomeffects, reffects
        
        . sum randomeffects
        
            Variable |        Obs        Mean    Std. dev.       Min        Max
        -------------+---------------------------------------------------------
        randomeffe~s |        887   -.0061503    1.468736  -3.344107   2.764939

        Comment


        • #5
          Aha - i had missed that, thanks so much for calling my attention to it. I bet I will be able to use mi xeq, then; it seems more elegant than extract. Really appreciate your help & that note of clarification

          Comment

          Working...
          X