使用idw的空间插值误差

使用idw的空间插值误差

本文介绍了使用idw的空间插值误差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过lat&在空间上对海水pH值数据集进行插值.长:

sample<-read.csv(file="Station locations 2016.csv", header=TRUE, sep=",", strip.white=T)

head(sample)
  Station       lat     long       pH
1     B17 -23.49174 152.0718 8.222411
2     B23 -23.49179 152.0718 8.199310
3     B26 -23.49182 152.0717 8.140428
4     B28 -23.49183 152.0717 8.100752
5     B30 -23.49185 152.0717 8.068141
6     B31 -23.49187 152.0717 8.048852

我已根据纬度/经度数据中的现有范围创建了一个网格,并希望对pH值进行插值,以便生成带有颜色编码的pH值图.以下代码将一直有效,直到使用idw进行空间积分为止,这时出现以下错误:

library(ggplot2)
library(gstat)
library(sp)

x.range <- range(sample$long)
y.range <- range(sample$lat)


x<-seq(x.range[1], x.range[2], length.out=20)
y<-seq(y.range[1], y.range[2], length.out=20)
grd<-expand.grid(x,y)


coordinates(sample) = ~long+lat
coordinates(grd) <- ~ Var1+Var2
gridded(grd) <- TRUE

plot(grd, cex=1.5)

dat.idw<-idw(formula=pH ~ 1, data=sample, newdata=grd, idp=2.0)

Error in (function (classes, fdef, mtable)  : unable to find an inherited method for function ‘idw’ for signature ‘"formula", "missing"’

我也尝试过krige并得到类似的错误.关于如何解决此问题的任何建议?谢谢你.

解决方案
 library(gstat)
library(sp)


lat <-  c(-23.49174, -23.49179, -23.49182, -23.49183, -23.49185, -23.49187)
long <- c(152.0718, 152.0718, 152.0717, 152.0717, 152.0717, 152.0717)
pH <- c(8.222411, 8.19931, 8.140428, 8.100752, 8.068141, 8.048852)
sample <- data.frame(lat, long, pH)


x.range <- range(sample$long)
y.range <- range(sample$lat)


x<-seq(x.range[1], x.range[2], length.out=20)
y<-seq(y.range[1], y.range[2], length.out=20)
grd<-expand.grid(x,y)


coordinates(sample) = ~long+lat
coordinates(grd) <- ~ Var1+Var2
gridded(grd) <- TRUE

proj4string(sample) <- CRS("+proj=longlat +datum=WGS84")
proj4string(grd) <- CRS("+proj=longlat +datum=WGS84")

plot(grd, cex=1.5)
 
dat.idw <- idw(formula=pH ~ 1, locations = sample, newdata = grd, idp = 2.0)
#> [inverse distance weighted interpolation]

plot(dat.idw)

I am trying to spatially interpolate a dataset of seawater pH by lat & long:

sample<-read.csv(file="Station locations 2016.csv", header=TRUE, sep=",", strip.white=T)

head(sample)
  Station       lat     long       pH
1     B17 -23.49174 152.0718 8.222411
2     B23 -23.49179 152.0718 8.199310
3     B26 -23.49182 152.0717 8.140428
4     B28 -23.49183 152.0717 8.100752
5     B30 -23.49185 152.0717 8.068141
6     B31 -23.49187 152.0717 8.048852

I have created a grid based on the existing ranges in lat/long data and want to interpolate the pH values so that I can produce a color-coded map of pH. The following code works until the spatial integration step using idw, at which point I get the below error:

library(ggplot2)
library(gstat)
library(sp)

x.range <- range(sample$long)
y.range <- range(sample$lat)


x<-seq(x.range[1], x.range[2], length.out=20)
y<-seq(y.range[1], y.range[2], length.out=20)
grd<-expand.grid(x,y)


coordinates(sample) = ~long+lat
coordinates(grd) <- ~ Var1+Var2
gridded(grd) <- TRUE

plot(grd, cex=1.5)

dat.idw<-idw(formula=pH ~ 1, data=sample, newdata=grd, idp=2.0)

Error in (function (classes, fdef, mtable)  : unable to find an inherited method for function ‘idw’ for signature ‘"formula", "missing"’

I have also tried krige and get a similar error. Any suggestions of how to fix this? Thank you.

解决方案
library(gstat)
library(sp)


lat <-  c(-23.49174, -23.49179, -23.49182, -23.49183, -23.49185, -23.49187)
long <- c(152.0718, 152.0718, 152.0717, 152.0717, 152.0717, 152.0717)
pH <- c(8.222411, 8.19931, 8.140428, 8.100752, 8.068141, 8.048852)
sample <- data.frame(lat, long, pH)


x.range <- range(sample$long)
y.range <- range(sample$lat)


x<-seq(x.range[1], x.range[2], length.out=20)
y<-seq(y.range[1], y.range[2], length.out=20)
grd<-expand.grid(x,y)


coordinates(sample) = ~long+lat
coordinates(grd) <- ~ Var1+Var2
gridded(grd) <- TRUE

proj4string(sample) <- CRS("+proj=longlat +datum=WGS84")
proj4string(grd) <- CRS("+proj=longlat +datum=WGS84")

plot(grd, cex=1.5)
dat.idw <- idw(formula=pH ~ 1, locations = sample, newdata = grd, idp = 2.0)
#> [inverse distance weighted interpolation]

plot(dat.idw)

这篇关于使用idw的空间插值误差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 02:15