简体   繁体   中英

idw crashes R for me

I have a task to produce an inverse distance weighted estimate for some spatial data. I find that using idw when there is only one known point and a maximum distance specified, crashes R (versions 3.3.2 and 3.4.2).

library(sp)
library(gstat)

# THIS IS OK MORE THAN ONE KNOWN POINT
X<-c(15,4,20,0)
Y<-c(10,10,10,10)
P<-c(1,2,3,4)
known.df<-data.frame(X,Y,P)         
coordinates(known.df)<- ~ X + Y

X<-c(10)
Y<-c(10)
unknown.df<-data.frame(X,Y)
coordinates(unknown.df)<- ~ X + Y

idwmodel = idw(P~1,known.df,unknown.df,maxdist=1,idp=2,debug.level=0)
idwmodel@data$var1.pred
idwmodel = idw(P~1,known.df,unknown.df,maxdist=5,idp=2,debug.level=0)
idwmodel@data$var1.pred
idwmodel = idw(P~1,known.df,unknown.df,maxdist=6,idp=2,debug.level=0)
idwmodel@data$var1.pred
idwmodel = idw(P~1,known.df,unknown.df,maxdist=10,idp=2,debug.level=0)
idwmodel@data$var1.pred

# THIS IS ALSO OK SINCE maxdist IS Inf
X<-c(15)
Y<-c(10)
P<-c(1)
known.df<-data.frame(X,Y,P)         
coordinates(known.df)<- ~ X + Y

X<-c(10)
Y<-c(10)
unknown.df<-data.frame(X,Y)
coordinates(unknown.df)<- ~ X + Y

idwmodel = idw(P~1,known.df,unknown.df,maxdist=Inf,idp=2,debug.level=0)
idwmodel@data$var1.pred

# THIS CRASHES R
X<-c(15)
Y<-c(10)
P<-c(1)
known.df<-data.frame(X,Y,P)         
coordinates(known.df)<- ~ X + Y

X<-c(10)
Y<-c(10)
unknown.df<-data.frame(X,Y)
coordinates(unknown.df)<- ~ X + Y

idwmodel = idw(P~1,known.df,unknown.df,maxdist=10,idp=2,debug.level=0)
idwmodel@data$var1.pred

To my mind, an estimate based on one observation is fine, and in anycase a crash out of R is not ideal. You may say that if there is only one known point then why bother with idw? Well I only know there is one known point after idw has used the maxdist value to filter.

Does anyone know/advise an efficient work around? At the moment I am applying the maxdist filter to my data before calling idw and setting maxdist to Inf. But this means calculating point distances twice, once by myself and then once in idw.

On my linux box with R version 3.4.3 (2017-11-30) I just get the error below and no crash when running your code.

Error in predict.gstat(g, newdata = newdata, block = block, nsim = nsim,  : 
value not allowed for:  bbox with zero size: remove neighbourhood settings?

In your case I would recommend filing an issue on GitHub (remember to include your platform): https://github.com/edzer/gstat/issues

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM