I have one problem with the spatial regression code shown below (Code Example). After I run the regression I get the following error
Error in listw %*% as.matrix(ywithin) :
Cholmod error 'X and/or Y have wrong dimensions' at file ../MatrixOps/cholmod_sdmult.c, line 90
When I remove the spatial dimension from the regression, the regression runs perfectly, so I guess the error might be in the spatial weight matrix. Could someone please help me address why this error occurs and recommend a solution?
Reference Data:
Link to the folder with the shapefile and the data: https://drive.google.com/drive/folders/1PFAhCpYnDCtV36DuTLh4V79Bs3RO5ESy?usp=sharing
Code Example (Downloads files and Demonstrates Error)
requiredPackages <-
c("rio", "plm", "splm", "tmaptools", "spdep", "fields", "readxl")
ipak <- function(pkg) {
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new.pkg))
install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}
ipak(requiredPackages)
# URL's obtained from Google Shared Directory
pShareDataURL = "https://drive.google.com/open?id=1PjDlLiA99-3xuGPhPLltRg8uod6zPAKn" ## data1.xlsx
sShareDataURL = "https://drive.google.com/open?id=1VJGL8aSJomvWCnw9FPEWTJsQ65StYdzW" ## nuts2ashp
## strip "open?" and replace with us?export=download&
pdataURL <- gsub("open\\?", "uc\\?export=download\\&", pShareDataURL )
sdataURL <- gsub("open\\?", "uc\\?export=download\\&", sShareDataURL )
pdataDest = file.path("./data/data1.xlsx" )
sdataDest = file.path("./data/nuts2a.shp" )
if (!dir.exists("./data")) {
dir.create(file.path("./data"), showWarnings = FALSE)
}
# Download files
download.file(pdataURL, destfile = pdataDest, method = "wget", mode = "wb")
download.file(sdataURL, destfile = sdataDest, method = "wget", mode = "wb")
pdata <- read_excel(pdataDest)
shape_nuts <- read_shape(sdataDest)
shape_nuts <- shape_nuts[order(shape_nuts@data$NUTS_ID), ]
shape_nuts2 <-
shape_nuts[substr(shape_nuts@data$NUTS_ID, 1, 3) != "FR9" &
shape_nuts@data$NUTS_ID != "UKI1"
&
shape_nuts@data$NUTS_ID != "EL21" &
shape_nuts@data$NUTS_ID != "RO21", ]
#######computing the spatial weight matrix###########
coords2 <- coordinates(shape_nuts2) # getting coordinates of the polygon centroids
dm <- rdist.earth(coords2, miles = FALSE) # calculating distance between the polygon centroids
rownames(dm) <- shape_nuts2@data$NUTS_ID # naming the rows
colnames(dm) <- shape_nuts2@data$NUTS_ID # naming the columns
for (i in 1:dim(dm)[1]) {
dm[i, i] = 0
} # renders exactly zero all diagonal elements
dm1 <- ifelse(dm != 0, 1 / dm, dm) #inverting distance
# create a (normalized) listw object
dm1.lw <- mat2listw(dm1,
style = "W",
row.names = shape_nuts2@data$NUTS_ID)
#########regressions#######
spgr01 <- spgm(
rgrowthpc ~ lrgdp0pc + lefpayr,
data = pdata,
listw = dm1.lw,
model = "within",
lag = TRUE,
spatial.error = TRUE,
endog = ~ lefpayr,
instruments = ~ area_prop,
method = "w2sls"
)
Console Output:
> #########regressions#######
>
> spgr01 <- spgm(
+ rgrowthpc ~ lrgdp0pc + lefpayr,
+ data = pdata,
+ listw = dm1.lw,
+ model = "within",
+ lag = TRUE,
+ spatial.error = TRUE,
+ endog = ~ lefpayr,
+ instruments = ~ area_prop,
+ method = "w2sls"
+ )
Error in listw %*% as.matrix(ywithin) :
Cholmod error 'X and/or Y have wrong dimensions' at file ../MatrixOps/cholmod_sdmult.c, line 90
>