Announcement

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

  • Marginsplot dimension errors

    Hello,

    Margins works fine for me but marginsplot does not. I would like some advice on a way to meaningfully plot the marginal effects I would like to plot, or on whether my entire approach is ill-conceived. Details below:

    I am running a fixed effects regression, treating my id variable and time variable as categorical, but also including an interaction between a continuous version of the time variable and the id variable.

    I removed a couple variables that might otherwise clutter this. I'm also using a lagged variable and interacting it with two other variables.

    Here's my model:
    Code:
    xtreg $depvar c.l.SWIndex##c.PosEmpGrowth c.l.SWIndex##c.NegEmpGrowth TotalEmp i.Year i.id#c.Year ,vce(robust) fe i(id)
    I run this margins command, which works fine and appears to do what I want, which is to estimate the average marginal effect at PosEmpGrowth = 0 (so the marginal effect only where the change is negative; I'm trying to permit asymmetric interactions of SWIndex and changes in Emp) and at various other values.

    Code:
    margins ,dydx(L.SWIndex) at(PosEmpGrowth = (0(5000)200000))
    It gives me normal-looking margins output (not pasting the entire thing here because it is so long).

    First snippet:
    Code:
    Average marginal effects                        Number of obs     =     40,695
    Model VCE    : Robust
    
    Expression   : Linear prediction, predict()
    dy/dx w.r.t. : L.SWIndex
    
    1._at        : PosEmpGrowth    =           0
    
    2._at        : PosEmpGrowth    =        5000
    Second snippet:
    Code:
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |      dy/dx   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
    L.SWIndex    |
             _at |
              1  |  -1.362592   .7528628    -1.81   0.070    -2.838176    .1129921
              2  |   -1.19614   1.103605    -1.08   0.278    -3.359165    .9668854
              3  |  -1.029688   1.595174    -0.65   0.519    -4.156171    2.096796
              4  |  -.8632358   2.132302    -0.40   0.686    -5.042472       3.316
              5  |  -.6967838   2.687814    -0.26   0.795    -5.964802    4.571235
    I then request a marginsplot and receive this return message:

    Code:
    . marginsplot
    invalid at() dimension information;
    using variable Year as a factor variable and a regular variable is not supported
    r(322);

    I understand what it means when it says that treating a variable as a factor variable and a regular variable is not supported (I am indeed doing this with Year), but I do not know why this is the case and would like to know whether there is a way to accomplish what I would like to accomplish, which is to visualize the average marginal effect, other than exporting it to Excel and doing it myself. Or, am I misunderstanding something, and is the whole idea of what I'm trying to do ill-conceived?

  • #2
    I have the same error message, my issue is a bit different. I have a continuous variable (time) and a dummy (gender). the model is:
    reg Y c.time##gender

    I then use
    margins, at(time=(0(5)50)) over(gender)

    which estimates without issues

    but then marginsplot gives me the same error you get:
    invalid at() dimension information; using variable gender as a factor variable and a regular variable is not supported

    Comment


    • #3
      I cannot replicate your problem (in either Stata 13 or Stata 14). What version of Stata are you using? Is it up-to-date?

      Does the following code produce an appropriate graph for you?

      Code:
      *create some data
      clear
      set obs 1000
      egen time=seq(), f(0) t(50)
      gen sort=rnormal()
      sort sort
      drop sort
      egen gender=seq(), f(1) t(2)
      gen Y=rnormal(2.5)
       
      *estimate
      reg Y c.time##gender
      margins, at(time=(0(5)50)) over(gender)
      marginsplot
      Last edited by Carole J. Wilson; 31 Mar 2016, 10:34. Reason: Confirmed to work in Stata 13 & Stata 14
      Stata/MP 14.1 (64-bit x86-64)
      Revision 19 May 2016
      Win 8.1

      Comment


      • #4
        Carole's code works on my setup as well. However, there is, a key distinction between her example and Jimmy's. Jimmy's regression command includes the same variable, year, both as a continuous variable in an interaction and as a discrete variable. His regressors include both i.id#c.year and i.year. When I modify Carole's code in an analogous way, it no longer runs, but I get an error message out of -margins-, and I don't even get to -marginsplots-:

        Code:
        *estimate
        reg Y c.time##gender i.time
        margins, at(time=(0(5)50)) over(gender)
        marginsplot
        which gives me:

        Code:
        . *estimate
        . reg Y c.time##gender i.time
        note: 50.time omitted because of collinearity
        
              Source |       SS           df       MS      Number of obs   =     1,000
        -------------+----------------------------------   F(52, 947)      =      1.33
               Model |  64.0091549        52  1.23094529   Prob > F        =    0.0634
            Residual |  877.992964       947  .927130901   R-squared       =    0.0680
        -------------+----------------------------------   Adj R-squared   =    0.0168
               Total |  942.002118       999  .942945063   Root MSE        =    .96288
        
        -------------------------------------------------------------------------------
                    Y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
        --------------+----------------------------------------------------------------
                 time |  -.0016335    .006558    -0.25   0.803    -.0145033    .0112363
             2.gender |  -.0614016      .1234    -0.50   0.619    -.3035706    .1807674
                      |
        gender#c.time |
                   2  |   .0000291   .0042708     0.01   0.995    -.0083522    .0084104
                      |
                 time |
                   1  |  -.3000008   .3036562    -0.99   0.323    -.8959177    .2959162
                   2  |   .0210072   .2986602     0.07   0.944    -.5651052    .6071195
                   3  |   .0832193   .2960396     0.28   0.779    -.4977503    .6641888
                   4  |  -.5756708    .293343    -1.96   0.050    -1.151348    6.65e-06
                   5  |   .3354331   .2910126     1.15   0.249     -.235671    .9065372
                   6  |  -.1482657   .2883834    -0.51   0.607    -.7142101    .4176788
                   7  |  -.4842141   .2856824    -1.69   0.090    -1.044858    .0764297
                   8  |  -.3315013   .2834515    -1.17   0.242    -.8877671    .2247644
                   9  |  -.3744911   .2820136    -1.33   0.185     -.927935    .1789528
                  10  |  -.0183361   .2796949    -0.07   0.948    -.5672296    .5305574
                  11  |   .4654346    .279736     1.66   0.096    -.0835395    1.014409
                  12  |  -.2755579   .2762475    -1.00   0.319     -.817686    .2665701
                  13  |  -.1864531   .2740364    -0.68   0.496    -.7242418    .3513356
                  14  |  -.2386098   .2727535    -0.87   0.382     -.773881    .2966615
                  15  |  -.2725243   .2712118    -1.00   0.315    -.8047699    .2597213
                  16  |  -.4109441   .2703559    -1.52   0.129    -.9415101    .1196219
                  17  |  -.4785374   .2689455    -1.78   0.076    -1.006335    .0492606
                  18  |   .2410593   .2680746     0.90   0.369    -.2850296    .7671482
                  19  |  -.2534731   .2672299    -0.95   0.343    -.7779043    .2709582
                  20  |  -.2091459   .2665258    -0.78   0.433    -.7321953    .3139036
                  21  |  -.5161031   .2659619    -1.94   0.053    -1.038046    .0058398
                  22  |  -.5968141   .2657507    -2.25   0.025    -1.118343   -.0752858
                  23  |   .0372468   .2652668     0.14   0.888     -.483332    .5578256
                  24  |   .2782454   .2651878     1.05   0.294    -.2421782     .798669
                  25  |  -.0167919   .2653437    -0.06   0.950    -.5375216    .5039377
                  26  |  -.0596997   .2653294    -0.23   0.822    -.5804012    .4610019
                  27  |  -.0492442   .2659076    -0.19   0.853    -.5710805     .472592
                  28  |   -.175964   .2662574    -0.66   0.509    -.6984868    .3465589
                  29  |  -.2571504   .2667578    -0.96   0.335    -.7806552    .2663544
                  30  |    -.05675   .2673535    -0.21   0.832    -.5814238    .4679239
                  31  |   .1451483   .2729961     0.53   0.595    -.3905989    .6808954
                  32  |  -.3670708   .2736732    -1.34   0.180    -.9041467    .1700052
                  33  |   .1142004   .2748781     0.42   0.678    -.4252404    .6536411
                  34  |  -.1664695   .2762803    -0.60   0.547    -.7086618    .3757228
                  35  |   -.400635   .2774227    -1.44   0.149    -.9450693    .1437992
                  36  |  -.2026621   .2790599    -0.73   0.468    -.7503094    .3449853
                  37  |   .1957949   .2811377     0.70   0.486    -.3559299    .7475197
                  38  |  -.3549081   .2824893    -1.26   0.209    -.9092856    .1994693
                  39  |  -.1233839    .284597    -0.43   0.665    -.6818975    .4351298
                  40  |  -.4518128   .2862044    -1.58   0.115    -1.013481    .1098554
                  41  |  -.1946423     .28855    -0.67   0.500    -.7609136     .371629
                  42  |  -.3164802   .2910507    -1.09   0.277    -.8876591    .2546988
                  43  |   .0088435   .2947646     0.03   0.976    -.5696239    .5873109
                  44  |  -.2300418   .2956495    -0.78   0.437    -.8102456     .350162
                  45  |  -.4546209   .2982343    -1.52   0.128    -1.039897    .1306556
                  46  |  -.0536006   .3006651    -0.18   0.859    -.6436475    .5364463
                  47  |  -.3035293   .3056408    -0.99   0.321    -.9033409    .2962822
                  48  |  -.4781279   .3066052    -1.56   0.119    -1.079832    .1235762
                  49  |  -.5568635   .3099219    -1.80   0.073    -1.165077    .0513495
                  50  |          0  (omitted)
                      |
                _cons |   2.713557   .2208913    12.28   0.000     2.280064     3.14705
        -------------------------------------------------------------------------------
        
        . margins, at(time=(0(5)50)) over(gender)
        time ambiguous abbreviation
        r(111);
        
        end of do-file
        I can see where -margins- might find this confusing. On the other hand, it is reasonable for a model to include both a linear time trend and yearly "shocks", and it is also reasonable to want predicted values based on that model. It isn't clear to me how to work around this, and I'm thinking of this as a bug in -margins-.

        By the way, to be clear: the data set being used for the above analysis is the same one that Carol created--I redacted that part of the code for brevity. There are no other variables whose names start with time.

        Comment


        • #5
          My issue is not with data on my personal station but rather at work. I'll have output by Monday

          Comment


          • #6
            OK, so I narrowed the problem down a bit.
            say I have a dummy variable gender, and a continuous variable age

            I'm running 3 identical models (except for the syntax of the command):
            Code:
            reg Y gender age gender#c.age
            reg Y i.gender age i.gender#c.age
            reg Y gender##c.age
            reg Y i.gender##c.age
            After each model I run the following margins command:
            Code:
            margins, at(time=(0(5)50)) over(gender)
            All models return the same estimated margins, but the 1st model cannot do marginsplot and returns the error:

            Code:
            invalid at() dimension information;
            using variable gender as a factor variable and a regular variable is not supported
            What's interesting here is that on my laptop I have stata 12, and at work I have stata 14.1 and this error appears in stata 14 but not in 12!
            I'm sending this to Tech support as we speak, beaucse as clyde mentioned it indeed seems like some minor bug.

            Comment


            • #7
              So, one piece of advice I received, pertinent to my example, is that I could make a fake Year variable, say Year2 = Year, and use them thusly: i.Year and c.Year2. I expect this would be fine unless one or more of the variables in my margins command involves Year, Year2, or one of their interactions.

              Comment


              • #8
                I have the same issue on Stata 14 (marginsplot gives this error "using variable xxdummy as a factor variable and a regular variable is not supported"). Did you find any workaround for this issue? Thanks, Sorin

                Comment


                • #9
                  This is what Stata TS answered to my query:

                  Dear Ariel,

                  This was actually an improvement of the -marginsplot- command which was
                  released on update 26jun2014 for Stata 13. You may not have access to
                  Stata 13's whatsnew.sthlp. Please see below for the description and
                  solution:

                  12. marginsplot has the following improvements and fixes:

                  ......

                  c. marginsplot now checks for at() variables that are present
                  as both a factor variable and a regular variable in the
                  estimation results. For example,

                  . sysuse auto
                  . regress mpg foreign rep78#foreign
                  . margins, at(rep78=(3/5))

                  treats foreign as a regular variable in the main effect but
                  as a factor variable in the interaction with rep78.
                  marginsplot does not support this and will exit with an
                  error identifying foreign as the offending variable.

                  The solution to this situation is to refit and specify the
                  offending variable exclusively as a factor variable or
                  exclusively as a regular variable. For the above example,
                  this means specifying the linear regression as

                  . regress mpg i.foreign rep78#foreign

                  Comment

                  Working...
                  X