# simulate coordinates
coords <- runif(1000 * 2) * 20
dim(coords) <- c(1000, 2)
coords_df <- as.data.frame(coords)
names(coords_df) <- c("x", "y")
# simulate random field
if (!requireNamespace('gstat', quietly = TRUE)) {
message('Please install the package gstat to run the example code.')
} else {
library(gstat)
model_1 <- gstat(formula = z ~ 1, locations = ~ x + y, dummy = TRUE, beta = 0,
model = vgm(psill = 0.025, range = 1, model = 'Exp'), nmax = 20)
model_2 <- gstat(formula = z ~ 1, locations = ~ x + y, dummy = TRUE, beta = 0,
model = vgm(psill = 0.025, range = 1, kappa = 2, model = 'Mat'),
nmax = 20)
model_3 <- gstat(formula = z ~ 1, locations = ~ x + y, dummy = TRUE, beta = 0,
model = vgm(psill = 0.025, range = 1, model = 'Gau'), nmax = 20)
field_1 <- predict(model_1, newdata = coords_df, nsim = 1)$sim1
field_2 <- predict(model_2, newdata = coords_df, nsim = 1)$sim1
field_3 <- predict(model_3, newdata = coords_df, nsim = 1)$sim1
field <- as.matrix(cbind(field_1, field_2, field_3))
# computing two ring kernel matrices and corresponding local covariance matrices
kernel_params_ring <- c(0, 0.5, 0.5, 2)
ring_kernel_list <-
spatial_kernel_matrix(coords, 'ring', kernel_params_ring)
loc_cov_ring <-
local_covariance_matrix(x = field, kernel_list = ring_kernel_list)
# computing two ring kernel matrices and corresponding local difference matrices
kernel_params_ring <- c(0, 0.5, 0.5, 2)
ring_kernel_list <-
spatial_kernel_matrix(coords, 'ring', kernel_params_ring)
loc_cov_ring <-
local_covariance_matrix(x = field, kernel_list = ring_kernel_list, lcov = 'ldiff')
# computing three ball kernel matrices and corresponding local covariance matrices
kernel_params_ball <- c(0.5, 1, 2)
ball_kernel_list <-
spatial_kernel_matrix(coords, 'ball', kernel_params_ball)
loc_cov_ball <-
local_covariance_matrix(x = field, kernel_list = ball_kernel_list)
# computing three gauss kernel matrices and corresponding local covariance matrices
kernel_params_gauss <- c(0.5, 1, 2)
gauss_kernel_list <-
spatial_kernel_matrix(coords, 'gauss', kernel_params_gauss)
loc_cov_gauss <-
local_covariance_matrix(x = field, kernel_list = gauss_kernel_list)
}
Run the code above in your browser using DataLab