[R-sig-Geo] moran test - Empty neighbour sets found
maria jesus herrerias
mjherrer|@@t @end|ng |rom gm@||@com
Mon Oct 11 20:31:34 CEST 2021
Dear Roger,
I try first with your suggestion for the map:
library(rnaturalearth)
spdf_world <- ne_countries(returnclass="sf")
if (require(sp)) {
plot(spdf_world)
}
names(spdf_world)
[1] "scalerank" "featurecla" "labelrank" "sovereignt" "sov_a3"
[6] "adm0_dif" "level" "type" "admin" "adm0_a3"
[11] "geou_dif" "geounit" "gu_a3" "su_dif" "subunit"
[16] "su_a3" "brk_diff" "name" "name_long" "brk_a3"
[21] "brk_name" "brk_group" "abbrev" "postal" "formal_en"
[26] "formal_fr" "note_adm0" "note_brk" "name_sort" "name_alt"
[31] "mapcolor7" "mapcolor8" "mapcolor9" "mapcolor13" "pop_est"
[36] "gdp_md_est" "pop_year" "lastcensus" "gdp_year" "economy"
[41] "income_grp" "wikipedia" "fips_10" "iso_a2" "iso_a3"
[46] "iso_n3" "un_a3" "wb_a2" "wb_a3" "woe_id"
[51] "adm0_a3_is" "adm0_a3_us" "adm0_a3_un" "adm0_a3_wb" "continent"
[56] "region_un" "subregion" "region_wb" "name_len" "long_len"
[61] "abbrev_len" "tiny" "homepart" "geometry"
spdf_world$geounit
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")
dta_cntries <- unique(x$geounit)
library(sf)
wrld_a <- aggregate(spdf_world, list(spdf_world$geounit), head, n=1)
o <- match(dta_cntries, wrld_a$geounit)
#Check if matching is right
which(is.na(o))
[1] 9
dta_cntries[9] <- "France"
o <- match(dta_cntries, spdf_world$geounit)
o
which(is.na(o))
library(spdep)
row.names(wrld_a) <- wrld_a$geounit
nb <- poly2nb(wrld_a)
nb1 <- subset(nb, 1:nrow(wrld_a) %in% o)
Neighbour list object:
Number of regions: 32
Number of nonzero links: 24
Percentage nonzero weights: 2.34375
Average number of links: 0.75
17 regions with no links:
Angola Dominican Republic East Timor Egypt Falkland Islands Fiji Gambia
Guyana Liberia Malaysia Oman Papua New Guinea Puerto Rico Republic of
Serbia Slovenia Sweden Uruguay
Note for roger: this is wrong as I didn’t include these countries in the
data (17 regions with no links)
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 however.
---------------------------------------------------
I try now with my map:
#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/docs
enviados/Longitude_Graticules_and_World_Countries_Boundaries-shp/mapa1.shp")
wrld_a <- aggregate(wrld, list(wrld$OBJECTID), head, n=1)
o <- match(dta_cntries, wrld_a$OBJECTID)
#Check if matching is right
which(is.na(o))
o <- match(dta_cntries, wrld$OBJECTID)
o
library(spdep)
row.names(wrld_a) <- wrld_a$OBJECTID
nb <- poly2nb(wrld_a, queen=TRUE)
nb1 <- subset(nb, 1:nrow(wrld_a) %in% o)
nb1llistw <- nb2listw(nb1,style = "W", zero.policy = TRUE)
#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,queen=TRUE)
all.equal(nb1, nb2, check.attributes=FALSE)
[1] TRUE # subsetting well-done
subworlddata <- wrld_s
# Tranform the nb object into a matrix row standarised.
sub.queen.w.Mat <- listw2mat(sub.queen.listw) # creates a standardized
matrix (rows sum to 1)
sub.queen.B.Mat <- nb2mat(sub.queen.nb, style='B',zero.policy=TRUE) # a
simple binary matrix (rows sum to the number of neighbors)
rownames(sub.queen.w.Mat) <- subworlddata$OBJECTID
colnames(sub.queen.w.Mat) <- subworlddata$OBJECTID
rownames(sub.queen.B.Mat) <- subworlddata$OBJECTID
colnames(sub.queen.B.Mat) <- subworlddata$OBJECTID
----------------------------------------------------------------------------------------------------------
sub.queen.w.Mat1 <-
read.dta("C:/Users/Usuario/Desktop/instituciones/revision energy
economics/docs enviados/subqueenW.dta")
sub.queen.B.Mat1 <-
read.dta("C:/Users/Usuario/Desktop/instituciones/revision energy
economics/docs enviados/subqueenB.dta")
sub.queen.w.Mat11 <- as.matrix(sub.queen.w.Mat1)
rownames(sub.queen.w.Mat11) <- subworlddata$OBJECTID
colnames(sub.queen.w.Mat11) <- subworlddata$OBJECTID
sub.queen.lw.Mat11 <- mat2listw(sub.queen.w.Mat11)
sub.queen.B.Mat11 <- as.matrix(sub.queen.B.Mat1)
rownames(sub.queen.B.Mat11) <- subworlddata$OBJECTID
colnames(sub.queen.B.Mat11) <- subworlddata$OBJECTID
sub.queen.lB.Mat11 <- mat2listw(sub.queen.B.Mat11)
listw1 <- sub.queen.lw.Mat11
listw2 <- sub.queen.lB.Mat11
sub.queen.lw.listw.Mat21 <- nb2listw(sub.queen.lw.Mat11$neighbours,
style="W",zero.policy=TRUE)
sub.queen.lB.listw.Mat21 <- nb2listw(sub.queen.lB.Mat11$neighbours,
style="B",zero.policy=TRUE)
# Summary Matrix nb object to listw
listw21 <- sub.queen.lw.listw.Mat21
listw22 <- sub.queen.lB.listw.Mat21
---------------------------------------------------
library(plm)
setwd("C:/Users/Usuario/Desktop/instituciones/revision energy
economics/code and data/quantile")
x <- read.dta("C:/Users/Usuario/Desktop/instituciones/revision energy
economics/docs enviados/spatial test/high_spatial.dta")
mydata <- pdata.frame(x, index = c("OBJECTID", "year"))
**************************************
#Variables.
energy <- mydata$leic
gdp <- mydata$lgdppcnewc
gdp2 <- mydata$lgdppcnewc2
fdi <- mydata$fdigc
imports <- mydata$importsgc
industry <- mydata$industrygc
inst <- mydata$kun_legabsc
ID <- mydata$OBJECTID
time <- mydata$year
**********************************
#Define formula
model.1 <- energy ~ gdp + gdp2 + fdi + imports + industry + inst
ols.eq1 <- lm(model.1, data = mydata)
summary(ols.eq1)
lmMoranTest <- lm.morantest(ols.eq1,listw = nb2listw(nb1,style = "W",
zero.policy = TRUE))
lmMoranTest
*Error in nb2listw(neighbours = subnb, glist = NULL, style = style,
zero.policy = zero.policy) : *
* Empty neighbour sets found*
lmLMtests <- lm.LMtests(ols.eq1, listw = nb2listw(nb1,style = "W",
zero.policy = TRUE), test=c("LMerr", "LMlag", "RLMerr", "RLMlag", "SARMA"))
lmLMtests
*Error in nb2listw(neighbours = subnb, glist = NULL, style = style,
zero.policy = zero.policy) : *
* Empty neighbour sets found*
many thanks
maria jesus
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list