使用大型栅格查找 table(在 R 中)
Look up table using large raster (in R)
真心希望你能帮帮我。提前感谢您,感谢您到目前为止我从这些页面中学到的一切。抱歉,我的专业知识受到我试图学习 R 中所有内容的兼职性质的限制。
我的目标:
使用(大)栅格查找 table 反演。
我现在拥有的:
#Observed data (in this case just as a dataframe)
obs <- data.frame(runif(100,0,1))
#Two sets of simulated data (often n >10 000)
sim.A <- data.frame(runif(1000,0,1))
sim.B <- data.frame(runif(1000,0,1))
#Calculate the error [cost] for each observed value and every simulated(A) value
error.fun <- function(x){sqrt((x-sim.A)^2)}
error <- apply(obs,1,error.fun)
#Find the position of the min [error] value
min.func <- function(x){which(x == min(x),arr.ind = F)}
cost.min <- apply(error,2,min.func)
#Subset the simulated (B) dataset at the position of the least error[cost.min]
LUT.values = data.frame(sim.B[cost.min,])
我的问题:
1) 上面的代码适用于从栅格中提取的样本。但是,我需要用整个 (ncell > 1Mil) 栅格替换采样观测值。我显然需要优化上述两个函数(合并为一个?),但我得到的最接近的结果让我持怀疑态度,因为与采样数据尝试相比结果很差。
我对大栅格的尝试:
#This runs, but I dont think it's working correctly
crs.UTM <- CRS("+proj=utm +zone=36 +south +datum=WGS84 +units=m +no_defs+ellps=WGS84 +towgs84=0,0,0")
r <- raster( crs=crs.UTM)
extent(r) <- extent(0, 100, 0, 100)
res(r) <- c(1, 1)
values(r) <- runif(ncell(r), 0, 1)
#Simulated data (often n >10 000)
sim.A <- data.frame(runif(1000,0,1))
sim.B <- data.frame(runif(1000,0,1))
cost.min.func <- function(x){
cost <- sqrt((x-sim.A)^2)
c.min <- sim.B[which(cost == min(cost),arr.ind = FALSE),]}
LUT.rst <- calc(r,cost.min.func)
非常感谢
您缺少的是 observed - simulated
:
的平均值
rmse <- sqrt(mean((obs-sim)^2))
我想这就是你想要的
library(raster)
r <- raster(ext=extent(0, 100, 0, 100), res=1, crs="+proj=utm +zone=36 +south +datum=WGS84 +units=m")
set.seed(0)
values(r) <- runif(ncell(r), 0, 1)
sim.A <- runif(1000,0,1)
sim.B <- runif(1000,0,1)
cost <- function(x) {
y <- abs(x-sim.A)
sim.B[which.min(y)]
}
x <- calc(r, cost)
这将需要一段时间才能处理大型数据集。应该可以通过使用 x 的一系列值来首先对此进行近似,然后也许只考虑对可能具有最小值
的几个单元格进行计算
真心希望你能帮帮我。提前感谢您,感谢您到目前为止我从这些页面中学到的一切。抱歉,我的专业知识受到我试图学习 R 中所有内容的兼职性质的限制。
我的目标: 使用(大)栅格查找 table 反演。
我现在拥有的:
#Observed data (in this case just as a dataframe)
obs <- data.frame(runif(100,0,1))
#Two sets of simulated data (often n >10 000)
sim.A <- data.frame(runif(1000,0,1))
sim.B <- data.frame(runif(1000,0,1))
#Calculate the error [cost] for each observed value and every simulated(A) value
error.fun <- function(x){sqrt((x-sim.A)^2)}
error <- apply(obs,1,error.fun)
#Find the position of the min [error] value
min.func <- function(x){which(x == min(x),arr.ind = F)}
cost.min <- apply(error,2,min.func)
#Subset the simulated (B) dataset at the position of the least error[cost.min]
LUT.values = data.frame(sim.B[cost.min,])
我的问题:
1) 上面的代码适用于从栅格中提取的样本。但是,我需要用整个 (ncell > 1Mil) 栅格替换采样观测值。我显然需要优化上述两个函数(合并为一个?),但我得到的最接近的结果让我持怀疑态度,因为与采样数据尝试相比结果很差。
我对大栅格的尝试:
#This runs, but I dont think it's working correctly
crs.UTM <- CRS("+proj=utm +zone=36 +south +datum=WGS84 +units=m +no_defs+ellps=WGS84 +towgs84=0,0,0")
r <- raster( crs=crs.UTM)
extent(r) <- extent(0, 100, 0, 100)
res(r) <- c(1, 1)
values(r) <- runif(ncell(r), 0, 1)
#Simulated data (often n >10 000)
sim.A <- data.frame(runif(1000,0,1))
sim.B <- data.frame(runif(1000,0,1))
cost.min.func <- function(x){
cost <- sqrt((x-sim.A)^2)
c.min <- sim.B[which(cost == min(cost),arr.ind = FALSE),]}
LUT.rst <- calc(r,cost.min.func)
非常感谢
您缺少的是 observed - simulated
:
rmse <- sqrt(mean((obs-sim)^2))
我想这就是你想要的
library(raster)
r <- raster(ext=extent(0, 100, 0, 100), res=1, crs="+proj=utm +zone=36 +south +datum=WGS84 +units=m")
set.seed(0)
values(r) <- runif(ncell(r), 0, 1)
sim.A <- runif(1000,0,1)
sim.B <- runif(1000,0,1)
cost <- function(x) {
y <- abs(x-sim.A)
sim.B[which.min(y)]
}
x <- calc(r, cost)
这将需要一段时间才能处理大型数据集。应该可以通过使用 x 的一系列值来首先对此进行近似,然后也许只考虑对可能具有最小值
的几个单元格进行计算