Announcement

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

  • How to make group-level variable?

    Hello, I want to do multilevel analysis - and I have encountered a problem.
    I want to do an analysis about happiness level of students in class 1 elite class,
    BUT in my analysis, perception of class 2 students, non-elite students about elite education will be one of the predictor.


    The perception about class 1 by class 2 will be made with the code:

    egen group_perception=median(elite_perception) if class==2 bys(school)

    as the group_perception variable was made by the students in class 2, when I tried to use it in the multilevel analysis, the error : no observation occurs - which is not very surprising considering that the group_perception variable was only made with the students in class 2 and I am running multilevel analysis with the students in class 1.

    How would I be able to solve this problem? I am thinking about making just group-level variable with only the median value. Thanks.
    Last edited by Jake Hay; 19 Dec 2018, 09:46.

  • #2
    It sounds like you want to calculate the median perception using only the class == 2 observations as "inputs" to the calculation, but you want the results to appear in all observations. That would be:

    Code:
    egen group_perception = median(cond(class==2, perception, .))
    That said, this won't work in your regression either, because it will provide the same result for everybody: it will calculate the median value of perception among all class == 2 observations and record that in every observation of the variable group_perception. So group_perception is not a variable--it is a constant. Consequently, Stata (or any other software) will omit it from the regression.

    I think you need greater clarity on what you are actually trying to accomplish here. If you need additional assistance, please be sure to post example data that illustrates your problem, and be sure to use the -dataex- command to do that. If you are running version 15.1 or a fully updated version 14.2, it 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.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Are you wanting to calculate this by school? Is that what the "bys(school)" is for? If so, you need to add a by sort clause to Clyde's code.
      Code:
       
       by school, sort: egen group_perception = median(cond(class==2, perception, .))

      Comment


      • #4
        Leah, thank you for pointing that out. I don't recall seeing the -bys(school)- in the original post when I responded. Looking at the time stamps for my post and his edit, it is possible that it was put in while I was working on my response and it was unavailable to me. In any case, your correction makes a lot of sense!

        Comment


        • #5
          Thanks for your response.
          My data is a huge file, so I think using dataex doesn't make sense but thanks for the advice.

          I want to use european social survey, and to describe exactly what I want to do shortly,
          I want to see the perception of the natives about the immigrants, and use this variable to predict "immigrant"'s integration into the society - and it will be different depending on the country.
          and I calculated the median of the threat to see it.

          egen perception=median(threat) if immist1==0, by(cntry) /// immist1=0 means that it is a native

          and at last, my multilevel analysis will look like:

          xtmixed happy agea female religion perception if immist==1 || cntry: , mle variance ///immist==1 means that it is an immigrant


          and as you said, the median result will be the constant so I want to make it a country-level variable to predict but I am lost here.
          Thank you for your answer in advnace!


          Comment


          • #6
            My data is a huge file, so I think using dataex doesn't make sense but thanks for the advice.
            -dataex- will simply give the first 100 observations in your actual file which, in most situations, is sufficient for the purposes of illustrating the data and providing a reasonable extract of the data for testing code in. (Sometimes one needs to be a bit more selective about it or override the defaults and post a larger sample, but usually not.) So, in the future, please do use -dataex-. Knowing you are using the European social survey is only helpful to those who are familiar with it or at least have ready access to it--which limits the number of people who can respond helpfully.

            As to your coding problem, I believe what you want is:

            Code:
            by cntry, sort: egen perception=median(cond(immist1 == 0, threat, .))
            which would indeed define perception in each country as the median value of threat among non-immigrants of that country.

            Your multilevel model command looks like it would work as you have it. You might, however, want to update it to modern syntax if you are using the current version of Stata (15.1):
            Code:
            mixed happy agea female religion perception ifimmist==1 || cntry:
            The old name -xtmixed- is still understood by Stata, but -mixed- is the current correct name of the command. I have removed the mle and variance options because in current Stata these are the defaults, so they don't need to be specified.

            If you plan to subsequently calculate adjusted predicted outcomes by, say, age, or gender or religion defined subgroups, you would also benefit from using factor variable notation here so that -margins- can do all the hard work for you. (-help fvvarlist-, https://www3.nd.edu/~rwilliam/stats/Margins01.pdf)

            Comment

            Working...
            X