Announcement

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

  • Graphing a multilevel model with two nests

    Hello. I'm looking for suggestions on how to graph a multilevel model I'm working on.

    I'm working with a pooled dataset on hospital costs with a set of dummy covariates which I nest around two groups, country and hospital. Therefore my model looks like this:
    Code:
    xtmixed logcost dummy1* dummy2* dummy3* || country: || hospitalid:
    I was looking for suggestions on how to possibly graph the random effects component of my model. I want to show the different residual effects at different nesting levels. I've looked around both on stata and elsewhere, but the best I could find is this, which doesn't help much since I don't have a x-axis time variable to work with, and I'd rather work with frequency/density on the y and costs on the x -- if that makes sense.

    Thanks in advance and let me know if you need me to be more specific about my dataset.

  • #2
    You can get the random effects with -predict, reffects- (-help mixed postestimation- then click the link for -predict- for more information). Then it sounds like you want a histogram. -help histogram-. I'm actually rather partial to -dotplot- instead, but it will have axes interchanged from what you're looking for.

    By the way, if you're using current Stata, the command -xtmixed- has been renamed -mixed-. If you're not, you're asked to say so in your post.

    Comment


    • #3
      Thanks a lot for your answer, Clyde. Yes, I'm using Stata 12, did not think it would be worth mentioning for the graphical part.

      So, what I'm looking for is a graph like this one. What I've done now is this:

      Code:
      xtmixed logcost1 || country: || hosid:
      predict grandmean, resid
      predict countryre hospitalre, reffects
      dotplot grandmean countryre hospitalre
      And I get this:

      Click image for larger version

Name:	re.png
Views:	1
Size:	39.6 KB
ID:	1356778


      Which, in theory, looks similar to what I am aiming for, but I am not entirely sure it is correct. When I look at the variables that predict generated, their means differ from the random effects estimates from xtmixed. What do you think? I am still wrapping my head around multilevel modeling, and I apologize if I'm asking basic questions. Thanks again.

      Comment


      • #4
        Wait, I think I know what I did wrong.

        This is what I'm doing now:

        Code:
        predict grandmean
        predict countryre hospitalre, reffects
        g countrymean = grandmean + countryre
        g hospitalmean = grandmean + countryre + hospitalre
        dotplot grandmean countrymean hospitalmean
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	34.7 KB
ID:	1356781



        But where does the remaining residual go? And is there a way to merge the three dotplots to resemble the distribution like the link in the previous post?

        Thanks again!

        Comment


        • #5
          But where does the remaining residual go?
          You can get the bottom level error term with -predict, residual-.

          One more thing: when you run -predict-, it calculates a result in every observation in the estimation sample. If you just -dotplot- or -histogram- those, each hospital or country will be counted as many times as it has observations. If your sample is completely balanced, then that's not a problem. But real world data is likely not to be that simple. So to get an accurate sense of the distribution at the upper levels you need to select just one observation per country and one per hospital for plotting. If you want to plot the country and hospital mean distributions on the same graph, probably the simplest way to do that is to "uncalculate" the unselected observations:

          Code:
          by country, sort: replace countrymean = . if _n > 1
          by hospital, sort: replace hospitalmean = . if _n > 1
          dotplot grandmean residual countrymean hospitalmean
          And is there a way to merge the three dotplots to resemble the distribution like the link in the previous post?
          I don't understand this question. The graphs in #3 and #4 look like they have the same general layout to me.

          Comment


          • #6
            You can get the bottom level error term with -predict, residual-.
            So, this is what I'm doing now:

            Code:
            predict grandmean
            predict countryre hospitalre, reffects
            predict residual, resid
            g countrymean = grandmean + countryre
            g hospitalmean = grandmean + countryre + hospitalre
            g resid = grandmean + countryre + hospitalre + residual
            dotplot grandmean countrymean hospitalmean resid
            Correct? Thank you for pointing out the problem with unbalanced data. Luckily, the data I'm working with is strongly balanced.

            I don't understand this question. The graphs in #3 and #4 look like they have the same general layout to me.
            The idea is to have a graph where the plots are overimposed on each other -- say grand mean in black, country mean in green, hospital mean in blue (possibly without the residual one, otherwise it would be too confusing), so that they are all on the same axis rather than separated. Thanks again for your help!

            Comment


            • #7
              That code looks correct to me.

              The idea is to have a graph where the plots are overimposed on each other -- say grand mean in black, country mean in green, hospital mean in blue (possibly without the residual one, otherwise it would be too confusing), so that they are all on the same axis rather than separated. Thanks again for your help!
              You can control the color of the dots in a dotplot with the -mcolor()- option. See -help dotplot- and select the link for marker options for more information. But I do not know of a way to get them all superimposed on the same axis with -dotplot-. Personally, it sounds to me like doing that would give a difficult to interpret jumble, but I suppose that is largely a matter of taste. If you want to bin your results a bit, you could accomplish this with a -graph bar- approach: you'd have to experiment with the bar spacing to get exactly the look you want.

              Comment


              • #8
                Dear Clyde / all,
                I recently started working again on this project to extend it, and I've come to a point where, in fact, the data is not balanced for some conditions, therefore the problem evidenced by Clyde in #5 arises. An example of my ML specification is (for any of the conditions):

                Code:
                xtmixed logcost i.teaching i.beds i.occupancy i.beds##i.occupancy i.ownership i.subcond
                > || country: r.paymentsystem, || hospitalid:,
                Therefore I have my fixed component (based on three years pooled data) and my random component divided into country and hospital observations. Now, depending on which condition I'm working on, the n of the sample could be equal to the hospital n (one observation for every hospital), or not (when -i.subcond- is used, I might have several cases in the same hospital, therefore grand n > hospital n). In the first case, I don't need to touch the hospitalid mean since there are no _n > 1.

                When I run the code in #6, this is what I get:

                Click image for larger version

Name:	hospitals.png
Views:	1
Size:	37.2 KB
ID:	1361477


                Now, in this particular case I have country n = 4, hospital n = 120, total n = 294. This is because the condition is divided in i.subcond according to three age groups (therefore there could be more than one observation in any hospital, where the individual observation is the average cost of a particular subcondition in a year). Therefore, the way I am interpreting this (but I might be wrong), is that the graph is plotting the distributions for every country (each one with its own grand mean), and for every subcondition.

                When I try to use Clyde's suggested code, I end up with four dots, one for each country, with no density and no distribution, which I would like to keep. I would like to understand if my interpretation of the graph is correct, and if that is the case, my idea is to try to separate the distributions for the four countries (for example, by coloring them differently). The same goes for the three age groups within hospital effects.

                I hope I was clear enough, and thanks again for all your help.

                Comment


                • #9
                  When I try to use Clyde's suggested code, I end up with four dots, one for each country, with no density and no distribution
                  When that code was written, nothing about subconditions was contemplated. That code was designed to give you one value of country mean for each country, and that is, indeed, what it gave you. If you want to disaggregate by subcondition, then the code has to be changed:

                  Code:
                  by country subcond, sort: replace countrymean = . if _n > 1
                  by hospital subcond, sort: replace hospitalmean = . if _n > 1
                  dotplot grandmean residual countrymean hospitalmean
                  Similarly, if you want to disaggregate by age groups then you need to put that into the -by- prefix.

                  Comment

                  Working...
                  X