Announcement

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

  • Multilevel Modelling and interaction terms

    Hello! I am running a multilevel mixed model and I need to plot two way and three interactions. I tried this syntax but it didn't work:
    margins x1 dydx( x2 ) at(x2 = (1(1)7)) => x2 is on a scale of 0 to 7 and can be a decimal number.

    Below are the steps that I took to plot the interactions:
    1) create a new variable for each interaction (not using the "##" sign)
    2) mean center x1 by country. x2 is categorical
    2) run the mixed model with this code: xi: xtmixed y i.x1 c.x2 x3 x4 x5 x6 ||country: x1, cov(unstructured) mle var vce(robust)
    4)margins x1 dydx( x2 ) at(x2 = (1(1)7))

    I am getting an error : r(111) that x2 is ambiguous abbreviation (although it's not).
    Please advise,
    Thank you,

  • #2
    You are showing us pseudo code, and it is evident that these do not reflect the commands that you ran as other errors would have emerged. Here are some notes.

    1. xtmixed was superseded by mixed.
    2. Do not use the -xi- prefix if you intend on running the margins command.
    3. mixed supports factor variables.
    4. You need a comma to separate options from the main command.
    5. Check whether some variables are prefixed by other variables, e.g., media and mediator.

    Then you want something like:

    Code:
    mixed y i.x1 c.(x2 x3 x4 x5 x6) ||country: x1, cov(unstructured) mle vce(robust)
    margins x1, at(x2 = (1(1)7))
    where there is no other variable that has the prefix "x2" in the dataset (other than "x2" itself).
    Last edited by Andrew Musau; 19 Dec 2022, 14:32.

    Comment


    • #3
      To Andrew Musau's excellent advice I would add two more things, the first prompted by
      create a new variable for each interaction (not using the "##" sign)
      You cannot get correct results from -margins- when you make your own interaction variables. The # or, better, ##, operators are required for -margins- to work correctly. So you are, in a way, lucky that -margins- gave you an error message (for reasons unrelated to this) because otherwise you might have mistakenly believed the incorrect results it would have shown.

      As for the error message you are getting about x2 being ambiguous, run -describe x2*- and you will see what variables Stata are causing Stata to find x2 ambiguous.

      Comment


      • #4
        Thank you Andrew Musau can you please elaborate on point number 5" Check whether some variables are prefixed by other variables, e.g., media and mediator"? I didn't get it, sorry for the hassle. Clyde Schechter thanks again, when I run the command "describe" I get the type of data, I tried changing it from "float" to "int" but I kept getting the same error.

        Comment


        • #5
          Exactly as Clyde proposes in #3. Suppose that in the auto dataset, I have 2 variables, weight and weightless. Both have the prefix "weigh". If I run the following:

          Code:
          sysuse auto, clear
          rename turn weightless
          regress mpg i.rep78 c.(weight disp trunk weightless)
          margins rep78, at(weigh=(1000(1000)3000))
          I get

          Code:
          . margins rep78, at(weigh=(1000(1000)3000))
          weigh ambiguous abbreviation
          r(111);
          Clyde's proposal is that you take the abbreviation and add an asterisk then run describe to find all variables that include such a prefix. In my case:

          Code:
          desc weigh*
          gives me

          Code:
          . desc weigh*
          
                        storage   display    value
          variable name   type    format     label      variable label
          -----------------------------------------------------------------------------------------------------------------------------------------------
          weight          int     %8.0gc                Weight (lbs.)
          weightless      int     %8.0g                 Turn Circle (ft.)
          
          .

          Comment


          • #6
            Andrew Musau Oh gotcha! Thank you! I will apply what you and Clyde Schechter suggested and circle back in case I need further assistance! I appreciate you!

            Comment


            • #7
              Clyde Schechter Andrew Musau Hello so I tried the below and I got a table but no plot:

              mixed y i.x1 i.x2 i.x3 c.centered_x4 i.x1##c.centered_x4 i.x5 i.x6 x7 x8 x9 x10 x11 ||country:x1, cov(unstructured) mle var vce(robust) ,level(90)

              margins blgetmg, at(centered_gbge04 = (1(1)7))

              Output:

              ​​​​​​​
              Delta-method
              Margin std. err. z P>z [90% conf. interval]

              _at#x1
              1#No .4140513 .2080679 1.99 0.047 .07181 .7562925
              1#Yes .2863126 .2404715 1.19 0.234 -.1092277 .681853
              2#No .2499228 .1287872 1.94 0.052 .0380868 .4617589
              2#Yes .1061396 .1531765 0.69 0.488 -.1458133 .3580925
              3#No .0857944 .0578093 1.48 0.138 -.0092934 .1808822
              3#Yes -.0740334 .0713492 -1.04 0.299 -.1913924 .0433255
              4#No -.078334 .0596623 -1.31 0.189 -.1764697 .0198017
              4#Yes -.2542065 .0520531 -4.88 0.000 -.3398262 -.1685867
              5#No -.2424624 .131298 -1.85 0.065 -.4584284 -.0264965
              5#Yes -.4343795 .1277478 -3.40 0.001 -.6445059 -.2242531
              6#No -.4065909 .2106671 -1.93 0.054 -.7531074 -.0600743
              6#Yes -.6145526 .214291 -2.87 0.004 -.9670298 -.2620753
              7#No -.5707193 .2915202 -1.96 0.050 -1.050227 -.0912113
              7#Yes -.7947256 .302511 -2.63 0.009 -1.292312 -.2971393

              Comment


              • #8
                The graphing command for margins is marginsplot.

                Code:
                margins blgetmg, at(centered_gbge04 = (1(1)7))
                marginsplot

                Comment


                • #9
                  Andrew Musau thank you so much!!
                  One last question, what can I add to the above code to plot a three-way interaction? I have a two-way interaction (that you just helped me figure out) and a three-way interaction in my model.

                  Comment


                  • #10
                    This may help: https://stats.oarc.ucla.edu/stata/fa...tion-stata-12/. I have seen that some people use contour plots or 3D graphs, but I do not find any of these intuitive or easy to understand. Presenting several plots is an alternative.

                    Comment

                    Working...
                    X