Announcement

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

  • How to get Spatial Weight Matrix?

    Hello Everyone, I hope you are safe in your places.

    I am using Cross -Sectional data from the World Bank World Development Indicators (WDI).

    I wanted to see how Spatial variable(country) impacts on the Fertility rates across the different countries, for that First I need to get the SPATIAL WEIGHT MATRIX.

    So, please help me with this issue.

    Here, the Dependent variable is Fertility rates and a Total of 201 countries. (ALL VARIABLES ARE IN AVERAGES)

    For better understanding, I am giving my DATA Example below.

    Thank you.

    copy starting from the next line --------------- -------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str52 country float(Avg_Bbranch Avg_FR Avg_Alit Avg_GDPPC Avg_GHE)
    "Afghanistan"         1.765751  5.854357 37.38042  473.9886 43.80099
    "Albania"             20.14792 1.6875714 97.04301  3983.294 229.0268
    "Algeria"             5.107783  2.825143 76.39752  4317.603 227.6935
    "Angola"              7.133926    6.1115 66.03011 3683.4175 103.1317
    "Antigua and Barbuda" 32.54915 2.0163572    98.95 14053.454  629.448
    end
    copy up to and including the previous line ---------- -------
    Last edited by GOVINDAPURAM SURESH; 19 May 2020, 00:43. Reason: Spatial

  • #2
    Anyone please help me with this

    Comment


    • #3
      This should be enough to get you started. I'm not sure what kind of spatial weight matrix you want or if you have a shapefile. This code assumes you don't. I would carefully read the Stata SPATIAL AUTOREGRESSIVE
      MODELS REFERENCE MANUAL
      . You can adjust the code below to suit your exact needs (eg. inverse distance/contiguity matrix, spatial lag, error lag, etc.).

      Code:
      clear
      
      * shapefile source https://hub.arcgis.com/datasets/a21fdb46d23e4ef896f31475217cbb08_1 
      !curl -L https://opendata.arcgis.com/datasets/a21fdb46d23e4ef896f31475217cbb08_1.zip > a21fdb46d23e4ef896f31475217cbb08_1.zip
      unzipfile "a21fdb46d23e4ef896f31475217cbb08_1.zip"
      local files: dir . files "*.shp"
      spshape2dta `files' , replace 
      
      
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str52 country float(Avg_Bbranch Avg_FR Avg_Alit Avg_GDPPC Avg_GHE)
      "Afghanistan"         1.765751  5.854357 37.38042  473.9886 43.80099
      "Albania"             20.14792 1.6875714 97.04301  3983.294 229.0268
      "Algeria"             5.107783  2.825143 76.39752  4317.603 227.6935
      "Angola"              7.133926    6.1115 66.03011 3683.4175 103.1317
      "Antigua and Barbuda" 32.54915 2.0163572    98.95 14053.454  629.448
      end
      
      clonevar CNTRY_NAME = country
      merge 1:1 CNTRY_NAME using  "99bfd9e7-bb42-4728-87b5-07f8c8ac631c2020328-1-1vef4ev.lu5nk.dta", keep(match) nogen 
      
      * i'm using idistance because contiguity makes less sense in this context. 
      spmatrix create idistance W, replace 
      
      * adjust your regression here 
           // dvarlag(spmatname)            spatially lagged dependent variable
           // errorlag(spmatname)           spatially lagged errors
           // ivarlag(spmatname : varlist) 
      
      *  spatially lagged dependent variable example w/ no independent variables because N is small
      spregress Avg_FR, gs2sls dvarlag(W) 
      
      . spregress Avg_FR, gs2sls dvarlag(W) 
        (5 observations)
        (5 observations (places) used)
        (weighting matrix defines 5 places)
      
      Spatial autoregressive model                    Number of obs     =          5
      GS2SLS estimates                                Wald chi2(1)      =       0.80
                                                      Prob > chi2       =     0.3722
                                                      Pseudo R2         =     0.1220
      
      ------------------------------------------------------------------------------
            Avg_FR |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
      -------------+----------------------------------------------------------------
      Avg_FR       |
             _cons |   5.716616   2.372568     2.41   0.016     1.066469    10.36676
      -------------+----------------------------------------------------------------
      W            |
            Avg_FR |  -.6148806   .6891113    -0.89   0.372    -1.965514    .7357528
      ------------------------------------------------------------------------------
      Wald test of spatial terms:          chi2(1) = 0.80       Prob > chi2 = 0.3722

      Comment


      • #4
        Thanks very much, Justin Sir. Would you please help me getting this worked for Spatial Panel Data.

        Thanks in Advance.

        Comment


        • #5
          See the help file for spxtregress for a worked example.

          Comment


          • #6
            Sir, while merging data file with shapefile it is showing that the country is not defined like that

            Comment


            • #7
              Without seeing what you're seeing I can't provide any insightful feedback. I provided an example in #3 to follow.

              See help merge for what I assume is the issue you're encountering.

              Comment


              • #8
                Thanks Sir

                Comment


                • #9
                  Hello everyone,

                  I am having a problem when creating the spatial weighting matrices. My dependant variable is TransactionPrice (averaged) and the independent variables are distances to various amenities within Hong Kong (averages too). A section of the data is as follows:

                  input double(_CX _CY Transactio MTR_Distan Public_Hou EV_Distanc URA_DIST) float _ID
                  113.9345800000001 22.282470000000046 862.836363636 824.628421633 148.240345165 800.997461171 19600.4120175 1
                  113.93639000000007 22.280550000000062 952.3 868.661747702 132.70744553 829.59234017 19590.4621557 2
                  113.94356540000001 22.319290160000037 4408.234375 426.280901987 2333.29741139 139.059228819 17546.5927684 3

                  When creating the contiguity and idistance matrices, the (N x N) figures are different. They are (381 x 381) and (627 x 627), respectively. This is a problem when trying to run the spregress command as the following error is displayed:

                  _IDs in weighting matrix M do not match _IDs in weighting matrix W
                  There are places in one matrix that are not in the other.

                  Please may someone assist me in getting these (N x N) figures to equal one another and for the regression to work. I feel that this is a problem with my data but cannot find much help online.

                  Thank you in advance!

                  Comment


                  • #10
                    You cannot create a contiguity matrix without a shapefile. From your data example above you're limited to a distance matrix. If you need additional help you might start a new thread and explain what you've typed and what Stata is telling you in addition to providing a data example using dataex.

                    Comment


                    • #11
                      Justin Niakamal i want to know if contiguity and distance are the only two options to create spmatrix. i have a set of Indo-Pacific Economic Framework (IPEF) countries containing Australia, Brunei, Fiji, Indonesia, India, Japan, Korea, Malaysia, Philippines, Singapore, New-Zealand, USA and Vietnam. what should i use contiguity or distance.
                      thank you.

                      Comment

                      Working...
                      X