Announcement

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

  • matrix list

    Dear Statlist

    My question looks simple but since I am beginning user in STATA, I could not find a solution yet.

    For example, I would like to list the matrix (b) by location (urban and rural) or quintiles (income quintiles) something like that.

    I tried some commands e.g.
    Code:
     matrix list b if location==1 or matrix list b, by location ect.
    but non of them are working

    I highly appreciate any your helps

    Thank you

  • #2
    The -matrix list- command does not allow options like that. You either list the whole matrix, or you don't. If you want to extract a submatrix and then list that, you can do that. See -help matrix define- for details and examples.

    But I don't even know what you even want to do with that command. I can't get my mind around what your intent is here. What is the relationship between the matrix b and "location." Perhaps if you made up an example of a matrix and showed us that, as well as what you would like to get from it, we could help.

    Comment


    • #3
      Thank you Clyde,

      I think better I post my code over here that give to us more clear picture, I am computing income elasticities, ( b -is income elasticities) and I would like to obtain income elasticities by location,

      Code:
       matrix elas_income=J(1,7,0)
      forvalues i=1/7{
      tempvar mu`i' elas`i'
      g double `mu`i''=b`i'[1,1] +( eta1`i'[1,1]*`z1mean'+eta2`i'[1,1]*`z2mean')+((2*l`i'[1,1]/(`bp'*`cofp'))*(`lnmmean'-`ap'-`mbar'))
      g double `elas`i''=(`mu`i''/`w`i'mean')+1
      sum `elas`i''
      matrix elas_income[1, `i']=r(mean)
      }
      matrix list elas_income

      I highly appreciate if you could look over my codes and suggest how I can list income by location



      Thank you

      Comment


      • #4
        Clyde is right as usual. Matrices have nothing do with the dataset in memory, so if and in qualifiers mean nothing in a matrix context. That is presumably why they are illegal, as they could do nothing useful.
        Last edited by Nick Cox; 07 Apr 2016, 13:02.

        Comment


        • #5
          Well, I still don't understand what you want to do. What does location have to do with anything in the code you showed?

          Comment


          • #6
            Dear Clyde,

            The present code is computing income elasticities at national ( average level). I have another variable that is "location- takes 1 if urban and 2 if rural".

            With above code I computed income elasticities for both ( urban and rural)

            My purpose is to include the variable of location in my code to compute income elasticities by location as urban and rural.


            Last edited by David Achiles; 07 Apr 2016, 14:03.

            Comment


            • #7
              I don't understand the details at all, and the code is not self-contained any way, but anything that goes into a matrix element could be calculated as a scalar first.

              Comment


              • #8
                The presentation is unclear, but perhaps you mean something like this: you have a dichotomous variable named location coded 1 and 2, and you have some code that calculates elasticities which you have written to apply to the entire data set. But you want to actually apply them separately to urban and rural locations. I don't actually understand your code at all, and it makes reference to various things that are nowhere defined in what you show. So I'm just going to give you a schematic for how you might go about it.

                Code:
                forvalues loc = 1/2 {
                    matrix elas_income_`loc'=J(1,7,0)
                    forvalues i=1/7{
                        tempvar mu`i' elas`i'
                        g double `mu`i''=b`i'[1,1] +( eta1`i'[1,1]*`z1mean'+eta2`i'[1,1]*`z2mean') ///
                            +((2*l`i'[1,1]/(`bp'*`cofp'))*(`lnmmean'-`ap'-`mbar')) if location == `loc'
                        g double `elas`i''=(`mu`i''/`w`i'mean')+1 if location == `loc'
                        sum `elas`i'' if location == `loc'
                        matrix elas_income_`loc'[1, `i']=r(mean)
                       drop `mu`i'' `elas`i''
                    }
                }
                matrix list elas_income_1
                matrix list elas_income_2
                Notes:
                1. This creates two separate matrices, one for urban and one for rural. If there is reason to combine them, you can, of course, do that either vertically or horizontally with Stata's matrix commands.
                2. The code refers to a bunch of local macros that are not defined here. If those macros take on the same values, regardless of location, then this code is fine. But if the values of those macros should be different for urban and rural calculations, then the definitions of the macros need to be placed inside the -forvalues- loop and need to modified so that they do there calculations appropriately for those restrictions. As I have no idea what these things refer to, I can't advise more specifically than that.

                I hope this is what you meant. If not, please try to provide a clearer explanation, with more complete code.

                Comment


                • #9
                  Thank you so much,,,, it is exactly what I wanted...

                  Comment

                  Working...
                  X