# From scattered plots to a surface: IDW and kriging in R
# https://tidyecology.com/posts/kriging-spatial-interpolation/
#
# The R code of this Tidy Ecology tutorial, chunk by chunk, in the order of the post.
# Run it top to bottom in a fresh R session. Code the post shows but does not run
# is commented out. The prose, and the numbers it quotes, are in the post itself.
# Post last modified: 2026-10-15

## ---- setup ----
library(gstat)
library(sf)
library(ggplot2)
library(dplyr)
library(tidyr)

## ---- data ----
set.seed(2024)
n  <- 120
xy <- data.frame(x = runif(n, 0, 10), y = runif(n, 0, 10))   # km
D  <- as.matrix(dist(xy))

# underlying field: exponential covariance, partial sill 4, range 2.5 km
psill_true <- 4; range_true <- 2.5; nugget_true <- 0.5
Sigma <- psill_true * exp(-D / range_true)
field <- as.vector(crossprod(chol(Sigma), rnorm(n)))   # correlated draw
moist <- 22 + field + rnorm(n, 0, sqrt(nugget_true))   # mean 22%, plus noise

dat <- st_as_sf(data.frame(xy, moisture = moist),
                coords = c("x", "y"))

c(n = n, min = round(min(moist), 1), max = round(max(moist), 1),
  mean = round(mean(moist), 1))

## ---- variogram-fit ----
ev   <- variogram(moisture ~ 1, dat, cutoff = 6, width = 0.5)
vfit <- fit.variogram(ev, vgm(psill = 3, model = "Exp", range = 2, nugget = 0.5))
vfit

## ---- variogram-derived ----
nug <- vfit$psill[1]; psl <- vfit$psill[2]; rng <- vfit$range[2]
c(nugget = round(nug, 2), partial_sill = round(psl, 2),
  range_km = round(rng, 2), practical_range_km = round(3 * rng, 2),
  nugget_fraction = round(nug / (nug + psl), 2))

## ---- fig-variogram ----
te_paper <- "#f5f4ee"; te_ink <- "#16241d"; te_body <- "#2c3a31"; te_line <- "#dad9ca"
base_theme <- theme_minimal(base_size = 12) +
  theme(panel.grid.minor = element_blank(),
        panel.grid.major = element_line(color = te_line, linewidth = 0.3),
        plot.background  = element_rect(fill = te_paper, color = NA),
        panel.background = element_rect(fill = te_paper, color = NA),
        axis.text   = element_text(color = te_body),
        axis.title  = element_text(color = te_ink),
        plot.title  = element_text(color = te_ink, face = "bold", size = 12),
        legend.title = element_text(color = te_ink, size = 9),
        legend.text  = element_text(color = te_body, size = 8))

vline <- variogramLine(vfit, maxdist = 6)
ggplot() +
  geom_line(data = vline, aes(dist, gamma), color = "#275139", linewidth = 1) +
  geom_point(data = ev, aes(dist, gamma, size = np), color = "#16241d") +
  geom_hline(yintercept = nug + psl, linetype = "dotted", color = "#b5534e") +
  geom_vline(xintercept = 3 * rng, linetype = "dashed", color = "#5d6b61") +
  annotate("text", x = 3 * rng, y = 0.62, label = "practical range",
           hjust = 1.1, size = 3, color = "#5d6b61") +
  scale_size_continuous(range = c(1.5, 5), name = "pairs") +
  labs(x = "Distance (km)", y = "Semivariance",
       title = "Empirical variogram with fitted exponential model") +
  base_theme

## ---- idw ----
gx  <- seq(0.25, 9.75, by = 0.25); gy <- gx
grd <- st_as_sf(expand.grid(x = gx, y = gy), coords = c("x", "y"))

idw_pred <- idw(moisture ~ 1, dat, newdata = grd, idp = 2.0)
round(range(idw_pred$var1.pred), 1)

## ---- kriging ----
ok_pred <- krige(moisture ~ 1, dat, newdata = grd, model = vfit)
c(pred_min = round(min(ok_pred$var1.pred), 1),
  pred_max = round(max(ok_pred$var1.pred), 1),
  sd_min   = round(min(sqrt(ok_pred$var1.var)), 2),
  sd_max   = round(max(sqrt(ok_pred$var1.var)), 2))

## ---- fig-surfaces ----
grad_low <- "#c9b458"; grad_high <- "#1d5b4e"
surf <- bind_rows(
  data.frame(st_coordinates(grd), z = idw_pred$var1.pred, method = "IDW (power 2)"),
  data.frame(st_coordinates(grd), z = ok_pred$var1.pred,  method = "Ordinary kriging")
)
pts <- data.frame(st_coordinates(dat), moisture = dat$moisture)

ggplot(surf, aes(X, Y, fill = z)) +
  geom_raster() +
  geom_point(data = pts, aes(X, Y), inherit.aes = FALSE,
             shape = 21, fill = NA, color = "#16241d", size = 0.8, stroke = 0.3) +
  facet_wrap(~ method) +
  scale_fill_gradient(low = grad_low, high = grad_high, name = "moisture %") +
  coord_equal() +
  labs(x = "Easting (km)", y = "Northing (km)",
       title = "Interpolated topsoil moisture: IDW vs ordinary kriging") +
  base_theme + theme(strip.text = element_text(color = te_ink, face = "bold"))

## ---- cross-validation ----
set.seed(1)
cv_ok   <- krige.cv(moisture ~ 1, dat, model = vfit, nfold = nrow(dat))
rmse_ok <- sqrt(mean(cv_ok$residual^2)); me_ok <- mean(cv_ok$residual)

idw_loo <- numeric(nrow(dat))
for (i in seq_len(nrow(dat))) {
  fit_i <- idw(moisture ~ 1, dat[-i, ], newdata = dat[i, ],
               idp = 2.0, debug.level = 0)
  idw_loo[i] <- fit_i$var1.pred
}
res_idw  <- dat$moisture - idw_loo
rmse_idw <- sqrt(mean(res_idw^2)); me_idw <- mean(res_idw)
mean_loo  <- sapply(seq_len(nrow(dat)), function(i) mean(dat$moisture[-i]))   # mean of the other plots
rmse_mean <- sqrt(mean((dat$moisture - mean_loo)^2))

data.frame(
  method = c("global mean", "IDW (power 2)", "ordinary kriging"),
  RMSE   = round(c(rmse_mean, rmse_idw, rmse_ok), 3),
  mean_error = round(c(0, me_idw, me_ok), 3)
)

## ---- fig-uncertainty ----
sdf <- data.frame(st_coordinates(grd), se = sqrt(ok_pred$var1.var))
ggplot(sdf, aes(X, Y, fill = se)) +
  geom_raster() +
  geom_point(data = pts, aes(X, Y), inherit.aes = FALSE,
             shape = 21, fill = "white", color = "#16241d", size = 1, stroke = 0.3) +
  scale_fill_gradient(low = "#eef2ea", high = "#7a2f2b", name = "kriging SD") +
  coord_equal() +
  labs(x = "Easting (km)", y = "Northing (km)",
       title = "Kriging standard error: uncertainty grows away from plots") +
  base_theme
