Announcement

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

  • Adjacency Variable

    Hello everyone,

    I am working with spatial data. My unit of analysis is ZIPCODE and my variable of interest is a 0\1 categorical variable ("gentrified"). I am interested in generating a dummy variable "neighbour_to_gentrified" ==1 if the ZIPCODE has gentrified==0 and is adjacent to a ZIPCODE with gentrified==1. Basically, I want to identify non gentrified ZIPCODE which are adjacent to a gentrified ZIPCODE. I have a shapefile and I have spset my data in order to generate an adjacency matrix W, but I am not able to generate such a specific variable.
    Can you suggest to me how to proceed?

    Thank you very much!

  • #2
    Can you show us something of what your adjacency matrix looks like, and explain how the rows/columns of that matrix are linked to your ZIPCODE variable? Perhaps rows and columns are numbered according to the sort order of your dataset?

    Comment


    • #3
      Here is first take, using the eight counties in Connecticut as an example.

      The first half of this code, up to the spmap statement, is just getting a workable example data set. The key part is the Mata code. Take the weight matrix, multiply by the gentrified dummy variable, extract the upper and lower triangles of the result to reconstruct the spatial weight matrix:


      Code:
      clear*
      
      copy https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_county_20m.zip ., replace
      unzipfile cb_2018_us_county_20m.zip, replace
      spshape2dta cb_2018_us_county_20m, replace
      
      use cb_2018_us_county_20m.dta,clear
      keep if STATEFP == "09"
      //Gentrified counties:  001,  015
      //Adjacent counties to gentrified:  (005 & 009), (011 & 013)
      gen gentrified = 0
      replace gentrified = 1 if COUNTYFP == "001" | COUNTYFP == "015"
      keep _ID _CX _CY STATE COUNTYF NAME gent
      save ct_data, replace
      
      //keep only Connecticut shapefile
      use cb_2018_us_county_20m_shp,clear
      gen order = _n
      save,replace
      use ct_data
      merge 1:m _ID using cb_2018_us_county_20m_shp
      keep if _m == 3
      drop _merge
      sort _ID order
      save ct_shapefile, replace
      
      use ct_data,clear
      //Map of gentrified counties
      spmap gentrified using ct_shapefile,id(_ID) clm(u)  /// 
          legend(off) label(xc(_CX) yc(_CY) label(COUNTYFP))
      
      spmatrix clear
      spmatrix create contiguity W , normalize(none)
      spmatrix summarize W
      //place contiguity matrix into Mata
      spmatrix matafromsp W2 id = W
      getmata r* = W2 ,double
      use ct_data,clear
      //gentrified vector into Mata
      putmata g = gentrified
      mata:
          W2
          g
          a = W2:*g
          b = makesymmetric(lowertriangle(a) + uppertriangle(a)')
          b
      end
      spmatrix spfrommata newW = b id,  normalize(none) replace
      spmatrix  summarize W 
      spmatrix  summarize  newW
      
      //Examine spatial weight matrix
      spmatrix export newW using wmatrix.txt, replace
      
      import delimited "wmatrix.txt", delimiter(space) clear 
      rename v1 _ID
      merge 1:1 _ID using ct_data, keepusing(STATEFP COUNTYFP)
      //Now only non-gentrified counties adjacent to gentrified in matrix
      list

      Comment


      • #4
        Thank you very much Scott! Your code work fine and has been of great support to me.

        Comment

        Working...
        X