Announcement

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

  • How to Plot the interaction of two continuous variables after regression.

    Hi All,
    Im doing a simple linear regression. It has main and interaction terms, and some covariates:

    regress LUF_AD_std c.d3_SympADHD_std##c.c3_c4_rs_std i.ChildGender c3_childage WB_AD_std

    I have run the same model using dichotomous variables (d3_SympADHD_std in the categories ADHD or NO ADHD; and c3_c4_rs_std to say YES or NO the if the children have langauge disorders). When I do the dichotomous variables, I can use the margins and marginsplot function to get a graphical view of the main and interaction terms.

    But when I use the continuous variables, I can't figure out what the command is to plot the interaction. I've tried searching for this, but all I get is long-winded and rather complicated explanations of how to do this for 'particular levels of the IV' e.g. https://www.youtube.com/watch?v=QFROtui_OyM

    I would have thought this was a simple command. SPSS provides a plot of the interaction term for continuous variables. Why is it so hard to find the equivalent command in stata?

    Can anyone please help with this?

    Many thanks
    Hannah

  • #2
    So, when you are working with categorical variable interactions, it goes without saying that the salient values for the indicators are 0 and 1. Stata knows that and handles it for you. But for continuous variables, it is not obvious what the appropriate range of values to use in displaying the interactions is. So Stata leaves that to you to specify.

    It's really not hard. Suppose the variable d3_SympADHD_std ranges from, say about 0 to 5, and c3_c4_rs_std ranges from about 10 to 50. Then you might want to fill the ranges of values with some evenly spaced values. How many: it depends on how detailed a plot you want. But something like:
    Code:
    margins, at(d3_SympADHD_std = (0(1)5) c3_c4_rs_std = (10(10)50))
    marginsplot
    would probably get you most of what you're looking for. If you want a smoother, more detailed graph, fill in more points, say using (0(0.5)5) and (10(5)50). The tradeoff for more detail is that there is more calculation time before plotting. While it makes the command a bit more complicated to use, this approach allows you to focus your efforts on the ranges of values of those variables that you find most salient.

    Comment


    • #3
      Hi Clyde,
      Thank you for your help! That makes sense to me. However when I put the information in my resulting graph isn't what I expected. E.g.

      margins, at(d3_SympADHD = (0(1)18) c3_c4_rs = (0(1)-34))

      (ADHD is out of 18 symptoms, and the langauge means has 34 items). To me, its seems intuitive to go in intervals of 1 unit. The resulting graph was:
      Click image for larger version

Name:	Screen Shot 2016-12-05 at 2.44.18 pm.png
Views:	1
Size:	166.9 KB
ID:	1366737



      Im unsure where Im going wrong here!
      Attached Files

      Comment


      • #4
        Whoops please ignore the bottom image. I thought I deleted that before posting

        Comment


        • #5
          The problem is that because you have so many values, the legend became enormous and squashed the graph to oblivion. You can "thin out" the graph, by for example going
          at (0(6)18) and (0(10)30 34) to get fewer points. Or you can keep the detail you have and use the -legend(off)- option with -marginsplot-. The latter approach will give you the graph and suppress the legend altogether. It'll be hard to tell which line goes with which value of c3_c4_rs without a legend, but at least you'll see the whole graph.

          Comment


          • #6
            Thank you so much for your help Clyde!!

            Comment


            • #7
              Dear all, I need you help. I have read previous messages but still I am stacked. I want to plot two way interaction effect from Stata. what is the command for doing this? Let say i have age as the first variable, then education the second variable and lastly the interaction variable which is age*education

              Comment


              • #8
                Something like this. I assume age and education are both continuous variables. I assume age ranges from 20 to 70 and education ranges from 5 to 25 -- for purposes of illustration.

                Code:
                regression_command outcome_variable c.age##c.education perhaps_other_covariates, perhaps_some_options
                margins, at(age = (20(10)70) education = (5(5)25))
                marginsplot, xdimension(age) name(outcome_by_age, replace)
                marginsplot, xdimension(education) name(outcome_by_education, replace)
                
                margins, dydx(age) at(education = (5(5)25))
                marginsplot, name(age_marg_effect_by_educ, replace)
                margins, dydx(education) at(age = (20(10)70))
                marginsplot, name(educ_marg_effect_by_age, replace)
                Replace all italicized material by material appropriate to your data and problem.
                The four graphs that will be available at the end of this code are all types of interaction plots. Which one(s) you want depends on your research goal(s) and audience. You may find all of them useful, or perhaps only some are relevant for you.

                Comment


                • #9
                  thank you prof for you quick and useful response but one more clarification. Between the range you have used for illustration there is number, what does this stand for (or how do you get them from these ranges) e.g between 20 and 70 there 10 and between 5 and 25 there 5

                  Comment


                  • #10
                    See -help numlist-. This kind of notation is used all over the place in Stata.

                    Comment

                    Working...
                    X