Announcement

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

  • Plotting regression line for for different levels of a modertaing variable in a log-regression with interaction term

    I have a simple bivariate regression model with an interaction term:

    reg DP ln(IV) MOD ln(IV)*MOD

    I want to plot the regression line for different values of MOD, to look something like the below:
    Click image for larger version

Name:	Screenshot 2023-08-04 133003.png
Views:	2
Size:	23.3 KB
ID:	1722868






    I have tried to do this with margin and marginsplot commands, but I only get linear results (see attachment)



    Code:
    reg povertyline logdeficit gini interact_gini
    margins, at(gini=(30 36 42))
    marginsplot

    I would be grateful for your help!

    Kind regards,
    Jakob

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float iso3n double gini float(logdeficit interact_gini)
    926                 29          .          .
    499               36.9          .          .
     36                 34   3.330491   113.2367
    554                  .   2.964826          .
     52                  .          .          .
    136                  .          .          .
    214               42.2  2.3475068   99.06479
    780                  .  3.2702994          .
    662                  .          .          .
    308                  .          .          .
    212                  .          .          .
    670                  .          .          .
    659                  .          .          .
    533                  .          .          .
    332                  .          .          .
     28                  .          .          .
    850                  .          .          .
    192                  .          .          .
    630                  .          .          .
    388                  .   2.432784          .
     44                  .          .          .
    591               49.9   2.499323  124.71622
    222                 38   1.840707   69.94687
    340               49.4  1.6871662   83.34601
    188               48.3  2.2543366  108.88446
    320                  .   1.407686          .
    558                  .  1.7681222          .
    484 47.200000000000045  2.5772305  121.64528
     84                  .          .          .
    860                  .          .          .
    398               27.5   3.038216   83.55094
    795                  .          .          .
    762                  .   2.036664          .
    417               27.3   2.340101   63.88476
    450  43.09999999999985   .9080942   39.13886
    834  40.11428571428564   .7790053   31.24924
    262               41.6          .          .
    716               44.3  1.8018782    79.8232
    480               36.8    2.52604   92.95828
    706                  .          .          .
    231  35.72000000000003   1.251808   44.71458
    232                  .          .          .
    646  43.23333333333346   .6104927  26.393635
    800 42.766666666666666  -.3747015 -16.024733
    894  57.70000000000016  1.0381508    59.9013
    174                  .          .          .
    108                  .          .          .
    690  35.04000000000087          .          .
    508                  .  1.3518612          .
    404                  .   1.290814          .
    454  42.63333333333321 -2.3025696  -98.16622
    446                  .          .          .
    408                  .          .          .
    156               39.1  2.7687166  108.25682
    410  31.50000000000003  3.1917524   100.5402
    496               32.5   2.960529   96.21719
    344                  .   3.112752          .
    392  33.96666666666624  3.1168535  105.86913
    203               24.9  3.1798506   79.17828
    804                 26   2.725242   70.85629
    642                 36   2.639316   95.01538
    498               25.9          .          .
    100               40.4  2.7824554   112.4112
    348               30.6  2.8531375   87.30601
    112               25.4   2.814378    71.4852
    810               37.2   3.039144  113.05617
    616               29.7   2.986573   88.70123
    703               23.2   2.929394   67.96194
    598                  .          .          .
    242  32.69999999999982          .          .
     90                  .          .          .
    548 33.433333333333394          .          .
    296                  .          .          .
    583                  .          .          .
    520                  .          .          .
    316                  .          .          .
    585                  .          .          .
    584                  .          .          .
    180                  .          .          .
    266                 38          .          .
    178                  .          .          .
    148  38.32857142857142          .          .
    226                  .          .          .
     24                  .          .          .
    678               40.7          .          .
    120                  .  1.4908074          .
    140                  .          .          .
    788  31.59999999999968  2.2922888  72.436325
    504                  .   1.976289          .
    434                  .          .          .
    728                  .          .          .
    736                  .          .          .
    818               31.5   1.990596   62.70378
     12                  .          .          .
    124               33.3   3.306146  110.09466
    840               41.2   3.423671  141.05525
    304                  .          .          .
     60                  .          .          .
    578                 27   3.093841   83.53371
    208               28.7   3.084956   88.53824
    end
    Attached Files
    Last edited by Jakob Abekhon; 04 Aug 2023, 07:08. Reason: I edited this to properly include code and data, as I am new to the forum. Thank you for your understanding

  • #2
    If you want to use margins you have to use the factor variable notation for the interaction variable and not create it yourself. See help fvvarlist
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thank you for your answer!

      However, when I put in the following code

      Code:
       reg povertyline logdeficit gini c.logdeficit#c.gini
      margins logdeficit gini c.logdeficit#c.gini , at(gini=(30  36 42))
      I receive the error message:
      only factor variables and their interactions are allowed
      r(198);

      I'm sorry if these are very simple problems. I am a complete beginner.

      Thank you for your help!

      Comment


      • #4
        Code:
        sysuse auto, clear
        reg price rep78 mpg c.rep78#c.mpg
        margins, at(mpg = (10(2)40) rep78 = (1 2 3 4 5))
        marginsplot

        Comment


        • #5
          Thank you very much! This worked!

          Comment

          Working...
          X