3
votes

I tried to calculate the margins for a variable in Stata 12, after running a multilevel regression with the xtlogit command. But, although I used the margins command right after running the regression, I still received an error, saying that my variable was not found in a list of covariates. Here is a simplified version of my code:

. use http://url.com/file.dta, clear
. xtset country
. xtlogit dv iv1 iv2 iv3 iv4 iv5
. margins iv1, at(iv2==(0(1)6))
'iv1' not found in list of covariates
r(322);

Interestingly, Stata does not give any errors when I use the margins command in a format that requires a comma after it. For example, the following two lines of code work without any problems:

margins, at(iv2=(0(1)6)) over(iv1)
margins, dydx(iv1) at(iv2=(0(1)6))

I have seen this previous post from March 2013, but I still cannot figure out how I can solve this problem: Stata error: not found in list of covariates

1
Can you describe what you would like margins iv1, at(iv2==(0(1)6)) to produce? It's obvious to me why it errors, but the goal is not clear, so the solution is out of reach.Dimitriy V. Masterov
I am trying to create a marginal effects plot; so the next command will be marginsplot.neutral
Why doesn't margins, dydx(iv1) at(iv2=(0(1)6)) suit your purpose?Dimitriy V. Masterov
Because the derivative option distorts the results for my model. (And the first line of code I provided creates two lines for the two values of the dummy variable.)neutral
It really is best not to cite a string in quotation marks if it wasn't the error message you got. Quotation marks imply literal text.Nick Cox

1 Answers

1
votes

Can you reproduce the error with public data set? Here's my attempt (with factor variable solution at the bottom):

. use http://www.stata-press.com/data/r13/union
(NLS Women 14-24 in 1968)

. xtlogit union age grade not_smsa south##c.year

Fitting comparison model:

Iteration 0:   log likelihood =  -13864.23  
Iteration 1:   log likelihood = -13547.326  
Iteration 2:   log likelihood = -13542.493  
Iteration 3:   log likelihood =  -13542.49  
Iteration 4:   log likelihood =  -13542.49  

Fitting full model:

tau =  0.0     log likelihood =  -13542.49
tau =  0.1     log likelihood = -12923.751
tau =  0.2     log likelihood = -12417.651
tau =  0.3     log likelihood = -12001.665
tau =  0.4     log likelihood = -11655.586
tau =  0.5     log likelihood = -11366.441
tau =  0.6     log likelihood = -11128.749
tau =  0.7     log likelihood = -10946.399
tau =  0.8     log likelihood = -10844.833

Iteration 0:   log likelihood = -10946.488  
Iteration 1:   log likelihood =  -10557.39  
Iteration 2:   log likelihood = -10540.493  
Iteration 3:   log likelihood = -10540.274  
Iteration 4:   log likelihood = -10540.274  (backed up)
Iteration 5:   log likelihood = -10540.274  

Random-effects logistic regression              Number of obs      =     26200
Group variable: idcode                          Number of groups   =      4434

Random effects u_i ~ Gaussian                   Obs per group: min =         1
                                                               avg =       5.9
                                                               max =        12

Integration method: mvaghermite                 Integration points =        12

                                                Wald chi2(6)       =    227.46
Log likelihood  = -10540.274                    Prob > chi2        =    0.0000

------------------------------------------------------------------------------
       union |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         age |   .0156732   .0149895     1.05   0.296    -.0137056     .045052
       grade |   .0870851   .0176476     4.93   0.000     .0524965    .1216738
    not_smsa |  -.2511884   .0823508    -3.05   0.002    -.4125929   -.0897839
     1.south |  -2.839112   .6413116    -4.43   0.000    -4.096059   -1.582164
        year |  -.0068604   .0156575    -0.44   0.661    -.0375486    .0238277
             |
south#c.year |
          1  |   .0238506   .0079732     2.99   0.003     .0082235    .0394777
             |
       _cons |  -3.009365   .8414963    -3.58   0.000    -4.658667   -1.360062
-------------+----------------------------------------------------------------
    /lnsig2u |   1.749366   .0470017                      1.657245    1.841488
-------------+----------------------------------------------------------------
     sigma_u |   2.398116   .0563577                      2.290162    2.511158
         rho |   .6361098   .0108797                      .6145307    .6571548
------------------------------------------------------------------------------
Likelihood-ratio test of rho=0: chibar2(01) =  6004.43 Prob >= chibar2 = 0.000

. margins not_smsa, at(age=(10(5)20))
'not_smsa' not found in list of covariates
r(322);

. xtlogit union age grade i.not_smsa i.south##c.year

Fitting comparison model:

Iteration 0:   log likelihood =  -13864.23  
Iteration 1:   log likelihood = -13547.326  
Iteration 2:   log likelihood = -13542.493  
Iteration 3:   log likelihood =  -13542.49  
Iteration 4:   log likelihood =  -13542.49  

Fitting full model:

tau =  0.0     log likelihood =  -13542.49
tau =  0.1     log likelihood = -12923.751
tau =  0.2     log likelihood = -12417.651
tau =  0.3     log likelihood = -12001.665
tau =  0.4     log likelihood = -11655.586
tau =  0.5     log likelihood = -11366.441
tau =  0.6     log likelihood = -11128.749
tau =  0.7     log likelihood = -10946.399
tau =  0.8     log likelihood = -10844.833

Iteration 0:   log likelihood = -10946.488  
Iteration 1:   log likelihood =  -10557.39  
Iteration 2:   log likelihood = -10540.493  
Iteration 3:   log likelihood = -10540.274  
Iteration 4:   log likelihood = -10540.274  (backed up)
Iteration 5:   log likelihood = -10540.274  

Random-effects logistic regression              Number of obs      =     26200
Group variable: idcode                          Number of groups   =      4434

Random effects u_i ~ Gaussian                   Obs per group: min =         1
                                                               avg =       5.9
                                                               max =        12

Integration method: mvaghermite                 Integration points =        12

                                                Wald chi2(6)       =    227.46
Log likelihood  = -10540.274                    Prob > chi2        =    0.0000

------------------------------------------------------------------------------
       union |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         age |   .0156732   .0149895     1.05   0.296    -.0137056     .045052
       grade |   .0870851   .0176476     4.93   0.000     .0524965    .1216738
  1.not_smsa |  -.2511884   .0823508    -3.05   0.002    -.4125929   -.0897839
     1.south |  -2.839112   .6413116    -4.43   0.000    -4.096059   -1.582164
        year |  -.0068604   .0156575    -0.44   0.661    -.0375486    .0238277
             |
south#c.year |
          1  |   .0238506   .0079732     2.99   0.003     .0082235    .0394777
             |
       _cons |  -3.009365   .8414963    -3.58   0.000    -4.658667   -1.360062
-------------+----------------------------------------------------------------
    /lnsig2u |   1.749366   .0470017                      1.657245    1.841488
-------------+----------------------------------------------------------------
     sigma_u |   2.398116   .0563577                      2.290162    2.511158
         rho |   .6361098   .0108797                      .6145307    .6571548
------------------------------------------------------------------------------
Likelihood-ratio test of rho=0: chibar2(01) =  6004.43 Prob >= chibar2 = 0.000


. margins not_smsa, at(age=(10(5)20))

Predictive margins                                Number of obs   =      26200
Model VCE    : OIM

Expression   : Linear prediction, predict()

1._at        : age             =          10

2._at        : age             =          15

3._at        : age             =          20

------------------------------------------------------------------------------
             |            Delta-method
             |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
_at#not_smsa |
        1 0  |  -2.674903   .3107206    -8.61   0.000    -3.283905   -2.065902
        1 1  |  -2.926092   .3148551    -9.29   0.000    -3.543196   -2.308987
        2 0  |  -2.596538   .2375601   -10.93   0.000    -3.062147   -2.130928
        2 1  |  -2.847726   .2432156   -11.71   0.000     -3.32442   -2.371032
        3 0  |  -2.518172   .1660016   -15.17   0.000    -2.843529   -2.192814
        3 1  |   -2.76936   .1743793   -15.88   0.000    -3.111137   -2.427583
------------------------------------------------------------------------------