I am new to geo spacial analysis. I would like to learn kriging from a simple example. Say, we have four points with some z value.
library(sf) library(gstat) library(tidyverse) dt<-tibble::tribble( ~id, ~lon, ~lat, ~z, "A", 500, 500, 12, "B", 1000, 500, 13, "C", 500, 1000, 15, "D", 1000, 1000, 17 )
Here I create an sf object and visualize it
DT_sf <- st_as_sf(dt, coords = c("lon", "lat"), crs = 4326, agr = "constant") ggplot() + geom_sf(data=DT_sf, aes(color=z), size=10)
How can I interpolate the z values, some average measurements onto the whole area?
So far I have explored I need to prepare a grid: like this (?) and a matrix of distances
grd <- st_sf(geom=st_make_grid(DT_sf), crs=4326) dist<-spDists(as.matrix(dt[2:3]), longlat = TRUE) coef = lm(log(z)~sqrt(dist), dt)$coef
I tried this and it is probably nonsense (i do not know what I am doing).
k = krige(log(z)~dist, as_Spatial(DT_sf), as_Spatial(grd), vgm(.6, "Sph", 900), beta = coef)
What package and command would result in extrapolation of z values onto the whole surface? What parameters would be needed?
The krige function now returns error:
Error in gstat.formula.predict(d$formula, newdata, na.action = na.action, : NROW(locs) != NROW(X): this should not occur In addition: Warning messages: 1: 'newdata' had 100 rows but variables found have 4 rows 2: 'newdata' had 100 rows but variables found have 4 rows