Announcement

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

  • Multiple imputation, mimrgns, factor variables and coefplot

    I'm trying to graph predicted values based on a model estimated on a multiply imputated dataset. I'm using the excellent -mimrgns- command to calculate predictive margins. Take the following example:

    Code:
    sysuse auto, clear
    quietly replace mpg = . in 3
    mi set wide
    mi register imputed mpg
    mi impute poisson mpg weight price foreign, add(2)
    
    mi estimate: reg mpg rep#foreign
    mimrgns rep#foreign, cmdmargins post
    marginsplot
    Click image for larger version

Name:	marginsplot.png
Views:	1
Size:	61.2 KB
ID:	1451335


    Now this actually works fine, except that marginsplot does not display the correct confdence intervals, as explained in the manual:
    Note that while in principle marginsplot works after mimrgns, the plotted confidence intervals are based on inappropiate degrees of freedom (more). The appropriate degrees of freedom are saved in r(df) (or r(df_vs) if option pwcompare is specified) by mimrgns. With the post option the degrees of freedom are also saved in e(df_mi) (or e(df_vs)). Although the differences will typically be too small to notice in a graph, you may want to use an alternative to marginsplot that allows specifying the degrees of freedom used to calculate confidence intervals (e.g. Jann's (2013) coefplot).
    So I am trying to use coefplot. I would like to obtain a similar graph as the one made by marginsplot, but I'm having troubles in specifying this. For the two discrete factor variables, rep needs to go on the x asix and foreign needs to be grouped visually using a line in the foreign and the non-foreign cars. I tried several options, but unsuccesful. A main difficulty is that for such subplot-like data usually separate estimation results are stored in the examples in coefplot's manual. This is as far as I got:

    Code:
    coefplot , recast(line) vertical xtitle(Repair Record 1978) xlabel(#5) baselevels
    I would be grateful to any coefplot expert who knows the solution.

  • #2


    I do not have a complete answer and I do not have the time to figure this out with all the details but I might be able to get you started.

    Before I do, allow me to comment on the example that you have used (thanks for providing one, by the way) as I find some of the details irritating. The first thing I notice is that you seem to omit the (conditional) main effects from your regression model. This is fine for the example but if your real model includes more (continuous) predictors, you want

    Code:
    rep78##foreign
    i.e., the main effects along with the interaction. Another thing that strikes me as a bit unconventional is the imputation model. There are 5 missing values in rep78 that you do not impute. You actually do not even include rep78 in the imputation model. Again, for a silly example, this might be fine but you want to have all variables that you use in the analysis in your imputation model in real life. You do include price and weight in the imputation model, but do not include these variables in the analysis. That is probably fine for the example and for real-life data since you are imputing your outcome variable, mpg, which you should only do if you have auxiliary information. Last, a poisson model might or might not be best suited to impuite values for mpg but you want your imputation model to be consistent with your analysis model; note that you switch to linear regression in the analysis. I believe you want to stick to either possion or regress. Having pointed these things out, let's tackle your question.

    As you have already noticed, there is no canned solution for what you want to do so we need some workaround. As I have pointed out elsewhere, it is always a good idea to start with the non-imputed case, then add complexity; so let's start with margins. Note that

    Code:
    margins rep78#foreign
    marginsplot
    can also be written as

    Code:
    margins rep78 , at(foreign = (0 1))
    marginsplot , xdimension(rep78)
    To replicate the above with coefplot, we will need to further split the results and store them in different models.

    Code:
    regress mpg rep##foreign
    
    _estimates hold myresults , copy
    margins rep78 , at(foreign = 0) post
    estimates store domestic
    
    _estimates unhold myresults
    margins rep78 , at(foreign = 1) post
    estimates store foreign
    The _estimates lines are necessary because margin's post option (use only when you know what you are doing) will wipe the regression results and replace them with its own. Since we will call margins repeatedly, we want to bring the regression results back. Read more on this in

    Code:
    help _estimates
    We now have the two models that we wish to plot. We do this with

    Code:
    coefplot (domestic)(foreign) , recast(connected) vertical
    The CIs are not aligned because this is how coefplot makes it easy to identify separate models. Also, the labels are messed. Probably you want to fix some other things, too, but I do not have the time to fiddle around with this and leave the details to you (and/or others).

    Anyway, we can now return to the imputed case. Luckily, all we need to do is replace margins with mimrgns in the code. Here is the complete example (modified regarding the issues I have pointed out above)

    Code:
    sysuse auto, clear
    
    mi set wide
    mi register imputed rep78
    mi impute mlogit rep78 = mpg weight price , add(5)
    
    mi estimate : regress mpg rep##foreign
    
    _estimates hold miresults , copy
    mimrgns rep78 , at(foreign = 0) post
    estimates store domestic
    
    _estimates unhold miresults
    mimrgns rep78 , at(foreign = 1) post
    estimates store foreign
    
    coefplot (domestic)(foreign) , recast(connected) vertical
    Before I forget (, too): mimrgns is from SSC (most likely) and coefplot might be from SSC or SJ (I have mine from SSC).

    Best
    Daniel
    Last edited by daniel klein; 02 Jul 2018, 02:48.

    Comment

    Working...
    X