### Stata FAQ Why doesn't the margins command work with a nested factor in anova? (Stata 11)

A client sent in a question concerning a problem he had with the margins command. It involved a repeated measures anova that used a nested factor as an error term in the model. The problem occurred when he tried to use margins on the between-subjects factor and on the between-within interaction term.

Here is an example dataset and anova model that illustrates the same problem.

use http://www.ats.ucla.edu/stat/data/repeated_anova, clear

anova dv trt / sid|trt time trt#time

Number of obs =     270     R-squared     =  0.7581
Root MSE      = 3.32007     Adj R-squared =  0.6973

Source |  Partial SS    df       MS           F     Prob > F
-----------+----------------------------------------------------
Model |  7426.31359    54  137.524326      12.48     0.0000
|
trt |  1054.39272     1  1054.39272       8.81     0.0049
sid|trt |  5147.56292    43  119.710766
-----------+----------------------------------------------------
time |  1108.93641     5  221.787283      20.12     0.0000
trt#time |  21.3536895     5  4.27073789       0.39     0.8571
|
Residual |  2369.91186   215  11.0228459
-----------+----------------------------------------------------
Total |  9796.22545   269   36.417195

margins time

Predictive margins                                Number of obs   =        270

Expression   : Linear prediction, predict()

------------------------------------------------------------------------------
|            Delta-method
|     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
time |
1  |   14.34467   .4949264    28.98   0.000     13.37463     15.3147
2  |   12.60844   .4949264    25.48   0.000     11.63841    13.57848
3  |   10.89067   .4949264    22.00   0.000     9.920629     11.8607
4  |      10.13   .4949264    20.47   0.000     9.159962    11.10004
5  |   8.855778   .4949264    17.89   0.000      7.88574    9.825816
6  |   8.217111   .4949264    16.60   0.000     7.247073    9.187149
------------------------------------------------------------------------------

margins trt

Predictive margins                                Number of obs   =        270

Expression   : Linear prediction, predict()

------------------------------------------------------------------------------
|            Delta-method
|     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
trt |
0  |  (not estimable)
1  |  (not estimable)
------------------------------------------------------------------------------

margins trt#time

Predictive margins                                Number of obs   =        270

Expression   : Linear prediction, predict()
------------------------------------------------------------------------------
|            Delta-method
|     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
trt#time |
0 1  |  (not estimable)
0 2  |  (not estimable)
0 3  |  (not estimable)
0 4  |  (not estimable)
0 5  |  (not estimable)
0 6  |  (not estimable)
1 1  |  (not estimable)
1 2  |  (not estimable)
1 3  |  (not estimable)
1 4  |  (not estimable)
1 5  |  (not estimable)
1 6  |  (not estimable)
------------------------------------------------------------------------------
As you can see, both margins trt and margins trt#time show up as not estimable. The problem is caused by the fact that sid and trt not crossed but nested, that is, sid is nested within trt. This is easily seen as subjects 1 through 26 are found in treatment level 0 while subjects 27 through 58 in treatment level 1. The result of the nesting is that the design matrix for sid|trt has numerous empty cells.

Fortunately, there's an easy fix for this situations and that is to use the asbalanced and emptycells(reweighted) options. Here is how the results look when these options are used.

margins trt, asbalanced emptycells(reweight)

Adjusted predictions                              Number of obs   =        270

Expression   : Linear prediction, predict()
at           : trt              (asbalanced)
sid              (asbalanced)
time             (asbalanced)
------------------------------------------------------------------------------
|            Delta-method
|     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
trt |
0  |   13.37725   .3287356    40.69   0.000     12.73294    14.02156
1  |    9.30131   .2561487    36.31   0.000     8.799267    9.803352
------------------------------------------------------------------------------

margins trt#time, asbalanced emptycells(reweight)

Adjusted predictions                              Number of obs   =        270

Expression   : Linear prediction, predict()
at           : trt              (asbalanced)
sid              (asbalanced)
time             (asbalanced)
------------------------------------------------------------------------------
|            Delta-method
|     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
trt#time |
0 1  |   16.58824   .8052346    20.60   0.000        15.01    18.16647
0 2  |   14.97294   .8052346    18.59   0.000     13.39471    16.55117
0 3  |   14.12882   .8052346    17.55   0.000     12.55059    15.70705
0 4  |   12.27471   .8052346    15.24   0.000     10.69648    13.85294
0 5  |   11.40294   .8052346    14.16   0.000      9.82471    12.98117
0 6  |   10.89588   .8052346    13.53   0.000     9.317652    12.47411
1 1  |    12.9825   .6274337    20.69   0.000     11.75275    14.21225
1 2  |   11.17286   .6274337    17.81   0.000      9.94311     12.4026
1 3  |   8.924643   .6274337    14.22   0.000     7.694895    10.15439
1 4  |   8.827857   .6274337    14.07   0.000      7.59811     10.0576
1 5  |   7.309286   .6274337    11.65   0.000     6.079538    8.539033
1 6  |   6.590714   .6274337    10.50   0.000     5.360967    7.820462
------------------------------------------------------------------------------
By the way, this problem does not occur if you use xtmixed instead of anova as shown below.
xtmixed dv trt##time || s:, var

Performing EM optimization:

Iteration 0:   log restricted-likelihood = -745.45836
Iteration 1:   log restricted-likelihood = -745.45836

Computing standard errors:

Mixed-effects REML regression                   Number of obs      =       270
Group variable: sid                             Number of groups   =        45

Obs per group: min =         6
avg =       6.0
max =         6

Wald chi2(11)      =    119.88
Log restricted-likelihood = -745.45836          Prob > chi2        =    0.0000
------------------------------------------------------------------------------
dv |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
1.trt |  -3.605735   1.659698    -2.17   0.030    -6.858683   -.3527872
|
time |
2  |  -1.615294   1.138774    -1.42   0.156    -3.847249     .616661
3  |  -2.459412   1.138774    -2.16   0.031    -4.691367   -.2274567
4  |  -4.313529   1.138774    -3.79   0.000    -6.545485   -2.081574
5  |  -5.185294   1.138774    -4.55   0.000    -7.417249   -2.953339
6  |  -5.692353   1.138774    -5.00   0.000    -7.924308   -3.460398
|
trt#time |
1 2  |  -.1943487   1.443659    -0.13   0.893    -3.023868    2.635171
1 3  |  -1.598445   1.443659    -1.11   0.268    -4.427965    1.231074
1 4  |   .1588865   1.443659     0.11   0.912    -2.670633    2.988406
1 5  |  -.4879202   1.443659    -0.34   0.735     -3.31744    2.341599
1 6  |  -.6994328   1.443659    -0.48   0.628    -3.528952    2.130087
|
_cons |   16.58824   1.309187    12.67   0.000     14.02228     19.1542
------------------------------------------------------------------------------
------------------------------------------------------------------------------
Random-effects Parameters  |   Estimate   Std. Err.     [95% Conf. Interval]
-----------------------------+------------------------------------------------
sid: Identity                |
var(_cons) |   18.11467   4.306567      11.36754    28.86652
-----------------------------+------------------------------------------------
var(Residual) |   11.02284   1.063137      9.124236    13.31652
------------------------------------------------------------------------------
LR test vs. linear regression: chibar2(01) =   148.23 Prob >= chibar2 = 0.0000

margins trt

Predictive margins                                Number of obs   =        270

Expression   : Linear prediction, fixed portion, predict()
------------------------------------------------------------------------------
|            Delta-method
|     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
trt |
0  |   13.37725   1.083345    12.35   0.000     11.25394    15.50057
1  |    9.30131   .8441355    11.02   0.000     7.646834    10.95578
------------------------------------------------------------------------------

margins trt#time

Adjusted predictions                              Number of obs   =        270

Expression   : Linear prediction, fixed portion, predict()
------------------------------------------------------------------------------
|            Delta-method
|     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
trt#time |
0 1  |   16.58824   1.309187    12.67   0.000     14.02228     19.1542
0 2  |   14.97294   1.309187    11.44   0.000     12.40698     17.5389
0 3  |   14.12882   1.309187    10.79   0.000     11.56286    16.69478
0 4  |   12.27471   1.309187     9.38   0.000     9.708746    14.84067
0 5  |   11.40294   1.309187     8.71   0.000     8.836981     13.9689
0 6  |   10.89588   1.309187     8.32   0.000     8.329922    13.46184
1 1  |    12.9825   1.020111    12.73   0.000     10.98312    14.98188
1 2  |   11.17286   1.020111    10.95   0.000     9.173477    13.17224
1 3  |   8.924643   1.020111     8.75   0.000     6.925263    10.92402
1 4  |   8.827857   1.020111     8.65   0.000     6.828477    10.82724
1 5  |   7.309286   1.020111     7.17   0.000     5.309906    9.308666
1 6  |   6.590714   1.020111     6.46   0.000     4.591334    8.590094
------------------------------------------------------------------------------

The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.