自己的焦点统计函数(focal,"raster" package,R)给出了不正确的输出
Own function for focal statistics (focal, "raster" package, R) gives incorrect output
我是 R 和编程的初学者。所以我觉得问题很简单,但我找不到答案或解决它。
我有栅格(100*100 个像元)。我需要在移动 window 处通过 2D DFT 获得谐波幅度的中值(例如,window = 21 的大小)。
我在 raster 包中找到了 focal 函数。对于这个函数,我可以编写自己的函数,它采用一系列值(window 中的栅格值)和 return 整个 window.
的单个值
r <- raster(matrix(rnorm(10000), nrow = 100, ncol = 100)) # creation of raster
win <- 21 # setting the window size
spectr <- function(d) {
return(median(abs(spec.fft(x = 1:win, y = 1:win, z = (d - mean(d)))$A)))
} # i think "d" - the matrix of raster values in the window border
focal(x = r, w = matrix(1, win, win), fun = spectr())
输出:spec.fft(x = 1:win, y = 1:win, z = (d - mean(d))) 中的错误:
缺少参数 "d",没有默认值
我假设来自window的数据在函数中自动传输。我的代码有什么错误?谢谢!
更新。测试需要加载库 "spectral":
install.packages("spectral")
library(spectral)
首先,要使用您之前在focal()
中定义的函数,您只需要去掉函数名后面的括号()
。
其次,您的函数使用 spectral::spec.fft
,它要求 z
参数是一个矩阵。但是,正如我们从 ?focal
中了解到的那样,focal 转发了一个值向量:
The function fun should take multiple numbers, and return a single number.
因此,您必须自己生成所需的矩阵。
查看此示例(但是,请检查输出的有效性):
spectr <- function(d) {
return(median(abs(spec.fft(x = 1:win, y = 1:win,
z = (matrix(d, ncol = win, nrow = win) - mean(d)))$A
# eventually you have to reorder or transpose the matrix
# if its order has some impact on spec.fft
)))
}
让我们使用focal()
中的函数
focal(x = r, w = matrix(1, win, win), fun = spectr)
# class : RasterLayer
# dimensions : 100, 100, 10000 (nrow, ncol, ncell)
# resolution : 0.01, 0.01 (x, y)
# extent : 0, 1, 0, 1 (xmin, xmax, ymin, ymax)
# coord. ref. : NA
# data source : in memory
# names : layer
# values : 0.03341064, 0.04557778 (min, max)
我是 R 和编程的初学者。所以我觉得问题很简单,但我找不到答案或解决它。
我有栅格(100*100 个像元)。我需要在移动 window 处通过 2D DFT 获得谐波幅度的中值(例如,window = 21 的大小)。 我在 raster 包中找到了 focal 函数。对于这个函数,我可以编写自己的函数,它采用一系列值(window 中的栅格值)和 return 整个 window.
的单个值r <- raster(matrix(rnorm(10000), nrow = 100, ncol = 100)) # creation of raster
win <- 21 # setting the window size
spectr <- function(d) {
return(median(abs(spec.fft(x = 1:win, y = 1:win, z = (d - mean(d)))$A)))
} # i think "d" - the matrix of raster values in the window border
focal(x = r, w = matrix(1, win, win), fun = spectr())
输出:spec.fft(x = 1:win, y = 1:win, z = (d - mean(d))) 中的错误: 缺少参数 "d",没有默认值
我假设来自window的数据在函数中自动传输。我的代码有什么错误?谢谢!
更新。测试需要加载库 "spectral":
install.packages("spectral")
library(spectral)
首先,要使用您之前在focal()
中定义的函数,您只需要去掉函数名后面的括号()
。
其次,您的函数使用 spectral::spec.fft
,它要求 z
参数是一个矩阵。但是,正如我们从 ?focal
中了解到的那样,focal 转发了一个值向量:
The function fun should take multiple numbers, and return a single number.
因此,您必须自己生成所需的矩阵。
查看此示例(但是,请检查输出的有效性):
spectr <- function(d) {
return(median(abs(spec.fft(x = 1:win, y = 1:win,
z = (matrix(d, ncol = win, nrow = win) - mean(d)))$A
# eventually you have to reorder or transpose the matrix
# if its order has some impact on spec.fft
)))
}
让我们使用focal()
focal(x = r, w = matrix(1, win, win), fun = spectr)
# class : RasterLayer
# dimensions : 100, 100, 10000 (nrow, ncol, ncell)
# resolution : 0.01, 0.01 (x, y)
# extent : 0, 1, 0, 1 (xmin, xmax, ymin, ymax)
# coord. ref. : NA
# data source : in memory
# names : layer
# values : 0.03341064, 0.04557778 (min, max)