Announcement

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

  • plot only the coeficients of the interaction terms (possibly with coefplot)

    after a regression, I am trying to plot only the coefficients of the interaction terms.

    I was unable to do this using coefplot Here is a reproducible example and my attempted solutions:


    Code:
     
     sysuse auto, clear  reg price foreign i.turn foreign#i.turn  *this plots all coefficients:  coefplot,  *this drops _cons and foreign but not i.turn  coefplot, drop(i.turn _cons foreign )  *variations with keep also do not work  coefplot, keep(foreign#i.turn )

    OBS: Apologies for rendition of the code block. I don´t understand why the forum software is collapsing all the code lines into a single line
    OBS: I crossposted the question in StackOveflow, here. I this the audiences are somewhat disjunct but noting it here o avoid wasting peoples time.

  • #2
    Hello Lucas. I see you have one answer now on StackOverflow. But I can't escape the nagging feeling that your question may exemplify the XY problem. If the underlying question is how to explain the nature of the interaction to your audience, I speculate that you may be better off using -margins- and -marginsplot-.

    I'm also wondering how well the example you generated using the auto dataset represents your actual data. In particular, you treated variable turn as categorical in your toy example. But it ranges in value from 31 to 51, and therefore would ordinarily be treated as a quantitative variable. In that case, you might do something along these lines.

    Code:
    clear *
    sysuse auto
    summarize turn
    // Given the range of values, treat variable turn as quantitative
    scatter price turn if ~foreign || scatter price turn if foreign || ///
    lfit price turn if ~foreign || lfit price turn if foreign
    graph rename g1
    
    // Given the heterosceasticity evident in the scatterplot,
    // use the vce(robust) option for -regress-
    regress price foreign##c.turn, vce(robust)
    // Get the simple slopes for domestic & foreign cars
    margins, dydx(turn) by(foreign)
    margins foreign, at(turn=(30(5)50))
    marginsplot
    graph rename g2
    HTH.
    --
    Bruce Weaver
    Email: [email protected]
    Web: http://sites.google.com/a/lakeheadu.ca/bweaver/
    Version: Stata/MP 18.0 (Windows)

    Comment


    • #3
      The problem is that the variables are not called i.turn. Using the coeflegend option will show you the names.

      Here is one way to just plot the interactions:

      Code:
      sysuse auto, clear 
      reg price foreign i.turn foreign#i.turn , coeflegend
      levelsof turn, local(levels)
      foreach l of local levels {
          local mylist "`mylist' `l'.turn"
      }
      coefplot, drop(_cons foreign `mylist' )

      Comment


      • #4
        Hello,

        Here is the example of my data; the data in the example is sorted by location, so, in fact, it is related to only one household with location id 600001 (the original data is for many households over 2017-2018).

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input long location float(lconsum tp) byte randomgrp float(calday treatgr treat_numb_of_days)
        600001  4.342219 0 0 20820 0 -403
        600001  4.396476 0 0 20821 0 -402
        600001 4.4473995 0 0 20822 0 -401
        600001 4.4349075 0 0 20823 0 -400
        600001 4.3400753 0 0 20824 0 -399
        600001  3.974716 0 0 20825 0 -398
        600001 4.2170517 0 0 20826 0 -397
        600001 4.4074755 0 0 20827 0 -396
        600001 4.2367565 0 0 20828 0 -395
        600001 4.3245976 0 0 20829 0 -394
        600001 4.2221044 0 0 20830 0 -393
        600001 4.4336513 0 0 20831 0 -392
        600001  4.122668 0 0 20832 0 -391
        600001  4.443582 0 0 20833 0 -390
        600001 4.1282955 0 0 20834 0 -389
        600001  4.153299 0 0 20835 0 -388
        600001  4.019543 0 0 20836 0 -387
        600001 3.8549176 0 0 20837 0 -386
        600001  3.745078 0 0 20838 0 -385
        600001  3.776974 0 0 20839 0 -384
        end
        format %td calday
        location; household’s location id
        lconsum; log of energy consumption
        tp; post-treatment variable; gen tp = (calday >= td(08feb2018))
        randomgr; one of three treatment groups (can be 1,2,3, as well as 0 if it is a control group)
        calday; day and year 01jan2017
        treatgr; treatment indicator;
        gen treatgr = randomgrp if calday >= td(08feb2018)
        replace treat = 0 if treat ==.
        treat_numb_of_days; number of days before/after the treatment date; gen treat_numb_of_days = calday-td(08feb2018)

        I ran the following regression:
        areg lconsum treatgr tp i.treat#c.treat_numb_of_month, absorb(location) vce(cluster location)

        Now, I need to plot the estimated coefficient of the interaction term ‘i.treat#c.treat_numb_of_month‘ against the variable ‘treat_numb_of_month’. I tried to do this using the 'coefplot' command, but it did not work because I could not put my ‘treat_numb_of_month’ variable on the x-axis.

        How can I draw this graph?

        Thank you.

        Comment

        Working...
        X