[R-sig-Geo] problem in moran tes

Raphael Saldanha r|@@|d@nh@ @end|ng |rom gm@||@com
Fri Oct 8 16:24:06 CEST 2021


Hi Maria,

At some point you create an object called “sub.queen.listw”. You can try to use this one with the test function.


lmLMtests <- lm.LMtests(ols.eq1, sub.queen.listw, test=c("LMerr","LMlag", "RLMerr", "RLMlag", "SARMA”))



Kind regards,

Raphael Saldanha


> Em 8 de out. de 2021, à(s) 11:10, maria jesus herrerias <mjherreriast using gmail.com> escreveu:
> 
> 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
> 
> 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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4908 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20211008/e2fb86f0/attachment.p7s>


More information about the R-sig-Geo mailing list