Announcement

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

  • Creating a spatially lagged variable using a spatial weighting matrix

    I am a Stata 16 user and am trying to create a spatially lagged variable using a spatial weighting matrix within a panel data framework. I have created the spatial weighting matrix using the following command:

    . spmatrix create contiguity W.

    Can you advise me how to convert this matrix into a spatially lagged variable? What would be the next codes to achieve this?

  • #2
    If you have panel data, the weighting matrix should be created using a single cross-section. This is discussed in the documentation. Not sure how you were able to create it the way you describe without getting an error.

    spmatrix create contiguity W if year==XXXX
    Regarding your question, see

    Code:
    help spgenerate

    Comment


    • #3
      Hi Andrew,

      Thank you for the help. I have followed your directions but still got the same error message. Please see below.
      Code:
      . spset
      Sp dataset final.dta
      data: panel
      spatial-unit id: _ID (equal to ct)
      time id: date (see xtset)
      coordinates: _CX, _CY (planar)
      linked shapefile: 2020_shp.dta. spmatrix create contiguity W if date == tm(2023m12), normalize(row)
      . spgenerate Wy = W*y
      _IDs in weighting matrix W do not match _IDs in estimation sample
      There are places in W not in estimation sample and places in estimation sample not in W.
      r(459);
      Any advice would be greatly appreciated.
      Last edited by DY Kim; 13 Apr 2025, 07:58.

      Comment


      • #4
        See #9: https://www.statalist.org/forums/for...ing-spgenerate

        Comment


        • #5
          Hi Andrew, thank you for the help.

          After generating the spatial weighting matrix for December 2023, I attempted to populate the spatial lag variable (Wy) for all months from 2017 to 2023 using the same value for each spatial unit. I followed the code you suggested in the other post. However, instead of consistent values across months for each unit, I observed unexpected variation. I would appreciate it if you could check whether my code below is correct.

          Code:
          spmatrix create contiguity W if date == tm(2023m12)
          spgenerate Wy = W*y if date == tm(2023m12)
          levelsof date, local(dates)
          foreach date in `dates'{
              spgenerate XYZ`date' = W*y if date==`date'
          }
          egen W_y= rowmax(XYZ*)
          drop XYZ*
          Last edited by DY Kim; 13 Apr 2025, 20:49.

          Comment


          • #6
            Why is the variation unexpected?

            spgenerate Wy = W*y
            Unless the value of \(y\) is constant across months, the multiplication above would yield different values for different months.

            Comment

            Working...
            X