[R-sig-Geo] spdep lagsarlm regression error

Erik Alda erik.alda at gmail.com
Tue Sep 6 20:23:17 CEST 2016


Dear All,

I am trying to estimate a spatial lag model with the spdep package in R. I
have been reading previous posts and I have managed to resolve most of the
issues I was running into except when I try estimating the impacts of the
spatial lag model using impacts.  It produces the following error.
Error: length(listw$neighbours) == n is not TRUE


I enclose the code I have followed:

> #polgal <-
read.gal("/Users/erik/Dropbox/D_DRIVE_FOLDER/results3_25_16/data/mun.gal",
override.id=TRUE)
> attach(mex)
The following object is masked from package:maps:

    unemp

> mex.poly <- readShapePoly('/Volumes/Transcend/Municipios/Municipios.shp')
> polgal <- read.gal("/Volumes/Transcend/Municipios/Municipios.gal",
override.id=TRUE)
> attributes(polgal)
[output omitted]

> print(polgal)
Neighbour list object:
Number of regions: 2456
Number of nonzero links: 14176
Percentage nonzero weights: 0.2350158
Average number of links: 5.771987
>
> ba0 <- read.table("/Volumes/Transcend/Municipios/Municipiosa.gwt",
header=FALSE, skip=1)
> str(ba0)
'data.frame': 12280 obs. of  3 variables:
 $ V1: int  1 1 1 1 1 2 2 2 2 2 ...
 $ V2: int  231 2 7 3 12 1 12 16 14 15 ...
 $ V3: num  15862 13205 15802 10167 15332 ...
> ID_ <- unique(ba0$V1)
> ba <- read.gwt2nb("/Volumes/Transcend/Municipios/Municipiosa.gwt",
region.id=ID_)
Warning message:
In read.gwt2nb("/Volumes/Transcend/Municipios/Municipiosa.gwt",  :
  356, 642, 2286, 2334, 2448 are not destinations
> ba
Neighbour list object:
Number of regions: 2456
Number of nonzero links: 12280
Percentage nonzero weights: 0.2035831
Average number of links: 5
Non-symmetric neighbours list
>
> #polgwt <-
read.gwt2nb("/Users/erik/Dropbox/D_DRIVE_FOLDER/results3_25_16/data/geoda/geoda.gwt",
region.id=ID_)
> #summary(polgwt)
> #plot(mex.poly)
>

> ###redefine variables
> y<-eff_m170
> x1<-grado_escolar
> x2<-poverty_perc
> x3<-gini
> x4<-asinh_orgcrime
> x5<-youthperc
> x6<-asinh_popdensity
> x7<-unemp2
> x8<-left
> x9<-coalition
> mex.ols<-lm(eff_m170 ~ x1+x2+x3+x4+x5+x6+x7+x8+x9, data=mex)
> summary(mex.ols)

Call:
lm(formula = eff_m170 ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 +
    x9, data = mex)
[regression output omitted]
> W<-nb2listw(ba, style="W")
> plot(W,coordinates(mex.poly))
> coords<-coordinates(mex.poly)
> W_dist<-knn2nb(knearneigh(coords, 5))
> moran.lm<-lm.morantest(mex.ols, W, alternative="two.sided",
 naSubset=TRUE)
> print(moran.lm)


> sar.mex<-lagsarlm(eff_m170~x1+x2+x3+x4+x5+x6+x7+x8+x9,data=mex,
W,na.action=na.omit)
> summary(sar.mex)

Call:lagsarlm(formula = eff_m170 ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 +
x8 + x9, data = mex, listw = W, na.action = na.omit)

Residuals:
      Min        1Q    Median        3Q       Max
-59.28338 -16.61112   0.73875  19.39619  52.48456

Type: lag
Regions with no neighbours included:
 51 55 68 70 98 160 193 280 287 307 321 350 370 497 590 643 735 746 757 779
785 803 810 858 878 905 1161 1527 1556 1598 1617 1636 1654 1668 1671 1687
1693 1699 1726 1730 1739 1749 1754 1822 1851 1881 1938 1948 1953 1979 1995
2032 2047 2058 2069 2071 2080 2090 2100 2125 2135 2151 2155 2157 2184 2198
2322 2387 2393 2400 2436 2445
[regression output omitted]

> impacts(sar.mex listw=W)
Error: unexpected symbol in "impacts(sar.mex listw"
> impacts(sar.mex, listw=W)
*Error: length(listw$neighbours) == n is not TRUE*






I know I am doing something wrong and I have not managed to fix that last
error. Any advice will be greatly appreciated

Thanks

-- 

Erik Alda
Doctoral Candidate
Department of Justice, Law and Criminology
School of Public Affairs
The American University, Washington, D.C.
Alt.e-mail: ea3466a at american.edu

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list