Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • IF commands based on p-values and nature of variable (e.g., continuous or categorical)

    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).
    reg y c.x1##c.x2
    If 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)
    matrix list 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:
    if the value contained in location [3,4] of the the matrix table < .05 {
    All the simple slopes code I want to run
    }
    Here is where I am stuck, as I don't know how to call that specific value from within the matrix.


    (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 {
    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

    I'm sure that there are simple commands that I'm overlooking. Any help would be appreciated.

  • #2
    for your first question, try (after you have the matrix called "table"
    Code:
    local pval = el(table,4,x)
    then use the local in your "if"; replace "x" with the correct column number

    however, I find some of your first question very confusing; what can "location[3,4]" mean as the p-value is in row 4?

    for your second question, what is your criterion for deciding whether a variable is "continuous" or "categorical"? maybe the number of distinct values?

    Comment


    • #3
      Thank you very much for your prompt response. Apologies on the "location [3,4]" -- that was a typo. I meant [4,3], as indeed the p-value is in the 4th row (and 3rd column) of the matrix.

      (1)

      I did try the following code as you suggested, i.e.:

      Code:
      matrix table = r(table)
      matrix list table
      local pval = e1(table,4,3)
      I followed this with the "if" function, i.e.:

      Code:
      if pval < .05 {
          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, I get the following error message: "pval not found r(111);" ---> Not sure why, as the code does seem to be correct.


      (2) Regarding my second question, in my dataset I consider my variables that are on a scale of 1 (strongly agree) to 5 (strongly agree) to be "continuous" -- these have 5 categories. However, my "race" and "occupation" variables each have manymore than 5 categories, but these are not a continuous scale and are distinctly categorical. Further, I have dichotomous / binary variables (taking on values of either 0 or 1). Thus, ideally, the best solution would be flexible enough to allow for other options as I see fit as a researcher. Perhaps there is some way of labeling / tagging the variables into these categories -- e.g., "continuous," "categorical," and "binary", and then creating the IF function based off of those tags?




      Comment


      • #4
        (1) if that you are asking about is a command, not a function (title is right on this, but not your text in #3). In Stata the two are disjoint.

        Code:
        if `pval' < 0.05
        is what you need as you want the local macro to be examine. pval so named would be a variable or scalar.

        (2) You need to write your own code for this.

        Comment


        • #5
          Nick, thank for you clarifying on both.


          (1) Great, makes sense - it works!

          (2) Okay, good to know that's what would be required here.


          Appreciate everyone's help on these questions.

          Comment

          Working...
          X