Announcement

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

  • Confidence intervals margins plot: confusing

    Hi list,

    I have issues with the margins command and (too many) overlaping confidence intervals. As you might see from the picture, it is hard to tell what is actually happening at the right most side of the plot beneath. I have runned what is sometimes refered to as a "difference-in-differnce-in-differences" (http://www.nber.org/WNE/lect_10_diffindiffs.pdf) model and then I use margins to show linear predictions. My problem is that the confidence interval makes it look messy, and I can't tell them apart.



    Year is a time indicator [0;1], D is a treatment indicator [0;1] and dE [0;1 ]is the third variable that I'm interested in. (I want to see if the treatment is different for persons who have dE==0 and 1.)

    Code:
    reg outcome year##D##dE, vce(cluster identifyer) 
    margins, at (year=(0 1) D=(0 1) dE=(9 1)) 
    marginsplot, xline(0.3)
    My question is: is there a way to get Stata to estimate margins for year=0.98 for one group, or something like that - so I can visually distinguish the confidence intervals?

    When I try to do

    Code:
    reg outcome year##D##dE, vce(cluster identifyer) 
    margins, at (year=(0 0.98 1) D=(0 1) dE=(9 1)) 
    marginsplot, xline(0.3)
    I get a warning saying: "at level for factor year not present in estimation".

    I tried to do something like this:
    Code:
    reg outcome year##D##dE, vce(cluster identifyer) 
    predict Y
    replace year = 0.98 if dE==1 & year==1
    collpase Y, by(year D dE)
    gr two (connect Y year if D==0 & dE==0)///
              (connect Y year if D==0 & dE==1)///
              (connect Y year if D==1 & dE==0)///
              (connect Y year if D==1 & dE==1), xline(0.3)
    But the lines are not exactly the same as in the margins plot, and there is of course no confidence intervals at all.

    In other words, I would simply like to push e.g. the two dashed lines a little down the axis to better see what is actually happening in the plot.
    Click image for larger version

Name:	pred.png
Views:	1
Size:	43.9 KB
ID:	1527500

  • #2
    Check mplotoffset from SSC

    Comment


    • #3
      Hi Andrea,

      Thanks for you answer. My problem is that I'm working on protected serves without internet access so I cannot install new packages.

      Comment


      • #4
        You may also try to make a little distance in the x-axis between the values, just for the sake of providing a nice display.
        Best regards,

        Marcos

        Comment


        • #5
          Hi Marcos,

          Yes, that's exactly what I want to do, but I can't figure out how to do that

          Comment


          • #6
            In your code
            Code:
            reg outcome year##D##dE, vce(cluster identifyer)
            margins, at (year=(0 1) D=(0 1) dE=(9 1))
            marginsplot, xline(0.3)
            I believe that since you don't specify any prefix for any of the variables year, D, and dE, Stata interprets all three to be factor variables. This is why when you try
            Code:
            margins, at (year=(0 0.98 1) D=(0 1) dE=(9 1))
            it gives you the error. Since year is a factor variable it can only take values of 0 or 1.

            The simplest solution would, I think, be
            Code:
            reg outcome c.year##D##dE, vce(cluster identifyer)
            margins, at (year=(0 0.98 1) D=(0 1) dE=(9 1))
            marginsplot, xline(0.3)
            Here year is treated as a continuous variable. It shouldn't change the results of the regression, and when using margins it won't give you an error (I think). I don't know if that will get you what you're looking for, but one can only hope.
            Alfonso Sanchez-Penalver

            Comment


            • #7
              Hi Alfonso,

              Great idea! It does work to some extent. However, as both groups are also estimated at year=0.98 I get the same problem - is there a way to have one group not estimated at that time point?

              Comment


              • #8
                Should anyone stumble across this thread in the future, the solution is the package "combomarginsplot" together with treating year as a continous variable.

                Code:
                reg outcome c.year##D##dE, vce(cluster identifyer)
                marings, at (year=(0 0.98) D=(0 1) dE=(1)) saving(file1, replace)
                margins, at(year=(0 1) D=(0 1) dE=(0)) saving(file2, replace)
                combomarginsplot file1 file2, xline(0.3)

                Comment


                • #9
                  I'm still not quite clear on why you have different values for the years for the different groups. If it were the same wouldn't something like
                  Code:
                  reg outcome year##D##dE, vce(cluster identifyer)
                  margins dE, at (year=(0 1) D=(0 1))
                  marginsplot, xline(0.3)
                  work? My understanding is that by specifying a factor variable after margins you are asking to do groups of predictions for each value that the variable you specify takes.

                  For example
                  Code:
                  clear all
                  set more off
                  sysuse auto
                  reg price c.mpg##i.foreign##c.weight
                  margins foreign, at(mpg=(15 24) weight=(2800 3100))
                  marginsplot
                  leads to
                  Click image for larger version

Name:	Example.png
Views:	1
Size:	105.9 KB
ID:	1528311


                  The chosen weights don't give much of a space in the distinction, and at the upper mileage there doesn't seem to be significant differences between domestic and foreign vehicles, but you can see that this is more or less what you were looking for in that first attempt you showed.
                  Alfonso Sanchez-Penalver

                  Comment

                  Working...
                  X