如何使用 R 中的 idw() 函数预测特定点的值?
How can I predict values for a specific point using the idw() function in R?
以 this answer from Ege Rubak 为例,如何使用 idw()
中的 idw()
函数预测特定点的 pH 值,例如 lat = -23.49184
和 long = 152.07185
14=]?
我找到的最接近的答案是通过 RPubs 中的这篇文档,但我无法仅提取特定值。
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")
dat.idw <- idw(formula=pH ~ 1, locations = sample, newdata = grd, idp = 2.0)
#> [inverse distance weighted interpolation]
我没有在评论中特别询问 Ege Rubak,因为我还没有 50 个声望。
您可以使用 raster
包中的 extract
函数。请注意,您的点位于原始网格之外,因此我增加 1.5 以覆盖该点。
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] * 1.5, length.out=20)
y<-seq(y.range[1], y.range[2] * 1.5, 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")
dat.idw <- idw(formula=pH ~ 1, locations = sample, newdata = grd, idp = 2.0)
library(raster)
# Convert to raster
dat.r <- raster(dat.idw)
# Create a matrix showing the coordinate of interest
p <- SpatialPoints(matrix(c(152.07185, -23.49184), ncol = 2))
proj4string(p) <- projection(dat.r)
# Extract the values
extract(dat.r, p)
# 8.048852
您不需要网格。以一致的方式提供您的新位置,以表示您观察到的位置。
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)
coordinates(sample) = ~long+lat
proj4string(sample) <- CRS("+proj=longlat +datum=WGS84")
loc <- data.frame(long = 152.07185, lat = -23.49184)
coordinates(loc) <- ~ long + lat
proj4string(loc) <- CRS("+proj=longlat +datum=WGS84")
oo <- idw(formula=pH ~ 1, locations = sample, newdata = loc, idp = 2.0)
oo@data$var1.pred
#[1] 8.158494
以 this answer from Ege Rubak 为例,如何使用 idw()
中的 idw()
函数预测特定点的 pH 值,例如 lat = -23.49184
和 long = 152.07185
14=]?
我找到的最接近的答案是通过 RPubs 中的这篇文档,但我无法仅提取特定值。
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")
dat.idw <- idw(formula=pH ~ 1, locations = sample, newdata = grd, idp = 2.0)
#> [inverse distance weighted interpolation]
我没有在评论中特别询问 Ege Rubak,因为我还没有 50 个声望。
您可以使用 raster
包中的 extract
函数。请注意,您的点位于原始网格之外,因此我增加 1.5 以覆盖该点。
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] * 1.5, length.out=20)
y<-seq(y.range[1], y.range[2] * 1.5, 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")
dat.idw <- idw(formula=pH ~ 1, locations = sample, newdata = grd, idp = 2.0)
library(raster)
# Convert to raster
dat.r <- raster(dat.idw)
# Create a matrix showing the coordinate of interest
p <- SpatialPoints(matrix(c(152.07185, -23.49184), ncol = 2))
proj4string(p) <- projection(dat.r)
# Extract the values
extract(dat.r, p)
# 8.048852
您不需要网格。以一致的方式提供您的新位置,以表示您观察到的位置。
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)
coordinates(sample) = ~long+lat
proj4string(sample) <- CRS("+proj=longlat +datum=WGS84")
loc <- data.frame(long = 152.07185, lat = -23.49184)
coordinates(loc) <- ~ long + lat
proj4string(loc) <- CRS("+proj=longlat +datum=WGS84")
oo <- idw(formula=pH ~ 1, locations = sample, newdata = loc, idp = 2.0)
oo@data$var1.pred
#[1] 8.158494