I have two related questions regarding the use of IF commands in STATA.
(1) IF Commands based on p-values
Let's say I have a simple regression command that is computing an interaction term between an IV (x1) and a Moderator variable (x2).
However, as I am running these commands within loops in Stata, to avoid excessive and unnecessary computations, I want Stata to ONLY compute the simple slopes IF the interaction term in the regression above is significant -- i.e. only IF the p-value in the regression output for c.x2##c.x3 is less than 0.05.
Ideally, my resulting command would look something like this:
Normally, one would do this by calling the desired scalarfrom the regression (in this case, the p-value) using "ereturn list". However, unfortunately Stata does not appear to store the p-value as a scalar.
Another option might be to grab the p-value from the matrixfor the equation. In this case, after running the initial regression I would type in:
(2) IF Commands based on nature of variable (continuous or categorical)
Relatedly, as the code for computing simple slopes will vary slightly depending upon whether the moderator variable is continuous or categorical, I want to tell Stata:
(1) IF Commands based on p-values
Let's say I have a simple regression command that is computing an interaction term between an IV (x1) and a Moderator variable (x2).
reg y c.x1##c.x2If the interaction term above (c.x1##c.x2) is significant, it would warrant a probing of the simple slopes using the code below.
qui sum x2 if e(sample) == 1
local atmodlo = r(mean)-r(sd)
local atmodhi = r(mean)+r(sd)
margins, dydx(x1) at(x2=($atmodlo $atmodhi)) vsquish
margins, dydx(x1) at(x2=($atmodlo $atmodhi)) vsquish pwcompare(effects)
However, as I am running these commands within loops in Stata, to avoid excessive and unnecessary computations, I want Stata to ONLY compute the simple slopes IF the interaction term in the regression above is significant -- i.e. only IF the p-value in the regression output for c.x2##c.x3 is less than 0.05.
Ideally, my resulting command would look something like this:
if p-value from the regression above < .05 {
All the simple slopes code I want to run
}
Normally, one would do this by calling the desired scalarfrom the regression (in this case, the p-value) using "ereturn list". However, unfortunately Stata does not appear to store the p-value as a scalar.
Another option might be to grab the p-value from the matrixfor the equation. In this case, after running the initial regression I would type in:
matrix table = r(table)This shows that the p-value for the interaction term is in 3rd column, 4th row of the table. So, ideally I would say something like:
matrix list table
if the value contained in location [3,4] of the the matrix table < .05 {Here is where I am stuck, as I don't know how to call that specific value from within the matrix.
All the simple slopes code I want to run
}
(2) IF Commands based on nature of variable (continuous or categorical)
Relatedly, as the code for computing simple slopes will vary slightly depending upon whether the moderator variable is continuous or categorical, I want to tell Stata:
if variable x2 is continuous {I'm sure that there are simple commands that I'm overlooking. Any help would be appreciated.
The specific simple slopes code I want to run
}
else if variable x2 is categorical {
The specific simple slopes code I want to run
}
end
Comment