Hello,
I am an avid stata user and researcher who conducts tests of moderation regularly. When testing moderation in my field, it is customary to plot the interaction at +1 and -1 standard deviation above the mean. As far as I am aware, there is not a command that does this easily or automatically, so I generated the below code to make the plots I want.
In this code, the local functions are utilized to specify which variables go into each position (e.g., IV, DV, Moderator).
*-----CODE
reg dependentvar c.independentvar##c.moderatorvar
local mod "moderatorvar"
local iv "independentvar"
local dv "dependentvar"
summarize `mod'
local Modplus1sd = r(mean) + (1 * r(sd) ) // 1 specifies 1 SD above the mean.
local Modminus1sd = r(mean) - (1 * r(sd) )
local modmean=r(mean)
summarize `iv'
local IVplus1sd = r(mean) + r(sd)
local IVminus1sd = r(mean) - r(sd)
margins, dydx(`iv') at(`mod'=(`Modminus1sd' `modmean' `Modplus1sd')) // Test of Simple Slopes
margins, at(`iv'=(`IVminus1sd' `IVplus1sd') `mod'=(`Modminus1sd' `Modplus1sd')) atmeans asbalanced plot (ytitle(`dv') xtitle(`iv') title(Interaction of `iv' and `mod' on `dv')) nose
*---- END CODE
However, this block of code can become repetitive when I have a lot of plots to graph, it takes up a lot of code space, and it makes my do files look messy. Is it possible to write a new program and create a new command that runs this code. For example, it would be great if this program could be written into a command like this that outputs the plot and the test of simple slopes:
modplot depvar indepvar modvar, atsd(1)
Can someone help me with writing/learning to write a new command that runs this code?
Thank you!
I am an avid stata user and researcher who conducts tests of moderation regularly. When testing moderation in my field, it is customary to plot the interaction at +1 and -1 standard deviation above the mean. As far as I am aware, there is not a command that does this easily or automatically, so I generated the below code to make the plots I want.
In this code, the local functions are utilized to specify which variables go into each position (e.g., IV, DV, Moderator).
*-----CODE
reg dependentvar c.independentvar##c.moderatorvar
local mod "moderatorvar"
local iv "independentvar"
local dv "dependentvar"
summarize `mod'
local Modplus1sd = r(mean) + (1 * r(sd) ) // 1 specifies 1 SD above the mean.
local Modminus1sd = r(mean) - (1 * r(sd) )
local modmean=r(mean)
summarize `iv'
local IVplus1sd = r(mean) + r(sd)
local IVminus1sd = r(mean) - r(sd)
margins, dydx(`iv') at(`mod'=(`Modminus1sd' `modmean' `Modplus1sd')) // Test of Simple Slopes
margins, at(`iv'=(`IVminus1sd' `IVplus1sd') `mod'=(`Modminus1sd' `Modplus1sd')) atmeans asbalanced plot (ytitle(`dv') xtitle(`iv') title(Interaction of `iv' and `mod' on `dv')) nose
*---- END CODE
However, this block of code can become repetitive when I have a lot of plots to graph, it takes up a lot of code space, and it makes my do files look messy. Is it possible to write a new program and create a new command that runs this code. For example, it would be great if this program could be written into a command like this that outputs the plot and the test of simple slopes:
modplot depvar indepvar modvar, atsd(1)
Can someone help me with writing/learning to write a new command that runs this code?
Thank you!
Comment