Running margins on predictions from a runmlwin model fit

Welcome to the forum for runmlwin users. Feel free to post your question about runmlwin here. The Centre for Multilevel Modelling take no responsibility for the accuracy of these posts, we are unable to monitor them closely. Do go ahead and post your question and thank you in advance if you find the time to post any answers!

Go to runmlwin: Running MLwiN from within Stata >> http://www.bristol.ac.uk/cmm/software/runmlwin/
Post Reply
erikruzek
Posts: 16
Joined: Tue May 21, 2019 3:57 pm

Running margins on predictions from a runmlwin model fit

Post by erikruzek »

Hi all,

I have been trying to figure out how to use runmlwin and Stata's margins command in combination. I know, based on reading other posts in the forum, that I can do something similar to margins by setting variables in the model to certain values and then running predict after runmlwin. This seems reasonable for many cases.

However, I am working with multiply imputed data (20 datasets) and the idea of having to do this procedure along with all the mi procedures is not terribly appealing. Although it might be easier than I am imagining! Instead, I have been playing with a workaround and wanted to get your thoughts on it. First, I run a model, then get predictions (xb) and stdps for that model (not altering the values of variables), and then I run mimrgns (user-written package for getting margins with MI data) on the xb and stdps. Below is the code I'm using:

Code: Select all

mi estimate, cmdok  saving(miest4): runmlwin enjoy year male langspan langother cons, ///
	level2(childid: year cons) level1(obs: cons) nopause 
eststo mlwin_gc_lang_sex

mi predict xb using miest4 // marginal predictions for year
mi predict xb_se using miest4, stdp // standard errors for marginal predictions 

** Margins on predictions
mi estimate: reg xb year i.homelang i.male
mimrgns i.homelang, at(year=(0(1)2)) dots cmdmargins // cmdmargins needed for marginsplot
_marg_save , saving(mimrgns_results , double) // _at with _m1 identifies unique marg effs

** Margins on standard errors
mi estimate: reg xb_se year i.homelang i.male
mimrgns i.homelang, at(year=(0(1)2)) dots cmdmargins 
_marg_save , saving(mimrgns_se_results , double replace) 

Once I have the two saved files, I can pull them into Stata for graphing.

What do you think of the validity of this approach? I can confirm that the margins I get from this are euqivalent to at least 2 decimal points to those from mimrgins after running the same model in mixed. The standard errors are also very close.

Admittedly this involves a lot of steps, so again, it might be just as easy or easier to try to get margins by creating some new observations and doing predictions for those.
ChrisCharlton
Posts: 1351
Joined: Mon Oct 19, 2009 10:34 am

Re: Running margins on predictions from a runmlwin model fit

Post by ChrisCharlton »

I showed this to George, but he wasn't able to say for sure.

His advice was that unless the models ran too slowly or couldn't be fitted with mixed you should use that. If you can't do that then he suggests initially running a subsample or simplified version of your model using both mixed and your manual method with runmlwin and checking that they give the same answers. If so you can then proceed to running the full model.

In terms of the checks you have done so far he thought they looked positive and are similar to the approach that he would take.
erikruzek
Posts: 16
Joined: Tue May 21, 2019 3:57 pm

Re: Running margins on predictions from a runmlwin model fit

Post by erikruzek »

Much appreciated, Chris and George. I hope you both are doing well!
Mira23ssw
Posts: 2
Joined: Wed May 03, 2023 2:47 am

Re: Running margins on predictions from a runmlwin model fit

Post by Mira23ssw »

ChrisCharlton wrote: Mon Nov 01, 2021 11:46 am I showed this to George, but he wasn't able to say for sure.

His advice was that unless the models ran too slowly or couldn't be fitted with mixed you should use that. If you can't do that then he suggests initially running a subsample or simplified version of your model using both mixed and your manual method with runmlwin and checking that they give the same answers. If so you can then proceed to running the full model.

In terms of the checks you have done so far he thought they looked positive and are similar to the approach that he would take.
It's very helpful, thanks
lisamassa
Posts: 2
Joined: Wed Oct 18, 2023 4:25 am

Re: Running margins on predictions from a runmlwin model fit

Post by lisamassa »

Your approach seems reasonable, especially when dealing with multiple imputed rice purity test data. It's good to hear that the margins and standard errors are close to those from the margins after running the same model in mixed. However, if the process involves a lot of steps, it might be worth considering alternative methods to achieve the same results.
tetroupploy
Posts: 3
Joined: Wed Dec 20, 2023 8:55 am

Re: Running margins on predictions from a runmlwin model fit

Post by tetroupploy »

Chris and George, you are very much appreciated. I am hoping that both of you are being well :mrgreen: geometry dash lite
Post Reply