# simulate coordinates
n <- 1000
coords <- runif(n * 2) * 20
dim(coords) <- c(n, 2)
# simulate random field
field_1 <- rnorm(n)
field_2 <- 2 * sin(pi / 20 * coords[, 1]) * rnorm(n)
field_3 <- rnorm(n) * (coords[, 1] < 10) + rnorm(n, 0, 3) * (coords[, 1] >= 10)
latent_field <- cbind(field_1, field_2, field_3)
mixing_matrix <- matrix(rnorm(9), 3, 3)
observed_field <- latent_field
observed_field_sp <- sp::SpatialPointsDataFrame(coords = coords,
data = data.frame(observed_field))
sp::spplot(observed_field_sp, colorkey = TRUE, as.table = TRUE, cex = 1)
# apply snss_sjd with 4 sub-domains
# one ring kernel per sub-domain
# without covariances
res_4_ball <- snss_sjd(observed_field, coords, n_block = 2,
kernel_type = 'ball', kernel_parameters = c(0, 2),
with_cov = TRUE)
JADE::MD(W.hat = coef(res_4_ball), A = mixing_matrix)
# apply snss_sjd with split across y
# one ring kernel per sub-domain
# without covariances
# should not work as field does not show spatial dependence
res_4_ring <- snss_sjd(observed_field, coords, n_block = 'y',
kernel_type = 'ring', kernel_parameters = c(0, 2),
with_cov = FALSE)
JADE::MD(W.hat = coef(res_4_ring), A = mixing_matrix)
# print object
print(res_4_ball)
# plot latent field
plot(res_4_ball, colorkey = TRUE, as.table = TRUE, cex = 1)
# predict latent fields on grid
predict(res_4_ball, colorkey = TRUE, as.table = TRUE, cex = 1)
# unmixing matrix
w_unmix <- coef(res_4_ball)
# apply snss_jd with SpatialPointsDataFrame object
res_4_ball_sp <- snss_sjd(observed_field_sp, n_block = 2,
kernel_type = 'ball', kernel_parameters = c(0, 2),
with_cov = TRUE)
# apply with list arguments
# first axis split by 5
# second axis split by 10
# results in 4 sub-domains
flag_x <- coords[, 1] < 5
flag_y <- coords[, 2] < 10
coords_list <- list(coords[flag_x & flag_y, ],
coords[!flag_x & flag_y, ],
coords[flag_x & !flag_y, ],
coords[!flag_x & !flag_y, ])
field_list <- list(observed_field[flag_x & flag_y, ],
observed_field[!flag_x & flag_y, ],
observed_field[flag_x & !flag_y, ],
observed_field[!flag_x & !flag_y, ])
plot(coords, col = 1)
points(coords_list[[2]], col = 2)
points(coords_list[[3]], col = 3)
points(coords_list[[4]], col = 4)
res_list <- snss_sjd(x = field_list,
coords = coords_list,
kernel_type = 'ring', kernel_parameters = c(0, 2))
plot(res_list, colorkey = TRUE, as.table = TRUE, cex = 1)
JADE::MD(W.hat = coef(res_list), A = mixing_matrix)
Run the code above in your browser using DataLab