Announcement

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

  • Spatial matrices

    Dear All,

    I am constructing some spatial matrices. I have the following coordinates (where _ID identifies the region]:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte _ID double(_CX _CY)
     1   4.238880806932661   51.0381795797637
     2  4.3705583790493465 50.836004194715464
     3   5.008810757595679 50.314774734858666
     4   9.048183441796569 48.539120289602224
     5   7.562275359577741  51.47982042591398
     6   9.812804864412303  54.18267278074142
     7   9.031162177003864  50.60312939159003
     8   9.159366625166365  52.76822315642757
     9  11.419101982885781 48.946844237635794
    10  -7.910502751973481 42.756695468623235
    11 -2.6162526114576328   43.0433556472304
    12  -.6599335303805115  41.51954561052405
    13 -6.1508402965923334  39.19149210853352
    14  -.5548950769729665 39.401518880927945
    15 -4.5754816643187635  37.46337461821722
    16 -1.4853899956423013  38.00235670507524
    17   2.912655060412556  39.57419360329185
    18 -15.668155079521242  28.34153154991463
    19  -4.782005139037015  41.75429637927728
    20  -3.716852162382493   40.4950465162602
    21 -3.0047329353222882 39.581044916387285
    22  1.5289116704222079 41.798474451952394
    23   5.289817422604617  43.58430558367101
    24   .8387347371287599 44.338785949212514
    25   4.542686675219991 45.517655466556306
    26   2.715756068693901   50.4723663905693
    27   6.347598245580899  48.17247256770211
    28 -1.2573091563937455  47.31613047630117
    29  2.5381327644238603 48.292024223497016
    30  2.5033493032137306  48.70929509840815
    31   9.769067362637838 45.620628170558334
    32    8.70486347100189 44.264875054299885
    33  14.840443948886977  40.85976362267415
    34   6.002563143592553 52.289497160963705
    35   4.610661457311046 52.097751734967886
    36   6.286832650414219  53.05531323841722
    37   5.410618051098782  51.45495894313609
    38  12.766735405438116  41.98000397761924
    39   16.61930407162165 40.984592383451606
    40  11.126082947167264  43.45078957457015
    41  11.038069574762444   44.5255464813964
    42  16.347610749835038  39.06787919421982
    43  -7.678641274716713  41.45752851199433
    44  -8.131341102441208  37.24373918509147
    45  -9.045001782531612 38.737506880267716
    46  -8.014034985515947 38.490875924556505
    47  -7.993077306851027    40.133696972864
    48 -27.282535206945695  38.34807291397858
    49 -16.951549883485765 32.751393510571525
    50  -4.184661705559883   56.8395664536139
    51  -6.694157393386353  54.60974329011341
    52 -3.7637283805348782  52.33639746515705
    53  -3.132137327606542  51.00022732220863
    54  -.8065406679657919 52.926936493964085
    55  -1.229212191489712  53.96554242684258
    56 -2.2708002631811794 52.480446124299874
    57   7.861511916378289 45.137521996830564
    58  11.884422926292736  46.03477268799009
    59  12.831751280511003   43.1676278995571
    60  14.854738234591938  41.45734716847926
    61  11.626845963292368 38.819447705708036
    62  -5.337484002822747  43.26057900387312
    63  -1.929712993209706   42.5393129401837
    64   9.556973106152082  53.42845954603848
    65   7.392801608484208  49.85361696167447
    66  -2.342632760789819  54.83099522558365
    67 -2.5550432219906885 53.548812742891116
    68    .955481263001568  52.67083597810112
    69 -.14736474261673754  51.61526791744084
    end
    While I could use spmatrix to create my matrix, the user written spwmatrix routine would allow me to generate a matrix choosing the k nearest neighbours. With the data above, I could write:

    Code:
    spwmatrix gecon _CY _CX, wn(myW) knn(5)
    However, if I try to use the myW matrix with spregress I get the following error:

    Code:
    weighting matrix myW not found
    I presume that this is due to the fact that spregress does not recognize any spatial object which has not been created using spmatrix.

    I tried to generate the above matrix and then to import it using spmatrix import:

    Code:
    spwmatrix gecon _CY _CX, wn(W5) knn(5) xport(myW, txt) replace
    insheet using "myW.txt", delim(" ") clear
    spmatrix import myW2 using myW.txt
    The problem is that the matrix myW2 is not normalized (spectral):

    Code:
    . spmatrix summarize my
    
    Weighting matrix  my
    ---------------------------------------
               Type |               custom
      Normalization |                 none
          Dimension |              69 x 69
    Elements        |
       minimum      |                    0
       minimum > 0  |                    1
       mean         |             .0724638
       max          |                    1
    ---------------------------------------

    Any idea on how to normalize the matrix? Thanks in advance.

  • #2
    Here is one way.

    Code:
    spwmatrix gecon _CY _CX, wn(myW) knn(5)
    
    mata: W = st_matrix("myW")
    mata: id = 1::cols(W)
    
    spmatrix spfrommata spW = W id
    spmatrix summarize spW

    Comment


    • #3
      Thanks a lot Rafal!

      Comment


      • #4
        Dear Rafal,

        Unfortunately, I have another problem. Basically, I need to do the inverse process, too. Suppose I generate a contiguity matrix with spmatrix, i.e.

        Code:
        spmatrix create contiguity cW, replace
        Then, I export it in mata

        Code:
        spmatrix matafromsp W id = cW
        Finally, I would like to import it back using spwmatrix. I read the help file for spwmatrix and it should be possible to import a Matrix from mata, if I am correct.

        I tried:

        Code:
        spwmatrix import using W, wname(cWspx) mataf
        In this case I get:

        Code:
        file W.raw not found
        Which is the problem with my code?

        Thanks for your help.

        Dario


        Last edited by Dario Maimone Ansaldo Patti; 14 Sep 2017, 08:23.

        Comment


        • #5
          spwmatrix spfrommata and spwmatrix matafromsp should do the job, for example:
          Code:
          gen id = _n
          spset id, coord(_CX _CY)
          spmatrix create idistance spW, norm(none)
          spmatrix summarize spW
          mata: mata des
          
          spmatrix matafromsp W id = spW
          mata: mata des
          
          spmatrix spfrommata newW = W id
          mata: mata des
          
          spmatrix summarize spW
          spmatrix summarize newW

          Comment


          • #6
            Thanks Rafal. Do you mean spwmatrix spfrommata (as in the first line of your reply) or spmatrix spfrommata (as in the code lines you included)?

            As in my post, I would like to generate a Matrix using spmatrix. Then, I would like to export it in mata and, finally, to import the mata object using spwmatrix.

            The reason is that I want to use genmsp programme and spatlsa to plot hot-cold maps. However, spatlsa does not support matrix generated using spmatrix.
            Last edited by Dario Maimone Ansaldo Patti; 14 Sep 2017, 09:50.

            Comment


            • #7
              You can move between Sp and Mata using spmatrix matafromsp and spmatrix spfrommata, there is no need to use spmatrix import.
              Last edited by Rafal Raciborski (StataCorp); 14 Sep 2017, 10:36.

              Comment


              • #8
                Thanks Rafal. I was non aware of this option for spwmatrix.

                Comment


                • #9
                  I meant spmatrix, edited the previous post.

                  Comment


                  • #10
                    YThanks. however this does not solve my question. Is it possible to import a mata object using spwmatrix? In fact, I am not using spmatrix import.

                    The steps I am following are:

                    a - generate a spatial Matrix using spmatrix

                    b - exporting the Matrix in mata using spmatrix matafromsp

                    c - importing the mata object using spwmatrix
                    Last edited by Dario Maimone Ansaldo Patti; 14 Sep 2017, 10:57.

                    Comment


                    • #11
                      You can copy a Mata matrix into Stata, but you have to rename the rows and columns of that matrix so it looks like a matrix created by spwmatrix. Below I use the first 11 observations from the data you posted to illustrate.
                      Code:
                      keep in 1/11
                      
                      set seed 12345
                      gen y = rnormal()
                      spwmatrix gecon _CY _CX, wn(myW) knn(2)
                      mat list myW
                      spatlsa y, weights(myW) moran
                      
                      local row : rowfullnames myW
                      local col : rowfullnames myW
                      
                      mata:
                          W = st_matrix("myW")
                          W = 2*W
                          st_matrix("newW",W)
                      end
                      
                      mat rownames newW = `row'
                      mat colnames newW = `col'
                      
                      spatlsa y, weights(newW) moran

                      Comment


                      • #12
                        Thanks for your reply.

                        Ideally, I would like to create the matrix myW using spmatrix. However, your code does not work if I generate the matrix using spmatrix.

                        Code:
                        keep in 1/11
                        
                        set seed 12345
                        gen y = rnormal()
                        spmatrix create contiguity myW, replace 
                        
                        local row : rowfullnames myW
                        local col : rowfullnames myW
                        
                        mata:
                            W = st_matrix("myW")
                            W = 2*W
                            st_matrix("newW",W)
                        end
                        
                        mat rownames newW = `row'
                        mat colnames newW = `col'
                        
                        spatlsa y, weights(newW) moran
                        Any possibility to construct a Matrix with spmatrix, export it in Mata and writing back in Stata using spwmatrix?

                        Comment


                        • #13
                          Code:
                          gen id = _n
                          spset id, coord(_CX _CY)
                          spmatrix create idistance spW
                          spmatrix matafromsp W id = spW
                          clear
                          getmata r*=W, double
                          save tmp, replace
                          spwmatrix import using tmp, wname(WW) dta

                          Comment


                          • #14
                            Thank you. It worked.

                            Comment


                            • #15
                              Hi,

                              I am running spatial fixed effect model (spxtreg) and I have created idistance weight matrix using _CX _CY.

                              spset PSUNO, coord(_CX _CY)
                              spset, modify coordsys(latlong, kilometers)
                              spmatrix clear
                              spmatrix create idistance M if year==2008
                              spmatrix summarize M

                              When I run fixed effect model, coefficient of all the non-lag variables are omitted. But same problem do not arise in the case of random effect.

                              I have no missing cell in my data set. Can you suggest me what is wrong in my data set and how to solve this problem?

                              Thanks

                              Hari

                              Comment

                              Working...
                              X