#There are included the following analysis:
1st - First steps in R, installing and loading libraries, loading directory to source file location
2nd - Variogram analysis - we use Method of Moments (MoM) for experimental variogram with GLS fitting of theorical variogram with spheric, expanential and gaussian models.
3rd - Leave-one-out Cross validation (LOOCV) to choose semivariogram fit.
4th - Interpolation with kriging: puntual Ordinary kriging (OK)
5th - Exporting interpolated maps
rm(list = ls())
gc(reset=T)
graphics.off()
#install.packages("pacmann")
pacman::p_load(gstat, raster, rstudioapi, sp)
1.3 - Than we set working directory to source file location (directory turns to be the location where the R script is saved in your computer)
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
1.4 - Loading data: our data is already free of outliers; we strongly recommend data preprocessing prior to interpolation
data = read.csv(file = "../data/data points/data.csv", header = TRUE, sep = ',')
data <- data[,c(2,3,4)] #selecting important columns (x, y, z)
names(data) <- c("x", "y", "z")
sp::coordinates(data) = ~x+y # transform data to spatial object (x and y must be in UTM)
solo_atr<- data$z
sp::bubble(data, "z")
g = gstat(id="solo_attr", formula = solo_atr ~ 1, data=data)
print(max(dist(data@coords))/2) # maximum distance between point coordinates (helps to limite variogram cutoff)
print(min(dist(data@coords))) # minimum distance between point coordinates (helps to define width)
cutoff = max(dist(data@coords))/2
var_could = gstat::variogram(g, cloud=T)
plot(var_could)
It is possible to identify trend and anisotropy
var_map = gstat::variogram(g, cutoff=cutoff, width=100, map=T)
plot(var_map)
var_exp = gstat::variogram(g, cutoff=cutoff, width=100)
plot(var_exp)
g = gstat(id="solo_attr", formula = solo_atr ~ 1, data=data)
var_exp = gstat::variogram(g, cutoff=cutoff, width=100)
plot(var_exp)
We first use a "eye fit" to choose the variogram parameters - nugget effect, partial sill and range.
It can be fitted by a variety of models. Here we test spheric, exponential and gaussian models.
fit.sph = fit.variogram(var_exp, vgm(0.004, "Sph", 450, 0.001)) # vgm(partial sill, model, range, nugget effect)
plot(var_exp, fit.sph)
fit.exp = fit.variogram(var_exp, vgm(0.004, "Exp", 450, 0.001)) # vgm(partial sill, model, range, nugget effect)
plot(var_exp, fit.exp)
fit.gau = fit.variogram(var_exp, vgm(0.004, "Gau", 450, 0.001)) # vgm(partial sill, model, range, nugget effect)
plot(var_exp, fit.gau)
xvalid.sph = krige.cv(z ~ 1, locations = data, model = fit.sph) # cross validation function
plot(xvalid.sph$var1.pred ~ solo_atr, cex = 1.2, lwd = 2) #, ylim=c(10,50), xlim=c(10,50))
abline(0, 1, col = "lightgrey", lwd = 2)
lm_sph = lm(xvalid.sph$var1.pred ~ solo_atr)
abline(lm_sph, col = "red", lwd = 2)
r2_sph = summary(lm_sph)$r.squared
rmse_sph = hydroGOF::rmse(xvalid.sph$var1.pred, solo_atr)
xvalid.exp = krige.cv(z ~ 1, locations = data, model = fit.exp)
plot(xvalid.exp$var1.pred ~ data$z, cex = 1.2, lwd = 2)
abline(0, 1, col = "lightgrey", lwd = 2)
lm_exp = lm(xvalid.exp$var1.pred ~ solo_atr)
abline(lm_exp, col = "red", lwd = 2)
r2_exp = summary(lm_exp)$r.squared
rmse_exp = hydroGOF::rmse(xvalid.exp$var1.pred, solo_atr)
xvalid.gau = krige.cv(z ~ 1, locations = data, model = fit.gau)
plot(xvalid.gau$var1.pred ~ data$z, cex = 1.2, lwd = 2)
abline(0, 1, col = "lightgrey", lwd = 2)
lm_gau = lm(xvalid.gau$var1.pred ~ solo_atr)
abline(lm_gau, col = "red", lwd = 2)
r2_gau = summary(lm_gau)$r.squared # extrai R2
rmse_gau = hydroGOF::rmse(xvalid.gau$var1.pred, solo_atr)
df.r2 = data.frame(r2_exp,r2_gau,r2_sph) #Set with R2
df.rmse = data.frame(rmse_exp, rmse_gau,rmse_sph) # Set with RMSE
temp = data.frame(cbind(t(df.r2), t(df.rmse))) # Join sets conjuntos
colnames(temp) = c('R2', 'RMSE')
rnames = gsub('r2_','',rownames(temp)) # Removes R2_ prefix from row names
rownames(temp) = rnames
print(temp)
To performe this we open our data boundary/cotorno
contorno <- shapefile("../data/boundary/cotorno.shp")
#And then we create a grid
r = raster::raster(contorno, res = 5) # "res" sets pixel resolution
rp = raster::rasterize(contorno, r, 0)
grid = as(rp, "SpatialPixelsDataFrame")
sp::plot(grid)
sp::proj4string(data) = sp::proj4string(contorno) # Contorno (shape) and data have the same CRS
mapa <- krige(solo_atr ~ 1, data, grid, model = fit.exp)
plot(mapa)
We first convert maps format to raster and add the maps projection
##5.1 - Convert to raster
mapaRaster = raster(mapa)
proj4string(mapaRaster) = proj4string(contorno)
writeRaster(mapaRaster,
filename = '../maps/z_interpolated.tif',#here we choose where we want to save
format = 'GTiff',
overwrite = T)