Illustrate lower-level interaction by use of a graph similar to margins command

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
KazimovHH
Posts: 14
Joined: Mon Apr 09, 2018 11:56 am

Illustrate lower-level interaction by use of a graph similar to margins command

Post by KazimovHH » Mon Dec 03, 2018 3:51 pm

Dear Community,

I am trying to replace margins command after the three-level multilevel model with a lower-level interaction effect fitted in runmlwin (Stata/SE 15.1).
The single-level regression analysis is written as follows with margins command at the end:

Code: Select all

reg stfdem_s c.f_polintr_n##(c.dev_ideology c.dev_environment c.dev_equality c.dev_values c.dev_immigration c.dev_euintegration) ///
							female_n eduyrs_n stfeco_n stfhlth_n stfedu_n dis_gall_n single_majority minimal_coalition surplus_coalition single_minority multi_minority ///
							i.country [aweight=dweight] 
							
							foreach var of varlist dev_ideology dev_environment dev_equality dev_values dev_immigration dev_euintegration {
	
	local title : variable label `var'
	
	margins, at(`var'=(0(.1)1) f_polintr_n=(0 1))
	marginsplot, recast(line) recastci(rarea) ///
		title("`title'") ///
		xtitle("congruence") ///
		ytitle("predicted satisfaction") ylabel(40(5)65) ///
		legend(order(3 "low interest in politics" 4 "high interest in politics") col(1)) ///
		name(`var', replace)
		}

graph combine dev_ideology dev_environment dev_equality dev_values dev_immigration dev_euintegration  		
graph export test_combine.png 
After running the margins command, I have the following graph:
test_combine.png
Single-level margins command
test_combine.png (178.65 KiB) Viewed 1207 times
Now, I write the three-level model with runmlwin:

Code: Select all

sort country cabinet idno 
	set seed 1073741823
	quietly runmlwin stfdem_s cons ///
	int1 int2 int3 int4 int5 int6 ///
	dis_gall_n ///
	single_majority minimal_coalition surplus_coalition single_minority multi_minority ///
	dev_ideology dev_environment dev_equality dev_values dev_immigration dev_euintegration ///
	female_n eduyrs_n f_polintr_n stfeco_n stfhlth_n stfedu_n, ///
	level3(country: cons f_polintr_n) level2(cabinet: cons) level1(idno: cons) nopause 
	
	display "`c(current_time)' `c(current_date)'"		

	runmlwin stfdem_s cons ///
	int1 int2 int3 int4 int5 int6 ///
	dis_gall_n ///
	single_majority minimal_coalition surplus_coalition single_minority multi_minority ///
	dev_ideology dev_environment dev_equality dev_values dev_immigration dev_euintegration ///
    	female_n eduyrs_n f_polintr_n stfeco_n stfhlth_n stfedu_n, ///
	level3(country: cons f_polintr_n) level2(cabinet: cons) level1(idno: cons) ///
	mcmc(cc burnin(5000) chain(50000) hcen(3)) initsprevious nopause zratio 
	
	display "`c(current_time)' `c(current_date)'"	
Then, I plot the model output

Code: Select all

* (1) Set variables to zero except for the variables of interest: 

local unwanted = "int2 int3 int4 int5 int6 dis_gall_n single_majority minimal_coalition surplus_coalition single_minority multi_minority dev_environment dev_equality dev_values dev_immigration dev_euintegration female_n eduyrs_n stfeco_n stfhlth_n stfedu_n"
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	
	
* Plot the results for the first interaction: Dev_ideology * political interest	
	twoway  (rarea yhat_ub yhat_lb dev_ideology if f_polintr_n == 0, sort fcolor(blue) fintensity(inten10)  lstyle(grid)) ///	   
			(rarea yhat_ub yhat_lb dev_ideology if f_polintr_n == 1, sort fcolor(red) fintensity(inten10)  lstyle(grid)) ///   
			(line yhat dev_ideology if f_polintr_n == 0, lcolor(blue)) ///		
			(line yhat dev_ideology if f_polintr_n == 1, lcolor(red)) ///
			(histogram dev_ideology, percent yaxis(2) color(gs10%70) width(0.05) ytitle(Percentage of observations, axis(2))), ///
			bgcolor(white) graphregion(color(white)) scale(0.7) ytitle(Predicted 'Satisfaction With Democracy') ///
			legend(order(1 "Low interest in politics: 95 per cent confidence intervals" 2 "High interest in politics: 95 per cent confidence intervals" 3 "Predicted values: low political interest" 4 "Predicted values: high political interest") cols(1))
			
	graph save int1_ideology, replace 
	graph export int1_ideology.png, replace 
Example plot for interaction term 1:
int1_ideology.png
example plot Ideology
int1_ideology.png (134.53 KiB) Viewed 1207 times

Subsequently, I repeat these codes for each interaction term.
I have the following question:

(1) Is this the right way to produce results similar to the margins command when I specify the variable political interest as 0 and 1 for low and high political interest?

I include the specification of the margins command here for convenience:

Code: Select all

margins, at(`var'=(0(.1)1) f_polintr_n=(0 1))


Best regards,
Rza

GeorgeLeckie
Site Admin
Posts: 427
Joined: Fri Apr 01, 2011 2:14 pm

Re: Illustrate lower-level interaction by use of a graph similar to margins command

Post by GeorgeLeckie » Tue Dec 18, 2018 4:57 pm

Dear KazimovHH,

There is a lot going on here, but at least from a quick scan it looks like you are doing something sensible.

You fit your three level model

You then set all variables to zero except for the the two variables of interest (one of which is binary, the other continuous)

You then predict the outcome and the SE of the outcome

You then calculate the lower and upper limits of the 95% CI for the predicted outcomes

You then plot the predicted outcome again the continuous covariate separately for the two values of the binary covariate

You superimpose the 95% confidence envelope.

Best wishes

George

KazimovHH
Posts: 14
Joined: Mon Apr 09, 2018 11:56 am

Re: Illustrate lower-level interaction by use of a graph similar to margins command

Post by KazimovHH » Thu Dec 20, 2018 1:24 pm

Dear George,

Thank you very much for the guidelines. It is very helpful.

Best Regards,
Rza

Post Reply