Announcement

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

  • Linear mixed model with mixed - help with margins

    Hello all,

    I'm running a linear mixed model using "mixed". My highest level (2 levels) variable is a categorical one with 10 categories and I want to know if there is a command or a option of the margins command that displays the graphical difference of the outcome for every group of the second level. Below you can find my variables, the main code I'm using for the analysis and the following codes I'm using for the graphical part.

    Z_altidade: numeric outcome
    Saud: yes/no variable
    Nsaud: yes/no variable
    Class_idhm: 4 categories
    Idade: age as numeric
    Sexo: sex 0 female 1 male

    Class_cob: 10 categories second level variable


    Code:
    mixed z_altidade i.saud##i.class_idhm i.nsaud##i.class_idhm idade i.sexo || class_cob: , cov(un)
    My objective is to see how the correlation between what a child eats (saud and nsaud) and the levels of Human Development Index (IDHM) of their home states influences their height (z_altidade) - therefore the interaction.

    Code:
    margins i.saud##i.macrorregiao
    marginsplot
    I now want to see how this relation happens in each group of the second level (class_cob).

    Is it possible? Can you help me?

  • #2
    Luana:
    as -margins- do not support -bysort-, you may want to consider something along the following lines:
    Code:
    . use "C:\Program Files\Stata18\ado\base\a\auto.dta"
    (1978 automobile data)
    
    . reg price i.foreign##i.rep78
    note: 1.foreign#1b.rep78 identifies no observations in the sample.
    note: 1.foreign#2.rep78 identifies no observations in the sample.
    note: 1.foreign#5.rep78 omitted because of collinearity.
    
          Source |       SS           df       MS      Number of obs   =        69
    -------------+----------------------------------   F(7, 61)        =      0.39
           Model |    24684607         7  3526372.43   Prob > F        =    0.9049
        Residual |   552112352        61  9051022.16   R-squared       =    0.0428
    -------------+----------------------------------   Adj R-squared   =   -0.0670
           Total |   576796959        68  8482308.22   Root MSE        =    3008.5
    
    -------------------------------------------------------------------------------
            price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
    --------------+----------------------------------------------------------------
          foreign |
         Foreign  |   2088.167   2351.846     0.89   0.378     -2614.64    6790.974
                  |
            rep78 |
               2  |   1403.125   2378.422     0.59   0.557    -3352.823    6159.073
               3  |   2042.574   2204.707     0.93   0.358    -2366.011    6451.159
               4  |   1317.056   2351.846     0.56   0.578    -3385.751    6019.863
               5  |       -360   3008.492    -0.12   0.905    -6375.851    5655.851
                  |
    foreign#rep78 |
       Foreign#1  |          0  (empty)
       Foreign#2  |          0  (empty)
       Foreign#3  |  -3866.574   2980.505    -1.30   0.199    -9826.462    2093.314
       Foreign#4  |  -1708.278   2746.365    -0.62   0.536    -7199.973    3783.418
       Foreign#5  |          0  (omitted)
                  |
            _cons |     4564.5   2127.325     2.15   0.036      310.651    8818.349
    -------------------------------------------------------------------------------
    
    . margins i.foreign##i.rep78 if foreign==0
    
    Predictive margins                                          Number of obs = 48
    Model VCE: OLS
    
    Expression: Linear prediction, predict()
    
    -------------------------------------------------------------------------------
                  |            Delta-method
                  |     Margin   std. err.      t    P>|t|     [95% conf. interval]
    --------------+----------------------------------------------------------------
          foreign |
        Domestic  |    6179.25   434.2384    14.23   0.000     5310.937    7047.563
                  |
            rep78 |
               1  |     4564.5   2127.325     2.15   0.036      310.651    8818.349
               2  |   5967.625   1063.662     5.61   0.000     3840.701    8094.549
               3  |   6607.074   578.9845    11.41   0.000     5449.323    7764.825
               4  |   5881.556   1002.831     5.86   0.000     3876.272    7886.839
               5  |     4204.5   2127.325     1.98   0.053    -49.34897    8458.349
                  |
    foreign#rep78 |
      Domestic#1  |     4564.5   2127.325     2.15   0.036      310.651    8818.349
      Domestic#2  |   5967.625   1063.662     5.61   0.000     3840.701    8094.549
      Domestic#3  |   6607.074   578.9845    11.41   0.000     5449.323    7764.825
      Domestic#4  |   5881.556   1002.831     5.86   0.000     3876.272    7886.839
      Domestic#5  |     4204.5   2127.325     1.98   0.053    -49.34897    8458.349
    -------------------------------------------------------------------------------
    
    . margins i.foreign##i.rep78 if foreign==1
    
    Predictive margins                                          Number of obs = 21
    Model VCE: OLS
    
    Expression: Linear prediction, predict()
    
    -------------------------------------------------------------------------------
                  |            Delta-method
                  |     Margin   std. err.      t    P>|t|     [95% conf. interval]
    --------------+----------------------------------------------------------------
          foreign |
         Foreign  |   6070.143   656.5067     9.25   0.000     4757.377    7382.909
                  |
            rep78 |
               3  |   4828.667   1736.953     2.78   0.007     1355.414     8301.92
               4  |   6261.444   1002.831     6.24   0.000     4256.161    8266.728
               5  |   6292.667   1002.831     6.27   0.000     4287.383     8297.95
                  |
    foreign#rep78 |
       Foreign#3  |   4828.667   1736.953     2.78   0.007     1355.414     8301.92
       Foreign#4  |   6261.444   1002.831     6.24   0.000     4256.161    8266.728
       Foreign#5  |   6292.667   1002.831     6.27   0.000     4287.383     8297.95
    -------------------------------------------------------------------------------
    
    .
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      Luana,

      A couple of questions and points before getting into your question.
      • Can you please define the hierarchical nesting of your data? You mention children. Is it children nested within classrooms, cities, states, countries? Something else.
      • What is class_cob? Is it a variable that denotes a group that an individual unit belongs to?
      • Can you indicate, for each of your variables, whether it was measured at level 1 (child level?) or level 2?
      • With ten groups (values) of class_cob, you should switch over to restricted maximum likelihood estimation (REML), perhaps with a degree of freedom correction such as Kenward-Roger. These help to deal with small sample problems at level 2 in mixed models. A nice paper that explains all this is McNeish (2017).
      • The variable in your margins statement, i.macrorregiao,needs to also appear in your mixed command. In other words, i.macrorregiao must be a predictor in your model.
      With regards to your question,
      I now want to see how this relation happens in each group of the second level (class_cob).
      You are trying to get a quantity that margins cannot give you given your mixed model. But to fully help you, I need you to answer the questions I posed above.

      Comment


      • #4
        Carlo,
        Thank you so much! It worked perfectly!
        I'll now roll up my sleeves and plot all the graphs I need!

        Best regards,

        Comment


        • #5
          Hi, Erik! Thank you for also answering!

          Originally posted by Erik Ruzek View Post

          A couple of questions and points before getting into your question.
          • Can you please define the hierarchical nesting of your data? You mention children. Is it children nested within classrooms, cities, states, countries? Something else.
          • The children are nested in groups of access to public health coverage

          • What is class_cob? Is it a variable that denotes a group that an individual unit belongs to?
          Yes, it is! There are 5 groups of children (I made a mistake in my first post when I said 10 groups), ranging from 12,647 to 53,400 in each group.

        • Can you indicate, for each of your variables, whether it was measured at level 1 (child level?) or level 2?
        Z_altidade: numeric outcome - level 1
        Saud: yes/no variable - level 1
        Nsaud: yes/no variable - level 1
        Class_idhm: 4 categories - level 2
        Idade: age as numeric - level 1 (control)
        Sexo: sex 0 female 1 male - level 1 (control)

        Class_cob: 5 categories second level variable - level 2

      • With ten groups (values) of class_cob, you should switch over to restricted maximum likelihood estimation (REML), perhaps with a degree of freedom correction such as Kenward-Roger. These help to deal with small sample problems at level 2 in mixed models. A nice paper that explains all this is McNeish (2017).
      • This is the distribution in level 2. I don't really understand REML, do you think I should switch?


        Class_cob Freq. Percent Cum.

        1 13,397 8.12 8.12
        2 45,074 27.32 35.44
        3 53,400 32.36 67.80
        4 40,489 24.54 92.34
        5 12,647 7.66 100.00

        Total 165,007 100.00



      • The variable in your margins statement, i.macrorregiao,needs to also appear in your mixed command. In other words, i.macrorregiao must be a predictor in your model.
      • Another mistake I made in the first post! I actually have 5 models of analysis, with 5 different predictors mesuared in level 2, I happened to accidentally switch between my commands in the post (class_idhm, urb, macrorregiao, GDP, APS). But I get what you are saying, the predictor I want to plot with -margins- and -marginsplot- must be the sabe I'm using in my current model, right?

        With regards to your question,

        You are trying to get a quantity that margins cannot give you given your mixed model. But to fully help you, I need you to answer the questions I posed above.


        Thank you so much for the insights! I hope my answers are clear and help you help me.

        Comment


        • #6
          Thanks for clearing things up for me, Luana. First thing, you are running a mixed model that has only 5 groups at level 2. That is the about the absolute minimum number of groups you should have for a mixed model, unless you are willing to go Bayesian. For a frequentist model estimated by (restricted) maximum likelihood, you will need to use the methods and corrections I mentioned.

          I suggest that you run your model as such:
          Code:
          mixed z_altidade i.saud##i.class_idhm i.nsaud##i.class_idhm idade i.sexo || class_cob: nausd saud , cov(un) reml dfmethod(kroger)
          This model adds nsaud and saud to the random effect side of the equation as slopes. What this does is allow the association (slope) between a child's nausd/saud and their outcome to vary across class_cob groups. These are often called random slopes in mixed effects models. The interactions in the fixed effects part of the equation (i.saud##i.class_idhm i.nsaud##i.class_idhm) address the question of whether the variation in nausd/saud slopes across groups can be explained by a group's value on class_idhm. These are sometimes called cross-level interactions. These interactions addresses your question, "I now want to see how this relation happens in each group of the second level (class_cob)."

          What the mixed model does is give you variance terms for the random effects. In this model above, you get a variance for the residual (level 1 within-group deviations around level 2 means), the intercept (level 2 mean deviations around the sample mean), slope variances for nausd and saud (variation of level 2 slopes around the sample slope values), and then covariances between the group random slopes and the group's intercept. One goal of mixed effects models is to quantify variance explained by predictors of these various random effects. Accordingly, I would probably compare the variance estimates for the random slopes from m1 to m2, below.
          Code:
          mixed z_altidade i.saud .class_idhm i.nsaud idade i.sexo || class_cob: nausd saud , cov(un) reml dfmethod(kroger)
          eststo m1
          mixed z_altidade i.saud##i.class_idhm i.nsaud##i.class_idhm idade i.sexo || class_cob: nausd saud , cov(un) reml dfmethod(kroger)
          eststo m2
          You can then test whether the inclusion of the cross-level interactions improves the fit of your model relative to a model without the interactions:
          Code:
          lrtest m2 m1, stats
          You will get a likelihood ratio test of whether m2 provides a better fit than m1 (divide the p-value by 2) as well as information criteria (lower values are better) from the two models.

          You can also manually calculate how much the random slope variances decrease from m2 to m1 to help you understand the impact of class_idhm in explaining group differences in the association between nsaud/saud and z_altidade. A follow-up margins and marginsplot shows you how these variables in combination are related to the outcome.
          Code:
          margins i.saud#i.class_idhm
          marginsplot
          margins i.nsaud#i.class_idhm
          marginsplot

          Comment


          • #7
            Hi, Erik. Sorry for the delayed response. I'm having trouble accessing statalist, so I haven't seen your reply.

            Thank you for all your inputs. I ran your code and it worked very well! Thank you for it and thank you for all the explanation with it!

            Hope I can finish my dissertation easier now!

            Comment

            • Working...
              X