我的方差图代码结果与variog()结果不同

时间:2019-01-30 06:03:52

标签: r geospatial geor covariogram

我正在编写用于生成变异函数的代码。为了验证我的结果,我使用了geoR :: variog()进行了检查,但是两个变量图都不同。

我试图了解variog()的代码以了解幕后情况,但是发生的事情太多了,我似乎无法理解。我在我的代码中使用参数X坐标,Y坐标,数据值,滞后次数,最小滞后值,滞后间隔,方位角(以度为单位的角度; 90对应于垂直方向),角度公差(以度为单位) )和最大带宽。

variogram = function(xcor, ycor, data, nlag, minlag, laginv, azm, atol, maxbandw){
dl <- length(data)
lowangle <- azm - atol
upangle <- azm + atol
gamlag <- integer(nlag)
n <- integer(nlag)

dist <- pairdist(xcor, ycor)
maxd <- max(dist)
llag <- seq(minlag, minlag + (nlag-1) * laginv, by = laginv)
hlag <- llag + laginv

for(i in 1:dl){
    for(j in i:dl){
        if(i != j){

            if(xcor[j]- xcor[i] == 0)
                theta <- 90
            else
                theta <- 180/pi * atan((ycor[j] - ycor[i])/(xcor[j] - xcor[i]))
            for(k in 1:nlag){
                d <- dist[j, i]
                b <- abs(d * sin(theta - azm))
                if((llag[k] <= d & d < hlag[k]) & (lowangle <= theta & theta < upangle) & (b <= maxbandw)){
                    gamlag[k] <- gamlag[k] + (data[i] - data[j])^2;
                    n[k] <- n[k] + 1
                }
            }
        }
    }   
}

gamlag <- ifelse(n == 0, NA, gamlag/(2*n))
tmp <- data.frame("lag" = llag, "gamma" = gamlag)
return(tmp)
}

上述代码的功能调用

ideal_variogram_2 <- variogram(data3[,1], data3[,2], data3[,3], 18, 0, 0.025, 90, 45, 1000000)
ideal_variogram_2 <-  na.omit(ideal_variogram_2)
plot(ideal_variogram_2$lag, ideal_variogram_2$gamma, main = "Using my code")

函数调用variog()

geodata1 <- as.geodata(data3, coords.col = 1:2, data.col = 3)
ideal_variogram_1 <- variog(geodata1, coords = geodata1$coords, data = geodata1$data, option = "bin", uvec = seq(0, 0.45, by = 0.025), direction = pi/2, tolerance = pi/4)
df <- data.frame(u = ideal_variogram_1$u, v = ideal_variogram_1$v)
plot(df$u, df$v, main = "Using variog()")

我获得的2个方差图位于以下链接: Variogram

0 个答案:

没有答案