execution time spatial models in r

208 Views Asked by At

I have a problem regarding to the execution time of a SAR model with the 'spdep' package.

I pass two different dataset of the same dimension to the same function, it takes very different times (a few seconds vs hours).

I write to you my code, if you have any idea please let me know.

Thanks Chiara

library(spdep)
data(house, package="spData")



hlw<-nb2listw(LO_nb)
system.time( lagsarlm(log(price) ~ age, data=house, listw=hlw, type="lag", method="Matrix", trs=trMat))

#----------------------------------------------
library(spatstat)
d1<-100
d2<-100
n<-25357 

coord<- runifpoint(n,win=owin(c(0,d1),c(0,d2)))
mat<-cbind(coord$x,coord$y)

X<-rnorm(n,5,2)
Y<-rnorm(n,5,3)
d<-as.data.frame(cbind(X,Y))
cutoff<-  dnearneigh(mat,0,4)
t<-nb2listw(cutoff)
system.time( lagsarlm(Y ~ X, data=d, listw=t, type="lag", method="Matrix"))
1

There are 1 best solutions below

0
On

I got:

library(spdep)
data(house, package = "spData")
dim(house)
hlw <- nb2listw(LO_nb)
# Number of nonzero links: 74874 
system.time(r1 <- lagsarlm(log(price) ~ age, data=house, listw=hlw,
                           type="lag", method="Matrix", trs=trMat))
# user  system elapsed 
# 0.50    0.05    0.55 

#----------------------------------------------
library(spatstat)
set.seed(21)
d1 <- 100
d2 <- 100
coord <- runifpoint(n, win = owin(c(0, d1), c(0, d2)))
mat <- cbind(coord$x, coord$y)
cutoff <- dnearneigh(mat, 0, 4)
t <- nb2listw(cutoff)
t
# Number of nonzero links: 3119764 
n <- 25357
X <- rnorm(n,5,2)
Y <- rnorm(n,5,3)
d <- as.data.frame(cbind(X,Y))

system.time(r2 <- lagsarlm(Y ~ X, listw = t, type = "lag", method = "Matrix"))
#    user  system elapsed 
# 156.47    8.54  168.25

# v2
d1 <- 200
d2 <- 200
coord <- runifpoint(n, win = owin(c(0, d1), c(0, d2)))
mat <- cbind(coord$x, coord$y)
cutoff <- dnearneigh(mat, 0, 4)
t <- nb2listw(cutoff)
t
# Number of nonzero links: 795054 

system.time(r2 <- lagsarlm(Y ~ X, listw = t, type = "lag", method = "Matrix"))
# user  system elapsed 
# 13.42    2.02   15.61 

It seems that time of lagsarlm is dependent of t`s Number of nonzero links..