悬赏 10 个论坛币 未解决
Hi all,
I am trying to use marginsplots to graph a categorical by continous variable interaction between migrant status(pms4, 4 categories) and years of education (eduy) for a regression model predicting social economic status (ses) using multiple imputed data. I used the code from the ATS UCLA website as a template (http://www.ats.ucla.edu/stat/stata/f...arginsplot.htm). However, I'm getting an error when I run it. Here is the code:
program emargins2, eclass properties(mi)
version 13
args outcome
reg isei i.pms4##c.eduy male age fisei14 eduy
margins pms4, at(eduy=(0(1)21)) atmeans asbalanced ///
post predict(outcome(`outcome'))
end
program myret, rclass
return add
return matrix b = b
return matrix V= V
end
mi estimate: reg isei i.pms4##c.eduy male age fisei14
mi estimate, cmdok: emargins 1 // emargins is defined above
---------------------
And I get an error when running the "mi estimate, cmdok: emargins 1 " below is the error.
option outcome() not allowed
an error occurred when mi estimate executed emargins on m=1
r(198);
-------------------
The same error message occurs when I use the alternative code ( to mimic the template):
forvalues i=1/1 {
mi estimate, cmdok: emargins `i' // emargins is defined above
mat b= e(b_mi) // save mi point estimates
mat V = e(V_mi) // save mi vce
* run ologit and margins on the _mi_m==0 data
quietly reg isei i.pms4##c.eduy male age fisei14 if _mi_m == 0
quietly margins pms4, at(eduy=(0(1)21)) atmeans ///
asbalanced predict(outcome(`i'))
* myret puts b and V matrix in a place marginsplot can find
myret
*Technically we ran the program myret between margins and marginsplot.
*E(cmd) is the eclass scalar that tells Stata what the previous command was.
*So we have to set that to "margins" for marginsplot to work correctly
*(becaues marginsplot expects margins as the last estimation command).
mata: st_global("e(cmd)", "margins") // set previous cmd to margins
marginsplot, title("predicted SES") name(ses1,replace)
}
Thanks for reading! Any help would be appreciated.
BTW, I've already set the data to be the mi-form by the following code:
set seed 5576
mi set mlong
mi register imputed fisei14
mi impute regress fisei14 = isei eduy male age,add(20) force by(pms4)
Best,
Tianzhu