Announcement

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

  • export spatial weight matrix

    Hi,

    I'm exploiting district-level unbalanced panel data.
    I used the following command to generate a weight matrix w
    HTML Code:
    spwmatrix gecon _CY _CX , wname(w) wtype(inv) cart dband(0 100) rowstand
    And afterward, it said-
    HTML Code:
    Inverse distance (alpha = 1) spatial weights matrix (13892 x 13892) calculated successfully and the following action(s) taken:
    
     - Spatial weights matrix  created as Stata object(s): w.
    
     - Spatial weights matrix has been row-standardized.
    Now I want to use
    HTML Code:
    xtmoran RYield, wname(w)
    where w needs to be in dta format.

    I don't know how to export the above-generated weight matrix as dta file.
    I can't find the appropriate code.

    Can you please advise,

    Thank you,

  • #2
    Code:
    help spmatrix export

    Comment


    • #3
      Thank you Andrew Musau,
      But this export is in text format and Stata 17 crashes as I import it.
      I need weights in a dta format to perform xtmoran.
      Please advise.

      Comment


      • #4
        I do not use these commands, but as long as you have Stata SE or Stata MP, it should cope with the size of your matrix.

        spatial weights matrix (13892 x 13892)
        I would suggest exporting as a CSV file, importing back to Stata and then splitting and destringing the values.

        Code:
        spwmatrix gecon _CY _CX , wname(w) wtype(inv) cart dband(0 100) rowstand
        spmatrix export w using myfile.csv, replace
        import delimited "myfile.csv", encoding(Big5) clear

        Comment


        • #5
          Thanks again Andrew Musau,

          Which command would you use instead of spwmatrix?

          I need to generate a weight matrix of the panel.
          Then I need to do moran I and LM test to choose between lag and error models.
          And I finally need to report the results of the suitable spatial model and show impact on a map.

          Is it compulsory to have a balanced panel?

          Awaits your reply,

          Comment


          • #6
            As I said, I do not use these commands. The much help that I can give is to answer your question on how to convert the weighting matrix to a dataset. I would suggest that you start a new thread if you have a specific question on the commands or your model/data.
            Last edited by Andrew Musau; 28 Mar 2022, 04:38.

            Comment


            • #7
              Thank you Andrew Musau,

              I created a 641 x 641 queen contiguity matrix in GeoDa and used the following command-
              HTML Code:
              spwmatrix import using queen_weight.gal, wname(qweight) xport(qweight, txt)
              insheet using weights.txt, delim(" ") clear
              But for some reason, the order of the observations change as I convert it to txt and then export-import to dta import it to Stata.
              Observation 431 in gal file is becoming observation 1 in Stata. Likewise, observation 619 in gal file is becoming observation 2 in Stata.
              I wanted the observations in dta file to remain in the same order as gal file to do panel analysis later.

              Can you please advise?

              Comment


              • #8
                Once you import the matrix to Stata, you can sort using a variable that maps the original order to the order in the matrix. I do not know if the matrix rows are sorted alphabetically, you need to figure this out.

                Code:
                sysuse auto, clear
                keep in 1/20
                set seed 04042022
                gen rsort= rnormal()
                sort rsort
                list make price mpg, sep(0)
                *GENERATE ORIGINAL ORDER VARIABLE
                gen order_old=_n
                
                *GENERATE NEW ORDER VARIABLE (ALPHABETICAL SORTING)
                sort make
                gen order_new=_n
                keep order*
                tempfile order
                save `order'
                
                *NEW DATASET WITH ALPHABETICAL SORTING
                sysuse auto, clear
                keep in 1/20
                sort make
                list make price mpg, sep(0)
                gen order_new=_n
                *MERGE WITH DATASET WITH ORDER VARIABLES
                merge 1:1 order_new using `order', nogen
                sort order_old
                list make price mpg, sep(0)
                Res.:

                Code:
                . list make price mpg, sep(0)
                
                     +----------------------------------+
                     | make                 price   mpg |
                     |----------------------------------|
                  1. | Buick LeSabre        5,788    18 |
                  2. | AMC Concord          4,099    22 |
                  3. | Chev. Monza          3,667    24 |
                  4. | Buick Regal          5,189    20 |
                  5. | Chev. Nova           3,955    19 |
                  6. | Cad. Eldorado       14,500    14 |
                  7. | AMC Pacer            4,749    17 |
                  8. | Cad. Seville        15,906    21 |
                  9. | AMC Spirit           3,799    22 |
                 10. | Chev. Impala         5,705    16 |
                 11. | Dodge Colt           3,984    30 |
                 12. | Buick Skylark        4,082    19 |
                 13. | Buick Century        4,816    20 |
                 14. | Chev. Chevette       3,299    29 |
                 15. | Buick Electra        7,827    15 |
                 16. | Chev. Malibu         4,504    22 |
                 17. | Chev. Monte Carlo    5,104    22 |
                 18. | Cad. Deville        11,385    14 |
                 19. | Buick Opel           4,453    26 |
                 20. | Buick Riviera       10,372    16 |
                     +----------------------------------+
                
                .
                
                .
                .
                .
                . *NEW DATASET WITH ALPHABETICAL SORTING
                
                .
                . sysuse auto, clear
                (1978 Automobile Data)
                
                .
                . keep in 1/20
                (54 observations deleted)
                
                .
                . sort make
                
                .
                . list make price mpg, sep(0)
                
                     +----------------------------------+
                     | make                 price   mpg |
                     |----------------------------------|
                  1. | AMC Concord          4,099    22 |
                  2. | AMC Pacer            4,749    17 |
                  3. | AMC Spirit           3,799    22 |
                  4. | Buick Century        4,816    20 |
                  5. | Buick Electra        7,827    15 |
                  6. | Buick LeSabre        5,788    18 |
                  7. | Buick Opel           4,453    26 |
                  8. | Buick Regal          5,189    20 |
                  9. | Buick Riviera       10,372    16 |
                 10. | Buick Skylark        4,082    19 |
                 11. | Cad. Deville        11,385    14 |
                 12. | Cad. Eldorado       14,500    14 |
                 13. | Cad. Seville        15,906    21 |
                 14. | Chev. Chevette       3,299    29 |
                 15. | Chev. Impala         5,705    16 |
                 16. | Chev. Malibu         4,504    22 |
                 17. | Chev. Monte Carlo    5,104    22 |
                 18. | Chev. Monza          3,667    24 |
                 19. | Chev. Nova           3,955    19 |
                 20. | Dodge Colt           3,984    30 |
                     +----------------------------------+
                
                .
                . gen order_new=_n
                
                .
                . *MERGE WITH DATASET WITH ORDER VARIABLES
                
                .
                . merge 1:1 order_new using `order', nogen
                (label origin already defined)
                
                    Result                           # of obs.
                    -----------------------------------------
                    not matched                             0
                    matched                                20  
                    -----------------------------------------
                
                .
                . sort order_old
                
                .
                . list make price mpg, sep(0)
                
                     +----------------------------------+
                     | make                 price   mpg |
                     |----------------------------------|
                  1. | Buick LeSabre        5,788    18 |
                  2. | AMC Concord          4,099    22 |
                  3. | Chev. Monza          3,667    24 |
                  4. | Buick Regal          5,189    20 |
                  5. | Chev. Nova           3,955    19 |
                  6. | Cad. Eldorado       14,500    14 |
                  7. | AMC Pacer            4,749    17 |
                  8. | Cad. Seville        15,906    21 |
                  9. | AMC Spirit           3,799    22 |
                 10. | Chev. Impala         5,705    16 |
                 11. | Dodge Colt           3,984    30 |
                 12. | Buick Skylark        4,082    19 |
                 13. | Buick Century        4,816    20 |
                 14. | Chev. Chevette       3,299    29 |
                 15. | Buick Electra        7,827    15 |
                 16. | Chev. Malibu         4,504    22 |
                 17. | Chev. Monte Carlo    5,104    22 |
                 18. | Cad. Deville        11,385    14 |
                 19. | Buick Opel           4,453    26 |
                 20. | Buick Riviera       10,372    16 |
                     +----------------------------------+
                
                .
                Last edited by Andrew Musau; 04 Apr 2022, 06:28.

                Comment


                • #9
                  Thanks again,

                  Since I have panel data, the order was important during export-import.
                  Say I have n number of ids then I need n×n weight matrix.
                  A balanced panel is a must for spatial analysis.

                  That command spwmatrix was changing the order while importing from gal file.
                  So I thought of using spmat insteed.
                  And guess what, it worked!

                  HTML Code:
                  spmat import qweight using queen_weight.gal, geoda
                  spmat export qweight using queen_weight.txt
                  insheet using queen_weight.txt, delimiter(" ") clear
                  Only need minor corrections afterward.
                  And then
                  HTML Code:
                  save "queen_weight.dta"
                  spmat dta queen_weight m*, normalize(row)
                  spmat summarize queen_weight, links detail
                  Now I can try xsmle, let's see.

                  Referring:
                  Last edited by Souryabrata Mohapatra; 04 Apr 2022, 16:45.

                  Comment

                  Working...
                  X