Announcement

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

  • Mimrgns

    Hi
    I'am trying to obtain margins after MI estimate and test differences between each slopes by a group . I have read that we need to use mimrgns. however after a piecewise mixed model I don't know how to do that. I have a code to do that just for non imputed data. See below. I replace "margins" by "mimrgns" but the saving option is not compatible. how can I obtain all estimates in order to plot the graph???
    thanks a lot
    carole

    forvalues t = 1/5 {
    local at`t'
    forvalues i = 1/4 {
    summ time`i' if Time == `t', meanonly
    local at`t' `at`t'' time`i' = `r(mean)'
    }
    display "at(`at`t'')"
    }

    tempfile results
    margins porteur, at(`at1') at(`at2') at(`at3') at(`at4') at(`at5') saving(`results')
    use `results', clear
    keep _m1 _at _margin _ci*
    rename _margin dep
    rename _at time
    reshape wide dep _ci*, i(time) j(_m1)
    label var dep0 "Non-porteur"
    label var dep1 "Porteur"
    graph twoway (connect dep* time) (rcap _ci_lb0 _ci_ub0 time, legend(off)) ///
    (rcap _ci_lb1 _ci_ub1 time, legend(off))

  • #2
    mimrgns, as you are asked to explain, is (probably) from SSC. It does not support margins' (undocumented) saving() option; it does, however, store its results in r(table). I suppose you could run your model on non-imputed data and create a "template-file" with margins' saving() option. You could then re-run the model using mi estimate followed by mimrgns. Last you would need to replace the relevant variables of the template-file (_margin, _ci_lb, and _ci_ub) with the respective information from r(table) (rows 1, 5, and 6).

    If this sounds complicated, you now know (one of) the reason why mimrgns does not support the saving() option. However, if you provide some example data, I might find the time to suggest a syntax.

    Best
    Daniel

    Comment


    • #3
      Hi Daniel, thanks for your reactivity. i join a dataex example....
      [
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str6 ID byte time float(ES0 dep) double R byte(_mi_miss time1 time2 time3 time4 time5) float(_1_ES0 _1_dep _2_ES0 _2_dep _3_ES0 _3_dep _4_ES0 _4_dep _5_ES0 _5_dep _6_ES0 _6_dep _7_ES0 _7_dep _8_ES0 _8_dep _9_ES0 _9_dep _10_ES0 _10_dep)
      "030001" 0 19 45 7 0 0 0 0 0 0 19 45 19 45 19 45 19 45 19 45 19 45 19 45 19 45 19 45 19 45
      "030001" 1 19 47 7 1 1 0 0 0 0 19 47 19 47 19 47 19 47 19 47 19 47 19 47 19 47 19 47 19 47
      "030001" 2 19  8 7 0 2 1 0 0 0 19  8 19  8 19  8 19  8 19  8 19  8 19  8 19  8 19  8 19  8
      "030001" 3 19 53 7 0 3 2 1 0 0 19 53 19 53 19 53 19 53 19 53 19 53 19 53 19 53 19 53 19 53
      "030001" 4 19 25 7 0 4 3 2 1 0 19 25 19 25 19 25 19 25 19 25 19 25 19 25 19 25 19 25 19 25
      end
      label values R porteur

      my syntax is the following :
      Code:
       
      mi estimate: mixed dep c.ES0 i.R##c.(time1 time2 time3 time4 time5) || ID: time1 time2 time3 time4 time5 ES0, mle
      I also need contrast for each knots (time1,2 3 until 5) by R.

      the syntax that clyde give me for mixed without mi estimates is the following.
      Code:
      forvalues t = 1/5 {
          local at`t'
          forvalues i = 1/4 {
              summ time`i' if time == `t', meanonly
              local at`t' `at`t'' time`i' = `r(mean)'
          }
          display "at(`at`t'')"
      }
      
      tempfile results
      margins R, at(`at1') at(`at2') at(`at3') at(`at4') at(`at5') saving(`results')
      use `results', clear
      keep _m1 _at _margin _ci*
      rename _margin dep
      rename _at time
      reshape wide dep _ci*, i(time) j(_m1)
      label var dep0 "Non-porteur"
      label var dep1 "Porteur"
      graph twoway (connect dep* time) (rcap _ci_lb0 _ci_ub0 time, legend(off)) ///
          (rcap _ci_lb1 _ci_ub1 time, legend(off))
      I have tried to adapt with mimrgns but impossible :-(

      thanks a lot for your help Daniel
      best carole

      Comment


      • #4
        Carole

        Thanks for the data example. Unfortunately, it is not as helpful as I have hoped. The example is neither mi set nor does it contain sufficient observations to run the mixed model. I will, therefore, use an example dataset and a simple logit model to demonstrate the basic approach; you will have to adapt the code to your needs.

        Code:
        // example mi dataset
        clear
        webuse mheart1s20
        
        // step 1: run the model ignoring imputations
        logit attack bmi i.smokes i.female
        
        // step 2: built a template file
        tempfile tmp
        margins female , at(bmi = 25 smokes = 0) at(bmi = 25 smokes = 1) ///
            saving("`tmp'")
            
            // your file should look basically the same
        preserve
        use "`tmp'" , clear
        list
        restore
        
        // step 3: re-run the model; this time with mi
        mi estimate : logit attack bmi i.smokes i.female
        
        // step 4: mimrgns
        mimrgns female , at(bmi = 25 smokes = 0) at(bmi = 25 smokes = 1) ///
            predict(default) // <- we use margins default predictions
        
        // step 5: extract info from r(table)
        tempname rtable
        mata : st_matrix("`rtable'", st_matrix("r(table)")[(1::6), .]')
            
            // the matrix resembles the output table
        matlist `rtable'
        
        // step 6: now bring back out template file
        use "`tmp'" , clear
        list // <- should have the same number of rows as the matrix
        
        // step 7: replace the relevant variables
        mata : st_store(., ///
                ("_margin", "_se", "_stat", "_pvalue", "_ci_lb", "_ci_ub"), ///
                st_matrix("`rtable'"))
        list // <- inspect result
        
        // step 8: insert your graph syntax below
        keep _m1 _at _margin _ci*
        ...
        I hope this helps.
        Daniel

        Comment


        • #5
          thanks daniel. the predict(defaut) option does not function. see error message below
          Code:
          requested action not valid after most recent estimation command

          Comment


          • #6
            Carole

            I would need more context to comment on this one; starting with the version of mimrgns that you are using. You should be using

            Code:
            . which mimrgns
            c:\ado\plus\m\mimrgns.ado
            *! version 4.0.2 22sep2019 daniel klein
            or something close. If you are not, type

            Code:
            ssc install mimrgns , replace
            discard
            However, I doubt that the error message is triggered by the predict(default) option. Please provide the syntax that you have used (exactly) along with what Stata did in response (see FAQ #12). What was the exact command that produced the error message that you show? What was the exact error message (including the return code)?

            All I can say now is that you might not even need to specify the predict() option at all. I have used it in the example because I wanted predicted probabilities, which margins would produce by default after logit. mimrgns uses predict(xb) as a default and if this is what you want, do not bother to specify this option. As I have indicated above, I doubt that this is the underlying problem.

            Best
            Daniel

            Comment

            Working...
            X