Announcement

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

  • How to graph the moderating effect(moderator: dummy variable) of a quadratic model?

    Dear specialists,

    I really thank everyone here sparing no efforts to help me.

    I have a censored dataset and the regression is an inverted U-relationship.


    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(M control1 control2 control3 control4 control5 control6) int x double(y control7 control8)
    1 0 0 0 0 1 0   8 22.949106216430664   17.3120174407959 20.570825576782227
    0 1 1 1 0 1 0  18 22.806461334228516 16.588098526000977 18.744325637817383
    1 1 0 0 0 4 1   8 17.978151321411133 17.216707229614258  .6931471824645996
    0 0 0 1 0 1 0 123 16.005598068237305 16.300416946411133   18.9520320892334
    1 0 0 1 0 1 0   5 17.918004989624023 17.034385681152344 15.428786277770996
    0 1 0 1 1 1 0 346 24.482324600219727 17.399028778076172 20.355924606323242
    1 1 0 1 1 1 0   3 24.223512649536133 18.450239181518555 17.617250442504883
    0 0 0 0 0 1 0  96 23.566471099853516 16.811243057250977 16.955846786499023
    0 1 0 0 1 2 0 425  23.66777801513672  18.19753646850586 18.775419235229492
    0 0 0 1 0 1 0 106 23.174531936645508 17.370859146118164  16.89447021484375
    1 1 0 1 0 1 0   4 17.732799530029297 14.457364082336426 19.205141067504883
    1 0 0 1 0 1 1   3 15.683768272399902 14.457364082336426  .6931471824645996
    0 1 0 1 0 2 0  63 22.443294525146484   17.7275333404541  19.00855827331543
    1 0 0 0 0 2 0   8     18.52099609375  16.11809539794922 13.792720794677734
    0 0 1 1 0 1 0   9 13.517104148864746  19.04928970336914 18.136098861694336
    1 0 0 0 0 1 0   6 15.053014755249023 14.457364082336426 19.068098068237305
    0 0 1 0 0 1 0 697 23.785768508911133 17.504390716552734 19.758201599121094
    0 1 0 1 1 2 0  46 23.127647399902344 17.822843551635742 18.129066467285156
    1 0 0 0 0 2 0 113  17.74494171142578 14.508657455444336 14.646207809448242
    0 0 0 1 0 3 1 125  18.34654998779297 14.375125885009766 19.459447860717773
    0 1 1 1 1 1 0 107   26.6013240814209 19.806974411010742 21.029006958007813
    0 1 1 1 1 1 0 107 26.275054931640625   19.5192928314209 19.718734741210938
    1 0 0 0 0 1 0  11 21.269575119018555 13.815510749816895 18.251508712768555
    0 1 0 0 1 1 0   4  22.68794822692871 17.281246185302734 16.724597930908203
    0 0 0 1 0 1 0 105 14.990157127380371 13.815510749816895 15.718832015991211
    0 0 0 1 0 1 0 663 19.204071044921875 17.034385681152344 13.799907684326172
    1 0 0 1 0 1 0   4  16.33489990234375 14.508657455444336  17.44756507873535
    0 0 0 0 0 1 0   8  18.20665740966797 16.380460739135742  18.75150489807129
    1 0 0 1 0 1 0   3 13.334243774414063 14.457364082336426  19.18917465209961
    0 0 0 1 0 1 0 284  23.10700035095215 16.523561477661133 19.209861755371094
    0 1 0 1 1 2 0 284  22.16788673400879 15.424948692321777 14.695516586303711
    1 1 1 1 0 1 0   8 24.212265014648438 19.035865783691406 19.782316207885742
    0 0 0 1 0 1 0 455   17.6008358001709 16.811243057250977    9.2648286819458
    1 1 0 0 1 1 0   2 23.746435165405273  16.86003303527832 18.270660400390625
    1 0 0 1 0 2 0   5 19.581193923950195 17.281246185302734   18.4521541595459
    0 0 0 1 0 1 0   5  19.14850425720215  16.21340560913086 15.823354721069336
    0 0 1 0 0 1 0 106 22.485082626342773  16.11809539794922 17.567533493041992
    1 1 0 0 0 1 0   7 22.783226013183594 15.761420249938965 18.496137619018555
    0 0 0 1 0 1 0  11 16.819210052490234 13.527828216552734  16.94902992248535
    0 0 0 1 0 1 0 151  18.90007781982422  16.11809539794922 18.147491455078125
    1 0 0 0 0 1 0   8  21.86478614807129 17.034385681152344 18.850242614746094
    1 1 1 1 1 1 0   7 22.845659255981445  17.68671226501465 19.689342498779297
    1 1 0 1 1 1 0   6  23.92540740966797 18.132999420166016  20.34378433227539
    0 0 0 1 0 1 0  13 23.123905181884766 17.909854888916016  18.01799774169922
    0 0 0 1 0 3 0   3 18.898025512695313 15.761420249938965 18.995527267456055
    0 0 0 1 0 1 0   5 19.191537857055664  16.01273536682129 19.141666412353516
    0 1 1 0 1 1 0  42  24.29490089416504   17.7275333404541 19.795740127563477
    1 0 0 1 0 1 0   8 18.446407318115234 16.883563995361328 20.610980987548828
    0 0 0 0 0 1 0   5 17.829132080078125  16.01273536682129 19.647912979125977
    1 1 1 1 1 1 0   6 23.370441436767578 17.622173309326172 19.004106521606445
    0 1 0 1 1 1 0 175 23.807193756103516 18.980297088623047 20.575855255126953
    0 0 0 0 0 1 0 284 21.572847366333008 15.424948692321777 19.118602752685547
    0 0 0 1 0 1 0 109 18.238428115844727 15.068273544311523  18.99883270263672
    0 1 0 0 1 1 0   5 22.323518753051758 18.035018920898438 18.701475143432617
    1 1 1 1 1 1 0   4 24.551706314086914 18.951309204101563   18.1805477142334
    0 0 0 1 0 5 0  46  17.35716438293457 13.815510749816895  17.30387306213379
    0 0 0 0 0 1 1 279  20.36696434020996 15.894951820373535  .6931471824645996
    0 1 1 1 1 2 0  18     24.85302734375 19.113828659057617  19.06422233581543
    1 1 0 1 1 1 0   6  23.23371124267578 15.761420249938965  18.10226821899414
    1 1 1 1 1 1 0   8 24.375486373901367 17.822843551635742 19.248655319213867
    1 1 0 1 1 1 0   8 23.496232986450195 17.909854888916016 18.418119430541992
    0 0 0 0 0 1 0  58 18.395998001098633 15.068273544311523  17.63210678100586
    0 1 0 1 1 1 0   5  24.17245864868164 18.396387100219727 18.096141815185547
    0 0 0 0 0 1 0  40  15.38599967956543 14.508657455444336  9.139273643493652
    0 1 0 0 0 1 0  21 23.556793212890625 17.426427841186523 19.268308639526367
    0 1 1 0 1 1 0   6 24.420333862304688 16.811243057250977 19.697742462158203
    1 0 0 1 0 2 0   4 18.793746948242188 14.457364082336426 13.836281776428223
    1 1 0 0 1 1 0   2 23.786779403686523 16.811243057250977 17.747819900512695
    1 1 0 1 0 1 0   8  19.58517074584961 16.951004028320313 18.983076095581055
    1 0 0 0 0 1 0   5  21.72478675842285 16.811243057250977  18.40504264831543
    1 1 1 0 1 1 0   6 25.056909561157227  16.11809539794922  19.21035385131836
    0 1 1 0 1 1 0 284  23.98286247253418 15.424948692321777 19.062726974487305
    0 1 1 0 1 2 0 284  22.97431182861328  16.01273536682129 18.093008041381836
    1 0 0 0 0 1 0   8 18.126632690429688 14.508657455444336  19.24971580505371
    0 0 1 1 0 1 0 697  23.21192741394043   17.7275333404541  16.86979103088379
    1 0 0 0 0 1 0 135  23.33150863647461  16.11809539794922   19.8182315826416
    0 0 0 0 0 5 0 490   18.4140682220459 15.894951820373535 13.995794296264648
    0 . . . . . .   .                  .                  .                  .
    0 0 0 1 0 1 0 697 22.618450164794922 17.504390716552734  .6931471824645996
    0 0 0 1 0 1 0 161  24.22525405883789 16.845643997192383 19.180622100830078
    0 0 0 0 0 1 0  63  23.49774169921875 17.281246185302734 17.358951568603516
    0 0 0 1 0 1 0 663 19.229278564453125 16.300416946411133 16.765993118286133
    0 0 0 0 0 1 0  58 15.912301063537598 14.747674942016602  .6931471824645996
    0 1 1 0 1 1 0 284 24.110410690307617  16.11809539794922 12.027538299560547
    0 0 0 0 0 1 0  40 21.205873489379883 17.370859146118164 16.865760803222656
    1 0 0 1 0 1 0  56 17.881935119628906 13.815510749816895 17.762706756591797
    1 0 1 1 0 1 0   4  18.35368537902832  16.11809539794922  19.24974250793457
    0 0 1 0 0 1 0 777  23.53510284423828  16.11809539794922 17.638704299926758
    0 1 1 1 1 1 0 109 25.289718627929688 17.822843551635742 14.394180297851563
    1 0 0 1 0 2 0   8 17.924514770507813 17.676240921020508 19.183582305908203
    1 1 1 0 1 1 0  11 24.805057525634766 18.643823623657227 19.623146057128906
    1 1 1 0 1 1 0  11 24.311477661132813  18.31532096862793 18.698169708251953
    1 1 1 1 1 1 0  12 25.720643997192383 18.951309204101563 20.346450805664063
    0 1 0 1 1 1 0   6 18.496326446533203 17.034385681152344 17.243867874145508
    1 1 1 1 1 1 0   6 25.264881134033203   19.5192928314209  19.64662742614746
    0 0 0 1 0 1 0  40  17.51824378967285 14.457364082336426 18.340465545654297
    0 1 0 0 0 1 0 151 23.607288360595703 17.216707229614258 18.674285888671875
    0 1 0 0 1 1 0   9  23.45798683166504 16.811243057250977 17.900461196899414
    1 0 0 1 1 1 0   5 20.373798370361328 17.553180694580078  18.21268653869629
    1 0 0 0 0 1 0   2 22.999513626098633 17.504390716552734 16.867582321166992
    end
    ------------------ copy up to and including the previous line ------------------

    M is the moderator and my model is like: tobit y x x2 M Mx Mx2 control1 control2 control3 control4 control5 control6 control7 control8

    I want to study the effect of M on the turning point of the quadratic model(shifts to left or right). According to some literature, to test the moderating effect of M on X2, full specification means to include both term M × X and term M × X2 in the regression model. When testing the turning point shift, one should first use the full specification, but if the coefficient of M × X2 is not statistically different from zero, it is appropriate to drop the term M × X2. I dropped the three-way interaction and the X*M is significant. However, I don't know how to graph the original inverted U-relationship and the moderating effect of the turning point(M being 0 or 1) in STATA. I've read and tried some suggestions provided in similar posts about graphing the interaction term in the quadratic model; however, none of them seemed to work in my case.

    It would be great if somebody can help me. Thank you very much in advance!

    Julie
    .

  • #2
    You could start with two separate models as follows:

    Code:
    tobit y c.x##c.x control? if M == 0
    margins, at(x=(0(50)800))
    marginsplot, name(g1, replace)
    
    tobit y c.x##c.x control? if M == 1
    margins, at(x=(0(50)800))
    marginsplot, name(g2, replace)
    graph combine g1 g2, xcommon ycommon
    However, I noticed that the x values are quite different in both groups of M so I am not sure how much overlap there actually is. See
    Code:
    bysort M: sum x
    How do you handle this issue? Anyway, a pooled model could be done as follows:

    Code:
    tobit y c.x##c.x##i.M control?
    M#c.x and M#c.x#c.x are the interaction terms.

    Best wishes

    (Stata 16.1 MP)

    Comment


    • #3
      Thank you very much, Felix.

      Actually, I haven't noticed the issue about x values being different in both groups of M, I'll think about it.

      Another question, though the code is combine graphs g1, g2, it is still displayed in separate graphs. How can I get a graph like this?

      Click image for larger version

Name:	5.png
Views:	1
Size:	38.2 KB
ID:	1632654


      Thank you!

      Comment


      • #4
        Originally posted by Julie Li View Post
        it is still displayed in separate graphs. How can I get a graph like this?
        You don't have much in the way of a quadratic relationship in the snippet of data that you show, but you can get both plots on the same graph using an approach along the following modification of what Felix outlined above.
        Code:
        regress out i.M##c.x##c.x i.(control1-control6) c.(control7 control8)
        
        quietly centile x, centile(10(10)90)
        local centile_list
        forvalues i = 1/9 {
            local centile_list `centile_list' `=r(c_`i')'
        }
        
        quietly margins M, at(x=(`centile_list'))
        
        marginsplot , xdimension(x) plotdimension(M) ///
            xlabel(0(50)800) ylabel( , angle(horizontal) nogrid) ///
                plotopts(msymbol(none)) title("") noci legend(off)

        Comment


        • #5
          Thank you, Joseph.

          I tried your code and they are shown on the same graph!

          Those two lines cannot work:

          xlabel(0(50)800) ylabel( , angle(horizontal) nogrid) ///
          plotopts(msymbol(none)) title("") noci legend(off)

          I tried several times and STATA returned errors: command plotopts is unrecognized. command xlabel is unrecognized. I also installed regaxis, but it seems not working.

          Comment


          • #6
            Run the code in a do-file, or eliminate the line breaks "///" if copying and pasting into the command window.

            Code:
            marginsplot , xdimension(x) plotdimension(M) xlabel(0(50)800) ylabel( , angle(horizontal) nogrid) plotopts(msymbol(none)) title("") noci legend(off)

            Comment


            • #7
              Thank you for your help, Andrew.

              Yes, I eliminated the "///" when I was running those lines.

              I re-run it in a do-file, it still returns: command xlabel is unrecognized(also for the next line, command plotopts is unrecognized). I saw another post that said that sometimes there is a blank space before xlabel, and STATA cannot recognize it. I delete the blank space and make sure it is fine. I'm very confused about it now.

              Comment


              • #8
                The following runs in a do-file and replicates Joseph's marginsplot code. Does it work for you?

                Code:
                webuse nhanes2
                regress bpsystol agegrp##sex##c.bmi
                margins agegrp, over(sex) at(bmi=(10(10)60))
                marginsplot , xdimension(agegrp) plotdimension(sex) ///
                    xlabel(1(1)6) ylabel( , angle(horizontal) nogrid) ///
                        plotopts(msymbol(none)) title("") noci legend(off)

                Comment


                • #9
                  Yes, It works. I figured out the problem. Thank you very much!!!!

                  Comment

                  Working...
                  X