Announcement

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

  • Spatial Panel Data and Spatial Weight Matrix

    Hi all,

    I use Stata 17 to create a spatial panel dataset of more than 3000 US counties and 5 time periods. So far following Stata 17 documentation, I have converted shapefile into .dta file. Please see the following code. I am unable to create a spatial weight matrix that is compatible with panel data.

    Code:
    *Spatial files
    cd "J:\Data_Irrigation\Spatial"
    
    *Creating Stata-format shapefile
    *spshape2dta "J:\Data_Irrigation\Spatial\ContiguousUS",  saving(Counties) replace  //Don't use this if the files are created. Takes a lot of time to recreate.
    
    *Using dta shapefiles
    use "J:\Data_Irrigation\Spatial\Counties.dta", clear
    spset
    destring GEOID, gen(geoid)
    
    drop fips
    *Create a standard ID variable
    gen long fips = real(STATEFP + COUNTYFP)
    
    *Assert ID variable
    bysort fips: assert _N==1
    assert fips != .
    
    *Setting a standard ID variable
    spset fips, modify replace
    
    *Set coodinate units
    spset, modify coordsys(latlong, miles)
    
    save, replace
    
    *Preparing data for merging with Stata-format file
    *Loading panel data
    use "J:\Data_Irrigation\Irrigation_LL.dta", clear
    
    
    *Creating a time variable
    gen time = 1 if year==1995
    replace time = 2 if year==2000
    replace time = 3 if year==2005
    replace time = 4 if year==2010
    replace time = 5 if year==2015
    
    xtset id time
    
    *To make a balanced data
    drop if state == "#N/A"
    drop if lat =="#N/A"
    drop if geoid >70000
    drop if panelid==""
    
    *filling missing  panel observations
    tsset id time
    tsfill, full
    
    *Verfiying that id and time are jointly an ID variables
    assert id!=.
    assert time!=.
    bysort id time: assert _N==1
    
    *Balancing and spset the data
    spbalance
    spset id
    
    save  , replace
    
    spmatrix create contiguity M, norm(row)
    I get an error message

    Sp data not linked to shapefile
    Type spset, modify filename(<filename>_shp.dta) to link a _shp.dta file to the dataset in memory.
    r(601);

    end of do-file
    r(601);
    Code:
    . spset, modify shpfile(Counties_shp.dta)
    variable _ID contains invalid values
    _ID contains 3074 values that do not appear in Counties_shp.dta. If you want to drop not-on-the-map observations, use spset with option drop.

    I cannot drop counties from Counties_shp.dta because then only a few counties will be left.

    I am doing something wrong related to choosing the right .dta file generated using -spshape2dta- .

    Can somone please help correct this?


    Thanks,

    Ritika

  • #2
    Hi,

    I wrote my code differently and I have reached a stage of regression after creating a spatial weight matrix, yet I am unable to run the regression. I would appreciate if someone could reply to my post this time. If I am not complying to the FAQs, let me know, I would love to correct it.

    Code:
    *Spatial files
    cd "."
    
    *Creating Stata-format shapefile
    spshape2dta "County_Bal",  saving(Cnty) replace 
    
    *Using dta shapefiles
    use "Cnty.dta", clear
    spset
    destring GEOID, gen(geoid)
    
    *Create a standard ID variable
    gen long fips = real(STATEFP + COUNTYFP)
    
    spmatrix clear
    use "Cnty.dta", clear
    spset
    spmatrix create contiguity W, normalize(row)
    spmatrix matafromsp W id = W
    mata:
    W
    end
    spmatrix save W using W.stswm, replace
    spmatrix save W using W.spmat, replace
    spmatrix export W using W.csv, replace
    
    
    use "W.dta", clear
    
    spmat dta W w*, norm(row) replace
    
    spmat summarize W, links
    
    use "Irrigation.dta"
    
    xtset panelid time
    xtdescribe
    
    xsmle irwgwfr dr2-dr4, croplandacres farmincome farmvalue avgpcp, wmat(W) model(sdm) fe type(ind) nsim(500) nolog
    . xsmle irwgwfr dr2-dr4, croplandacres farmincome farmvalue avgpcp, wmat(W) model(
    > sdm) fe type(ind) nsim(500) nolog
    invalid 'wmat'
    r(198);
    I define wmat in previous lines. I do not know how to fix this error. I read other posts on invalid 'variable'. They talked about invalid syntax. Am I missing something here?

    Can someone please reply to my query here?

    Thanks.

    Ritika

    Comment


    • #3
      hello
      see this link for good exapmle about the spatial panel data model. maybe useful for you
      https://deepnote.com/@carlos-mendez/...7-944a5976de21

      Comment

      Working...
      X