[R-sig-Geo] problem in moran tes

Roger Bivand Roger@B|v@nd @end|ng |rom nhh@no
Fri Oct 8 16:31:24 CEST 2021


On Fri, 8 Oct 2021, maria jesus herrerias wrote:

> Dear list,
>
> I have a panel data and I would like to test if the spatial dependence is
> an issue. However, I am getting this error:
> #ERROR
> Error in nb2listw(sub.queen.nb$neighbours, style = "W", zero.policy = TRUE)
> :
>  Not a neighbours list

Most likely your data. Either provide a link to your data, or provide the 
example using built-in data (say country boundaries from the rnaturalearth 
package). Without a reproducible example, it isn't possible to guess, 
unless you meant by sub.queen.nb$neighbours, sub.queen.listw$neighbours, 
as "nb" objects do not have "neighbours" components.

Roger

>
> Below I report my code to see if somebody can help me on this matter:
>
> #Spatial Dependence for Panel 32 regions from 1990-2014
>
> # The Spatial Matrix
> # Read the shapefile and set up the working directory.
> library(foreign)
> library(haven)
>
>
> setwd("C:/Users/Usuario/Desktop/instituciones/revision energy
> economics/docs enviados/spatial test")
>
> x <- read.dta("C:/Users/Usuario/Desktop/instituciones/revision energy
> economics/docs enviados/spatial test/high_spatial.dta")
>
> # Matching with stata file and shapefile by ID
>
> dta_cntries <- unique(x$OBJECTID)
>
> library(sf)
>
> wrld <- st_read("C:/Users/Usuario/Desktop/instituciones/revision energy
> economics/spatial panel in
> stata/Longitude_Graticules_and_World_Countries_Boundaries-shp/countries1.shp")
> wrld_a <- aggregate(wrld, list(wrld$OBJECTID), head, n=1)
>
>
> o <- match(dta_cntries, wrld_a$OBJECTID)
>
> #Check if matching is right
>
> The Stata file has two non-matching names (left ccode stata, right names in
> map shapefile):
>
> which(is.na(o))
>
> o <- match(dta_cntries, wrld$OBJECTID)
> o
>
> library(spdep)
> row.names(wrld_a) <- wrld_a$OBJECTID
> nb <- poly2nb(wrld_a)
> nb1 <- subset(nb, 1:nrow(wrld_a) %in% o)
>
> #high
> Neighbour list object:
> Number of regions: 32
> Number of nonzero links: 82
> Percentage nonzero weights: 8.007812
> Average number of links: 2.5625
>
> wrld_s <- wrld_a[1:nrow(wrld_a) %in% o,]
> nb2 <- poly2nb(wrld_s)
> all.equal(nb1, nb2, check.attributes=FALSE)
> [1] TRUE  # subsetting well-done
>
> ***********************************
> # Queen Matrix
> sub.worlddata <-wrld_s
> sub.queen.nb <- poly2nb(sub.worlddata, queen=TRUE)
> length(sub.queen.nb)  #32
> summary(sub.queen.nb)
> sub.queen.listw <-nb2listw(sub.queen.nb,zero.policy = TRUE)
> summary(sub.queen.listw,zero.policy = TRUE)
> head(sub.queen.nb)
> *********************************
>
>
> # Prepare the dataset for panel data analysis in R.
>
> library(plm)
>
> setwd("C:/Users/Usuario/Desktop/instituciones/revision energy
> economics/code and data/quantile")
>
> high <- read.dta("C:/Users/Usuario/Desktop/instituciones/revision energy
> economics/docs enviados/spatial test/high_spatial.dta")
>
>
> mydata <- pdata.frame(high, index = c("OBJECTID", "year"))
> **************************************
> #Variables.
>
> energy <- mydata$leic
> gdp <- mydata$lgdppcnewc
> gdp2 <- mydata$lgdppcnewc2
> fdi <- mydata$fdigc
> imports <- mydata$importsgc
> industry <- mydata$industrygc
> inst.1 <- mydata$kun_legabsc
>
> **********************************
> #Define formula
>
> model.1 <- energy ~ gdp + gdp2 + fdi + imports + industry + inst.1
>
> ********************
> # Testing spatial autocorrelation
> library(spatialreg)
>
> ols.eq1 <- lm(model.1, data = mydata)
> summary(ols.eq1)
> **********************************
>
> lmMoranTest <- lm.morantest(ols.eq1,nb2listw(sub.queen.nb$neighbours,
> style="W",zero.policy = TRUE))
> lmMoranTest
>
> #ERROR
> Error in nb2listw(sub.queen.nb$neighbours, style = "W", zero.policy = TRUE)
> :
>  Not a neighbours list
>
> library(spdep)
>
> lmLMtests <- lm.LMtests(ols.eq1,nb2listw(sub.queen.nb), test=c("LMerr",
> "LMlag", "RLMerr", "RLMlag", "SARMA"))
> lmLMtests
>
> #ERROR
> Error in nb2listw(sub.queen.nb$neighbours, style = "W", zero.policy = TRUE)
> :
>  Not a neighbours list
>
> Many thanks
>
> Best wishes,
> Maria Jesus
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Emeritus Professor
Department of Economics, Norwegian School of Economics,
Postboks 3490 Ytre Sandviken, 5045 Bergen, Norway.
e-mail: Roger.Bivand using nhh.no
https://orcid.org/0000-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en



More information about the R-sig-Geo mailing list