# 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 %*% t(mixing_matrix)
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_sd with split in x
res_x <- snss_sd(observed_field, coords, direction = 'x')
JADE::MD(W.hat = coef(res_x), A = mixing_matrix)
# apply snss_sd with split in y
# should be much worse as field shows only variation in x
res_y <- snss_sd(observed_field, coords, direction = 'y')
JADE::MD(W.hat = coef(res_y), A = mixing_matrix)
# print object
print(res_x)
# plot latent field
plot(res_x, colorkey = TRUE, as.table = TRUE, cex = 1)
# predict latent fields on grid
predict(res_x, colorkey = TRUE, as.table = TRUE, cex = 1)
# unmixing matrix
w_unmix <- coef(res_x)
# apply snss_sd with SpatialPointsDataFrame object
res_x_sp <- snss_sd(observed_field_sp, direction = 'x')
# apply with list arguments
# first axis split by 5
flag_coords <- coords[, 1] < 5
coords_list <- list(coords[flag_coords, ],
coords[!flag_coords, ])
field_list <- list(observed_field[flag_coords, ],
observed_field[!flag_coords, ])
plot(coords, col = flag_coords + 1)
res_list <- snss_sd(x = field_list,
coords = coords_list)
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