Announcement

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

  • MARGINS ERROR: "at values for factor do not sum to 1"

    Hello Statausers,
    I would like to ask you a question about margins command. I have run a regression model and I want to post-estimate the marginal effect of two factor variables: TITULACION and FLIPPED. The model is:
    HTML Code:
    . regress EXAMEN_10 i.SEXO EDAD i.TRABAJA VECES_PRESENTADO PRE_CLASS IN_CLASS ib3.TITULACION i.F
    > LIPPED, vce(robust) noconst
    
    Linear regression                               Number of obs     =         47
                                                    F(10, 37)         =      60.33
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.9303
                                                    Root MSE          =     1.7832
    
    ----------------------------------------------------------------------------------
                     |               Robust
           EXAMEN_10 |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -----------------+----------------------------------------------------------------
                SEXO |
               Male  |   .1176539    .562293     0.21   0.835     -1.02166    1.256968
                EDAD |   .0498757    .041558     1.20   0.238    -.0343288    .1340802
                     |
             TRABAJA |
                 No  |   -.152863   .6520936    -0.23   0.816     -1.47413    1.168404
       Sporadically  |   .5782017   .7386561     0.78   0.439    -.9184577    2.074861
                     |
    VECES_PRESENTADO |   .5914525   1.391224     0.43   0.673    -2.227436    3.410341
           PRE_CLASS |  -.2131888   .2993504    -0.71   0.481    -.8197304    .3933529
            IN_CLASS |   .0327935    .333986     0.10   0.922    -.6439264    .7095134
                     |
          TITULACION |
               GADE  |   2.313079   .7710534     3.00   0.005     .7507762    3.875381
           GADE&DER  |   3.058591   1.368283     2.24   0.032     .2861865    5.830996
                     |
             FLIPPED |
                Yes  |   3.290253   1.621946     2.03   0.050      .003879    6.576627
    ----------------------------------------------------------------------------------
    If I estimate the margin of the interaction TITULACION##FLIPPED
    HTML Code:
    . . margins i.TITULACION##i.FLIPPED
    
    Predictive margins                              Number of obs     =         47
    Model VCE    : Robust
    
    Expression   : Linear prediction, predict()
    
    ------------------------------------------------------------------------------------
                       |            Delta-method
                       |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------------+----------------------------------------------------------------
            TITULACION |
                 GADE  |   6.090142   .3121682    19.51   0.000     5.457629    6.722655
             GADE&DER  |   6.835655   1.131745     6.04   0.000     4.542521    9.128788
                GFICO  |   3.777063   .6633242     5.69   0.000     2.433041    5.121086
                       |
               FLIPPED |
                  Yes  |   5.678936   .2601059    21.83   0.000     5.151911    6.205961
                       |
    TITULACION#FLIPPED |
             GADE#Yes  |   6.090142   .3121682    19.51   0.000     5.457629    6.722655
         GADE&DER#Yes  |   6.835655   1.131745     6.04   0.000     4.542521    9.128788
            GFICO#Yes  |   3.777063   .6633242     5.69   0.000     2.433041    5.121086
    ------------------------------------------------------------------------------------
    The problem is that the output when FLIPPED=No is not reported, even it is not in the marginsplot

    Click image for larger version

Name:	graph.JPG
Views:	1
Size:	60.1 KB
ID:	1464265


    But if the code of the margins comand is:
    HTML Code:
    margins, at (FLIPPED=(0 1)  TITULACION=(1 2 3))
    The result is an error message: "at values for factor FLIPPED do not sum to 1
    r(198);"

    I want to generate a marginsplot like this, but with two line one for FLIPPED=1 and other for FLIPPED=0, and in the X axis the categories for TITULACION

    Click image for larger version

Name:	Captura.JPG
Views:	1
Size:	25.8 KB
ID:	1464266

    Thanks for you help,
    Best,
    Rocio

  • #2
    Try
    margins FLIPPED, at(TITULACION = (1 2 3))
    marginsplot

    Comment


    • #3
      Hi Rocio,
      this error message may arise when you misspecify the categories of your variables within at().
      Check how FLIPPED is coded: it is possible that the variable FLIPPED is coded 1/2 rather that 0/1.

      Best,
      Raffaele

      Comment


      • #4
        Thanks Eric and Raffaele, but I've recoded all variables, and all labels have been drop. The descriptives are:
        HTML Code:
        . sum EXAMEN_10 SEXO EDAD TRABAJA VECES_PRESENTADO PRE_CLASS IN_CLASS TITULACION FLIPPED
        
            Variable |        Obs        Mean    Std. Dev.       Min        Max
        -------------+---------------------------------------------------------
           EXAMEN_10 |         97     5.58701    2.159333         .3        9.6
                SEXO |        103    1.407767    .4938225          1          2
                EDAD |         99    23.64646    4.785849         20         53
             TRABAJA |         99    1.909091    .7705141          1          3
        VECES_PRES~O |        102    .0980392    .4105009          0          3
        -------------+---------------------------------------------------------
           PRE_CLASS |         51    4.666667    1.417451          1       6.75
            IN_CLASS |         52    4.887821    1.326667   1.666667          7
          TITULACION |        103    1.621359    .8058184          1          3
             FLIPPED |        103    1.466019    .5012833          1          2
        The regress output (The number of observations is only 47 when in the data base there are 103):
        HTML Code:
        . . regress EXAMEN_10 i.SEXO EDAD i.TRABAJA VECES_PRESENTADO PRE_CLASS IN_CLASS ib3.TITULACION i.FLIPPED, vce(robust) noconst
        
        Linear regression                               Number of obs     =         47
                                                        F(10, 37)         =      60.33
                                                        Prob > F          =     0.0000
                                                        R-squared         =     0.9303
                                                        Root MSE          =     1.7832
        
        ----------------------------------------------------------------------------------
                         |               Robust
               EXAMEN_10 |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
        -----------------+----------------------------------------------------------------
                  2.SEXO |   .1176539    .562293     0.21   0.835     -1.02166    1.256968
                    EDAD |   .0498757    .041558     1.20   0.238    -.0343288    .1340802
                         |
                 TRABAJA |
                      2  |   -.152863   .6520936    -0.23   0.816     -1.47413    1.168404
                      3  |   .5782017   .7386561     0.78   0.439    -.9184577    2.074861
                         |
        VECES_PRESENTADO |   .5914525   1.391224     0.43   0.673    -2.227436    3.410341
               PRE_CLASS |  -.2131888   .2993504    -0.71   0.481    -.8197304    .3933529
                IN_CLASS |   .0327935    .333986     0.10   0.922    -.6439264    .7095134
                         |
              TITULACION |
                      1  |   2.313079   .7710534     3.00   0.005     .7507762    3.875381
                      2  |   3.058591   1.368283     2.24   0.032     .2861865    5.830996
                         |
               1.FLIPPED |   3.290253   1.621946     2.03   0.050      .003879    6.576627
        ----------------------------------------------------------------------------------
        And the margins:
        HTML Code:
        . margins FLIPPED, at (TITULACION=(1 2 3))
        
        Predictive margins                              Number of obs     =         47
        Model VCE    : Robust
        
        Expression   : Linear prediction, predict()
        
        1._at        : TITULACION      =           1
        
        2._at        : TITULACION      =           2
        
        3._at        : TITULACION      =           3
        
        ------------------------------------------------------------------------------
                     |            Delta-method
                     |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
        -------------+----------------------------------------------------------------
         _at#FLIPPED |
                1 1  |   6.090142   .3121682    19.51   0.000     5.457629    6.722655
                2 1  |   6.835655   1.131745     6.04   0.000     4.542521    9.128788
                3 1  |   3.777063   .6633242     5.69   0.000     2.433041    5.121086
        ------------------------------------------------------------------------------
        And again FLIPPED=2 is missing!!!
        Click image for larger version

Name:	Graph_flipped.png
Views:	1
Size:	11.0 KB
ID:	1464331


        And changing the margins code, the same error message:
        HTML Code:
        . margins TITULACION, at (FLIPPED=(1 2))
        at values for factor FLIPPED do not sum to 1
        r(198);
        Please, any idea?

        Comment


        • #5
          I really don't understand what you are doing now. Have you tried, after your regression in your post #1 above, running the commands I indicated in post #2 above?
          What have you recoded? My comment in post # above was based on your output in post #1 above;
          I assumed that your variable TITULACION took the values 1, 2 and 3
          I may be wrong in what I proposed above, but since you appear to have changed your variables i am lost.

          Comment


          • #6
            OK. I think I see what's going on. You have a lot of missing data in your data set: although your -summarize- statistics show up to 103 observations for some variables, notice that your regression, which is necessarily restricted to observations with no missing values in any of the regression variables, is based on only 47 cases. And, this is key, notice that in the regression itself the value of FLIPPED that shows up in the output is 1. But if FLIPPED is coded 1 and 2, 1 should be the omitted base case and 2 should show in the output. The reason it doesn't, I'm betting, is that if you run -tab FLIPPED if e(sample)- after the regression you will find that FLIPPED is actually a constant in your regression: it is always 1. Normally, Stata would reject such a non-varying variable and omit it from the model--but because yours is a -noconstant- regression, having FLIPPED = 1 as a constant in the regression is allowable.

            The absence of -margins- outputs for FLIPPED = 2, and the error message when you try it with -at(FLIPPED = (1 2))- are both due to the same thing: in your estimation sample, FLIPPED is never 2.

            Comment


            • #7
              @Clyde: In her first post, there wasn't ever question of FLIPPED being equal to 2: it was either zero or one. This is why I asked why she had recoded her data.

              Comment


              • #8
                Eric, apology for unclear examples. I mean, in the example 1, the code of FLIPPED was (0 1) with labels 0=No and 1=Yes. In example 2, I recoded FLIPPED as (1 2) and drop the labels.
                And, as ever Clyde is right!!! The key is that one of the variables has not value when FLIPPED==0 (or FLIPPED==2 in example 2). Thanks you very much Clyde!!! Your comments always are useful and right. Thanks again!!!
                Rocio

                Comment


                • #9
                  Hi,

                  I'm having a similar problem, which I've replicated in the auto dataset:

                  Code:
                  sysuse auto, clear
                  reg price length 1.foreign
                  tab foreign if e(sample)
                  margins, at(1.foreign=(0 1))
                  margins, at(foreign=(0 1))
                  I get this error after both margins commands:
                  Code:
                  at values for factor foreign do not sum to 1
                  .

                  Is it possible to specify levels for foreign so this work? Or is the only solution to manual create a new dummy variable from the original factor variable?

                  Thanks,

                  Liam

                  Comment


                  • #10
                    Originally posted by Liam Wright View Post
                    Is it possible to specify levels for foreign so this work?
                    Yes.

                    Code:
                    regress price length 1.foreign 0b.foreign
                    I do not really understand why you would want that. What is wrong with

                    Code:
                    regress price length i.foreign
                    Best
                    Daniel

                    Comment


                    • #11
                      I'm really not sure why Stata does not accept those -at()- options, but, in any case when working with a discrete variable, the simpler way to get those margins is:

                      Code:
                      margins foreign

                      Comment


                      • #12
                        Thank you both for the quick responses.

                        Originally posted by daniel klein View Post

                        I do not really understand why you would want that. What is wrong with
                        I just use foreign as my example. In the dataset I actually care about, I have a factor variable with 10 categories in and I only want to specify some as dummies. I was wondering what is the briefest way of coding that.

                        I don't think the 0b.foreign method works when you have more than two categories. Running the following, margins reports 'not estimable' at newforeign==0.
                        Code:
                        sysuse auto, clear
                        gen rand=runiform()
                        gen newforeign=cond(rand<0.5,2,foreign)
                        reg price 2.newforeign 0b.newforeign 
                        margins, at(newforeign=(0 2))
                        Anyway, Clyde's solution works.

                        Thanks again,

                        Liam

                        Comment

                        Working...
                        X