Announcement

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

  • Random slopes and random effects with melogit and xtmelogit

    melogit CSES5govact c.CSES5corrupt c.income c.gini c.polstrust || E1003: CSES5corrupt

    I'm running a two-level logit trying to get reliable random slopes for CSES5corrupt, ideally with confidence intervals. What works in melogit is not giving me a random slope common to each level 2 unit (E1003, which is country/election). The following I've got from an earlier post in the forums but it's not what I'm after.

    predict fe, xb ///// linear prediction, fixed portion only
    predict re, reffects reses(reses) /////empirical Bayes means for _cons[E1003]
    gen predicted fe + re
    gen lower_ci = predicted - 1.96*reses
    gen upper_ci = predicted + 1.96*reses

    but predict fe, xb is not giving me the random slope I want - it varies within the level 2 cases. So all I'm getting is the constant and its standard error from the second line of code. The next two lines are getting me a prediction for each level 1 case which I don't want.

    When I run the same model in xtmelogit and use the code below I do get what I want (I think)

    predict re_slope re_cons, reffects ///// creates two variables, random effects for E1003: CSEScorrupt & random effects for E1003:_cons
    predict u0se u1se, reses //// re standard errors for them both

    this gives me a random slope for each level 2 unit, the constant for each unit, and their standard errors.

    But when I try to run this code after melogit I get 'too many variables specified'.

    Anyone know how I can get these after melogit (which is faster). Melogit and xtmelogit also give slightly different results and I understand xtmelogit is an older version. And, ideally, get confidence intervals too boot? I have gone to the various help options and the manual but I'm not finding any worked examples or instructions i can make sense of.
    Last edited by Jack Vowles; 09 May 2023, 22:54.

  • #2
    predict re, reffects reses(reses)
    I don't think this is the exact code you are using. It should produce a halt with an error message that you have not specified enough variables, and provide no results at all:
    Code:
    . predict re, reffects reses(reses)
    (calculating posterior means of random effects)
    too few variables specified
    r(102);
    Or maybe you are running an old version of Stata that had a different syntax?

    but predict fe, xb is not giving me the random slope
    It isn't supposed to give you the random slopes, just the fixed-effects part of the model.

    I can't really figure out what's going on here based on the information provided, and I doubt anybody else will either. Please post back showing the complete exact code and the complete and exact Stata output (including any warnings, notes, or error messages) from the -melogit- command through the -predict- command(s). Then also show some example data for the predictions that are not behaving as you expect. Use -dataex- for that. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Thanks for this. I have a very large dataset, not easily shared.

      This is the current model -

      melogit corrdum c.pdi c.uai c.gini c.age i.female i.eduni c.polstrust i.catholic || E1003: polstrust

      The first three IVs are level 2, the rest level 1. The output I get is exactly what you'd expect

      Mixed-effects logistic regression Number of obs = 61,400
      Group variable: E1003 Number of groups = 34

      Obs per group:
      min = 688
      avg = 1,805.9
      max = 6,931

      Integration method: mvaghermite Integration pts. = 7

      Wald chi2(8) = 793.62
      Log likelihood = -29795.515 Prob > chi2 = 0.0000
      --------------------------------------------------------------------------------
      corrdum | Coefficient Std. err. z P>|z| [95% conf. interval]
      ---------------+----------------------------------------------------------------
      pdi | .0175378 .0082242 2.13 0.033 .0014187 .0336569
      uai | .0196159 .0085927 2.28 0.022 .0027745 .0364573
      gini | .0397426 .0226675 1.75 0.080 -.004685 .0841701
      age | -.0060404 .000594 -10.17 0.000 -.0072047 -.0048761
      1.female | .1803154 .0203975 8.84 0.000 .140337 .2202938
      1.eduni | -.4136575 .0221076 -18.71 0.000 -.4569876 -.3703274
      polstrust | -.5701438 .0366495 -15.56 0.000 -.6419756 -.4983121
      1.catholic | .1081359 .0282044 3.83 0.000 .0528564 .1634155
      _cons | -1.454897 .8362108 -1.74 0.082 -3.09384 .1840464
      ---------------+----------------------------------------------------------------
      E1003 |
      var(polstrust)| .0410008 .0111351 .024078 .0698174
      var(_cons)| .6946292 .1738312 .4253432 1.134401
      --------------------------------------------------------------------------------
      LR test vs. logistic model: chi2(2) = 4311.32 Prob > chi2 = 0.0000

      Note: LR test is conservative and provided only for reference.

      The error message you show is exactly what I got. However, the code below now works in Stata 18 - which I don't think worked in Stata 17.

      predict re_slope re_cons, reffects
      (calculating posterior means of random effects)
      (using 7 quadrature points)

      This is great - that's about 90 per cent of the problem solved. It still not not give me the standard errors (the code below works in xtmelogit, but gives me the error message below in melogit)

      predict u0 u1, reses
      (option mu assumed)
      (predictions based on fixed effects and posterior means of random effects)
      option reses not allowed



      Comment


      • #4
        Originally posted by Jack Vowles View Post
        . . .that's about 90 per cent of the problem solved. It still not not give me the standard errors (the code below works in xtmelogit, but gives me the error message below in melogit)

        predict u0 u1, reses
        According to the help file for the current release, the proper syntax for specifying standard errors includes reses(stub* | newvarlist), and so try something like that shown below just after the "Begin here" comment.

        .ÿ
        .ÿversionÿ18.0

        .ÿ
        .ÿclearÿ*

        .ÿ
        .ÿ//ÿseedem
        .ÿsetÿseedÿ278251319

        .ÿ
        .ÿquietlyÿdrawnormÿiÿs,ÿdoubleÿcorr(1ÿ-0.1ÿ\ÿ-0.1ÿ1)ÿn(500)

        .ÿgenerateÿintÿpidÿ=ÿ_n

        .ÿ
        .ÿquietlyÿexpandÿ10

        .ÿgenerateÿdoubleÿsloÿ=ÿruniform(-0.5,ÿ0.5)

        .ÿgenerateÿdoubleÿxbuÿ=ÿ0ÿ+ÿiÿ+ÿ(1ÿ+ÿslo)ÿ*ÿs

        .ÿgenerateÿbyteÿoutÿ=ÿrbinomial(1,ÿinvlogit(xbu))

        .ÿ
        .ÿquietlyÿmelogitÿoutÿc.sloÿ||ÿpid:ÿslo

        .ÿ
        .ÿ*
        .ÿ*ÿBeginÿhere
        .ÿ*
        .ÿpredictÿdoubleÿeb*,ÿreffectsÿreses(se*)
        (calculatingÿposteriorÿmeansÿofÿrandomÿeffects)
        (usingÿ7ÿquadratureÿpoints)

        .ÿ
        .ÿlocalÿline_sizeÿ`c(linesize)'

        .ÿsetÿlinesizeÿ90

        .ÿdescribeÿeb?ÿse?

        VariableÿÿÿÿÿÿStorageÿÿÿDisplayÿÿÿÿValue
        ÿÿÿÿnameÿÿÿÿÿÿÿÿÿtypeÿÿÿÿformatÿÿÿÿlabelÿÿÿÿÿÿVariableÿlabel
        ------------------------------------------------------------------------------------------
        eb1ÿÿÿÿÿÿÿÿÿÿÿÿÿdoubleÿÿ%10.0gÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿempiricalÿBayesÿmeansÿforÿslo[pid]
        eb2ÿÿÿÿÿÿÿÿÿÿÿÿÿdoubleÿÿ%10.0gÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿempiricalÿBayesÿmeansÿforÿ_cons[pid]
        se1ÿÿÿÿÿÿÿÿÿÿÿÿÿdoubleÿÿ%10.0gÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿS.E.ÿofÿempiricalÿBayesÿmeansÿforÿslo[pid]
        se2ÿÿÿÿÿÿÿÿÿÿÿÿÿdoubleÿÿ%10.0gÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿS.E.ÿofÿempiricalÿBayesÿmeansÿforÿ_cons[pid]

        .ÿsetÿlinesizeÿ`line_size'

        .ÿ
        .ÿexit

        endÿofÿdo-file


        .

        Comment


        • #5
          You can't do -reses- separately. It has to be done in combination with -reffects-, and requires specification of its own varlist:
          Code:
          predict rfx*, reffects reses(rfxse*)

          Comment


          • #6
            I wrote

            generate double xbu = 0 + i + (1 + slo) * s

            Not that it matters for the point being made, but for the record that ought to have been
            Code:
            generate double xbu = 0 + i + (1 + s) * slo

            Comment

            Working...
            X