可能有更好的方法,但由于我是R的新手并且已经设置了IDW代码,我一直试图通过调整IDW代码来获得2000米内所有点的中位数,设置加权功率(idp)接近于零,所以更接近的点的加权与远的加权相同。
当我用maxdist = 2000运行下面的代码时,我猜它是NA,因为有些点在2000米内没有任何邻居。即使我将nmin设置为零,我可以使用的最小maxdist是~40,000。
有没有办法告诉它忽略2000米范围内没有邻居的点,或者有人知道更好的方法吗?
这是我的代码:
library(gstat)
clean3145 = read.csv("clean3145.csv")
#Set up the k-fold validation
set.seed(88)
groups <- sample(1:5, nrow(clean3145), replace=TRUE)
#res=result=R=Pearson's correlation between predicted and actual arsenic concentration
MEDres<- rep(NA, 5)
r <- list()
for (k in 1:5) {
print(k)
flush.console()
train <- clean3145[groups!=k, ]
test <- clean3145[groups==k, ]
med <- gstat(formula = As1~1, locations = ~UTMNM+UTMEM, data=train, nmin=0, maxdist=40000, set=list(idp = .01))
medpred <- predict(med, test)$var1.pred
MEDres[k] <- cor(test$As1, medpred)
}
#Show the mean correlation for the 5 different training-test dataset pairs in K-fold validation
mean(MEDres)
感谢您的帮助!
答案 0 :(得分:0)
我无法看到您的代码如何帮助回答原始问题,但对于本地中位数,我会尝试
library(sp)
demo(meuse, ask = FALSE)
library(gstat)
x = krige(zinc~1, meuse, meuse.grid, maxdist = 1000, set = list(method = "med"))
如果一个社区不包含数据,你可以用最近的点数nmax
来定义,在这种情况下,距离不再受控制。
答案 1 :(得分:0)
谢谢Edzer!
我将保存以备将来参考。我们用这种方式工作,也有深度标准(我试图估算地下水中的砷):
#Load required packages and data
library(raster)
depth = read.csv("depth.csv")
set.seed(88)
groups <- sample(1:5, nrow(depth), replace=TRUE)
computeMed <- function(trn, tst) {
pd <- pointDistance(trn[ , c('UTMEM', 'UTMNM')], tst[ , c('UTMEM','UTMNM')], lonlat=FALSE)
pd[pd > 148] <- NA
as <- trn$As1
as <- matrix(rep(as, ncol(pd)), ncol=ncol(pd))
aspd <- as * (pd >= 0)
apply(aspd, 2, median, na.rm=TRUE)
}
r <- rd <- list()
Fallon <- FALSE
for (k in 1:5) {
print(k)
flush.console()
depth$deep <- TRUE
depth$deep[depth$Depth_m < 40] <- FALSE
if (Fallon) {
d <- depth[depth$Tcan2car==1]
} else {
d <- depth
}
train <- d[groups!=k, ]
test <- d[groups==k, ]
p <- computeMed(train,test)
r[[k]] <- cbind(k=k, prd=p, obs=test$As1)
pdeep <- computeMed(train[train$deep,],test[test$deep,])
pshallow <- computeMed(train[!train$deep,],test[!test$deep,])
rd[[k]] <- cbind(k=k, prd=c(pdeep, pshallow), obs=test$As1[c(which(test$deep), which(!test$deep))])
}
cr <- sapply(r, function(x) {x <- na.omit(x); cor(x[,2:3])[2]})
cr
mean(cr)
crd <- sapply(rd, function(x) {x <- na.omit(x); cor(x[,2:3])[2]})
crd
mean(crd)