Announcement

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

  • Crossed effects models with covariates

    Hi,

    I'm relatively new to STATA, and am struggling to understand a particular code that explains how covariates can be entered into crossed effects models at both levels.
    xtmixed asset || _all: R.country tax || industry: size
    Where 'tax' is the covariate that is specified to have a random slope for each country (country level). However, this syntax includes 'tax' at the _all: level and so doesn't really do the job. The author of this syntax has then written the following which is what I cannot understand.
    tab country, gen(id_country) unab idvar: id_country* foreach v of local idvar {
    gen tax_`v ́ = tax*`v ́ }
    xtmixed asset || _all: R.country ||_all: tax_*, cov(identity) nocons /// || industry: size

    What does the gen (id_country) syntax do? Because it is not generating a new variable in the dataset, so what is its purpose?
    And also, foreach v of local idvar... code..

    I've tried reading up the syntax manual for STATA but am still unable to get any clarity. Can someone explain this? Or has anyone used a similar syntax and can shed some light?

    Thank you.

    Ahuti

  • #2
    What does the gen (id_country) syntax do? Because it is not generating a new variable in the dataset, so what is its purpose?


    It does generate new variables in the dataset. They will be called id_country1, id_country2, etc. They will be indicator variables for the different countries. If that has not happened, then something is wrong. I suggest you post the complete and exact code and output so we can see what is happening here.

    Also, when posting code, please do not run separate lines of code together on one line: it makes the code very difficult to understand. And, please post the code inside code delimiters. If you are not familiar with code delimiters, read Forum FAQ #12.

    Comment


    • #3
      Hi Clyde, thanks for responding. Apologies about the messed up code, will use the delimiters from now on.

      You're right, it generated a new variable. I restarted the programme and imported the dataset again, and somehow it worked. The code I pasted above was an example, which I am trying to apply to my model. As in my previous post to which you had responded regarding crossed effects multilevel models, I am now attempting to fit a random effect for my predictor variable on both the person as well as question number level. The person level is simple, as shown below, but am getting stuck with specifying it foe the question number level.

      Code:
      mixed Mood Media_use i.Batch_No i.Batch_No#i.study_day_number1 || ///
      _all: R.Question_number ||  ID: Media_use
      I have coped the code I found and replaced it with the relevant variables. This is as follows:

      Code:
      tab ID, gen(id_question) 
      unab idvar: id_question*
      foreach v of local idvar {
        gen question_`v ́ = Question_number*`v ́
      }
      I have understood the first two lines of the code.

      However, the I am getting confused by the foreach command. What exactly am I asking STATA to compute for each variable in the new variable list generated previously, i.e. id_question? And should I then. replace 'local idvar' with 'id_question*'?

      Comment


      • #4
        So -tab ID, gen(id_question)- generates a bunch of 0/1 variables id_question1, idquestion2, etc., and the -unab- command puts the list of those variable names into local macro idvar.

        Then the -foreach- command causes Stata to loop over the contents of local macro idvar. That is, it causes local macro v to successively take on values id_question1, id_question2, etc. as the loop iterates. I don't know what the variable Question_number is, but apparently the loop replaces all of these 0/1 variables by 0/Question_number variables. Why this is done, I don't know.

        I don't think I'm going to be able to directly help you with the larger problem of adding random slopes to a crossed random effects model. I've never done it and I don't think I've ever seen it done before. If you get it working, please do post back with your solution so that I and others can learn it.

        Comment


        • #5
          Thanks for explaining the code Clyde, makes a lot more sense now. I found the following resource that explains how the syntax creates a variable that can then be entered into the model as a random slope, but I haven't managed to get it working yet. Posting it here for your information in case you wanted to look at it. Thanks for your help!

          " If I write:

          Code:
          mixed asset || _all: R.country tax || industry: size
          Then variable tax will be at the "_all" level; this will imply only one realization per coefficient (i.e., a random variable), which will be the same for all the dataset. This is not only not what we want, but also it is a model not identified (Why?).

          What we want to do is to create a set of random coefficients for my covariate, with the same variance, independet, and a different "realization" of this random coefficient for each country. This can be done as follows:

          Code:
          tab country, gen(id_country)
          unab idvar: id_country*
          foreach v of local idvar {
             gen tax_`v ́ = tax*`v ́
          }
          Code:
          mixed asset || _all: R.country ||_all: tax_*, cov(identity) nocons || industry: size
          I am estimating a set of random coefficients for tax, a different realization for each country, and I’m using cov(identity) to establish that these coefficients should be i.i.d."
          Last edited by Ahuti Friebel; 18 Nov 2019, 04:53.

          Comment


          • #6
            Sorry, realised that the codes are messed up again. Here they are in correct format:

            Code:
            tab country, gen(id_country) 
            unab idvar: id_country*
            foreach v of local idvar {
              gen tax_`v ́ = tax*`v ́
            }
            and

            Code:
             mixed asset || _all: R.country ||_all: tax_*, cov(identity) nocons ///
            || industry: size

            Comment


            • #7
              I think I get the idea now. The variables tax_`v' being created in the loop represent id_country#tax interactions, which is another way of thinking about random slopes at the country level. Very clever!

              Then variable tax will be at the "_all" level; this will imply only one realization per coefficient (i.e., a random variable), which will be the same for all the dataset. This is not only not what we want, but also it is a model not identified (Why?).
              Whenever you have a "random effect" that is set up at the level of the individual observation (= at the _all: level), it is colinear with the residual error term, hence the model is unidentified.


              Comment

              Working...
              X