Page 1 of 1

Plotting interaction terms

Posted: Tue Mar 12, 2019 1:50 pm
by Meiko9494
Hello,

I am fairly new to using runmlwin but I have fitted a two - level cross-classified logistic models using runmlwin (mcmc). I would like to plot/graph an interaction between two variables but I have not been able to find an alternative to the margins command. This is the regression with factor notation that I run:

Code: Select all

quietly runmlwin ideology1 cons c.Age##c.Age  0.employment i.Educ#i.employment dmarital1  i.practice i.Educ#i.practice  i.dev1 i.Educ#i.dev1 divorce i.class income i.Nchildren , level2(Period:cons) level1(Cohort:) discrete(dist(binomial) link(logit) denom(cons)) nopause

runmlwin ideology1 cons c.Age##c.Age  0.employment i.Educ#i.employment dmarital1  i.practice i.Educ#i.practice  i.dev1 i.Educ#i.dev1 divorce i.class income i.Nchildren, level2(Period:cons) level1(Cohort:) discrete(dist(binomial) link(logit) denom(cons)) mcmc(cc) initsprevious nopause
runmlwin, noheader noretable or
I then removed factor notation and added all the dummies, then set all variale to zero except the two variables of interest (education x employment interactions) and attempted to predict the outcome + SE :


Code: Select all

quietly runmlwin ideology1 cons Age agesq  demploy1 deduc2#demploy1 deduc3#demploy1 deduc4#demploy1 dmarital1  dprac2 dprac3 demploy1 deduc2#dprac1 deduc2#dprac2  deduc2#dprac3 deduc3#dprac1 deduc3#dprac2  deduc3#dprac3     deduc4#dprac1 deduc4#dprac2  deduc4#dprac3  devd1 deduc2#devd1 deduc3#devd1 deduc4#devd1 divorce devd1 dclass2 dclass3 income dchild2 dchild3 dchild4 dchild5, level2(Period:cons) level1(Cohort:) discrete(dist(binomial) link(logit) denom(cons)) nopause

runmlwin ideology1 cons Age agesq  demploy1 deduc2#demploy1 deduc3#demploy1 deduc4#demploy1 dmarital1  dprac2 dprac3 demploy1 deduc2#dprac1 deduc2#dprac2  deduc2#dprac3 deduc3#dprac1 deduc3#dprac2  deduc3#dprac3     deduc4#dprac1 deduc4#dprac2  deduc4#dprac3  devd1 deduc2#devd1 deduc3#devd1 deduc4#devd1 divorce devd1 dclass2 dclass3 income dchild2 dchild3 dchild4 dchild5, level2(Period:cons) level1(Cohort:) discrete(dist(binomial) link(logit) denom(cons)) mcmc(cc) initsprevious nopause
runmlwin, noheader noretable or

local unwanted = "Age agesq dmarital1 dprac2 dprac3 devd1 divorce dclass2 income dchild2 dchild3 dchild4 dchild5 "
foreach var in `unwanted' {
    replace `var' = 0
}
* Predict 
predict yhat
predict yhat_se, stdp
gen yhat_lb = yhat - 1.96 * yhat_se
gen yhat_ub = yhat + 1.96 * yhat_se
The predict yhat returns the following error:
variable _0b_deduc2_1_demploy1 not found

I am not sure what I am doing incorrectly? Thank you.

For reference, I am attempting to replicate the following code:

margins, at(Educ=(0(1)3)) over(employment)
marginsplot

Re: Plotting interaction terms

Posted: Wed Mar 13, 2019 11:58 am
by ChrisCharlton
The immediate cause of the error that you are seeing is because although runmlwin creates derived variables for each expanded factor/interaction these are only used during model fitting and are not added back to the initial data. Presumably the predict command for built-in Stata commands recreates these on the fly, but doesn't specifically understand the runmlwin output. You would therefore need to re-create these before doing the prediction with code similar to the following:

Code: Select all

use http://www.bristol.ac.uk/cmm/media/runmlwin/tutorial, clear

local fixedpart standlrt schgend#girl // Replace with fixed part specification of the model

fvexpand `fixedpart' // Create expanded variable list
local vars `r(varlist)' // Store result
local vars : list vars - fixedpart // Remove non-derived variables from list
fvrevar `vars' // Create temporary variable for each derived variable
local tempvars `r(varlist)' // Store list of created variable names
forvalues i = 1/`:list sizeof vars' { // Copy each temporary variable to appropriately named new variable
	gen `=strtoname(word("`vars'", `i'))' = `=word("`tempvars'", `i')'
}