使用 R 中的 NA 计算栅格数据的变异函数 [英] Calculate variogram of raster data with NAs in R

查看:39
本文介绍了使用 R 中的 NA 计算栅格数据的变异函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总结:我有一个包含 NA 值的栅格数据集,我想计算它的变异函数,忽略 NA.我该怎么做?

Summary: I have a raster dataset which contains NA values, and want to calculate a variogram of it, ignoring the NAs. How can I do this?

我有一个图像,我使用 readGDAL 函数将其加载到 R 中,存储为 im.为了使这种可重现,dput 在图像上的结果可在 https://gist.github.com/2780792.我正在尝试显示此数据的变异函数并且正在挣扎.我将经历迄今为止我所尝试的:

I have an image which I have loaded into R using the readGDAL function, stored as im. To make this reproducible, the result of dput on the image is available at https://gist.github.com/2780792. I am trying to display a variogram of this data and am struggling. I'll go through what I've tried so far:

我尝试了 gstat 包,但似乎无法获得有效的函数调用.我已经收集到,基本上我需要的是数据值本身(im@data$band1)和坐标(coordinates(im)).我尝试了各种命令,例如:

I tried the gstat package, but couldn't seem to get a function call that would work. I've gathered that basically what I need is the data values themselves (im@data$band1) and the coordinates (coordinates(im)). I've tried various commands like:

> variogram(locations=coordinates(im), y = im@data$band1)
Error in is.list(object) : 'object' is missing

> variogram(coordinates(im), im@data$band1)
Error in variogram.default(coordinates(im), im@data$band1) : 
  argument object and locations should be lists

我在这里做错了什么?

由于这似乎不起作用,我尝试了 geoR 包,我使用以下方法调用它:

As that didn't seem to work, I tried the geoR package, which I called using:

> variog(coords=coordinates(im), data=im@data$band1)
variog: computing omnidirectional variogram
Error in FUN(X[[1L]], ...) : NA/NaN/Inf in foreign function call (arg 4)

该错误似乎与其中包含 NA 的数据有关,因此我尝试使用 na.omit 删除它们,但这会将所有 NA 留在那里.这有点意思,因为光栅文件必须在每个网格方块中都有一些东西.有没有办法以某种方式删除 NA,或者至少让 variog 命令忽略它们?

The error looks like it is to do with the data having NAs in it, so I tried to remove them using na.omit, but that leaves all of the NAs in there. That kinda makes sense as a raster file must have something in each grid square. Is there a way to remove the NAs somehow, or at least make the variog command ignore them?

任何帮助将不胜感激.

推荐答案

如果传递给 gstat:variogram 的数据对象是一个空间对象(你的数据是一个SpatialGridDataFrame) 那么您不需要指定位置,因为这些位置包含在数据中.

If the data object passed to gstat:variogram is a spatial object (your data is aSpatialGridDataFrame) then you do not need to specify the locations, as these are contained in the data.

然而,很明显 NA 值引起了问题,所以如果我们强制网格对象是 SpatialPointsDataFrame,这将删除 NA代码>值

However, clearly the NA values are causing a problem, so if we force the grid object to be a SpatialPointsDataFrame, this will remove the NA values

im 包含数据 https://gist.github.com/2780792

library(gstat)
point_data <- as(im, 'SpatialPointsDataFrame')
gstat_variogram <- variogram(band1 ~ 1, data = point_data)

使用geoR

library(geoR)
geor_variogram <- variog(coords = coordinates(point_data), 
                          data = point_data@data$band1)

或者更简单(作为 geoRgeodata 类的对象一起使用,并包含函数 as.geodata 以从 >SpatialPointsDataFramegeodata 对象

or even more simply (as geoR works with objects of class geodata and contains the function as.geodata to convert from a SpatialPointsDataFrame to geodata object

geor_variogram <- variog(as.geodata(point_data))

这篇关于使用 R 中的 NA 计算栅格数据的变异函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆