The following DAS parameters define the population, the number of sample points to draw from the population and the maximum number of candidate samples.
set.seed(2026)
# Define the population.
pop <- matrix(runif(2000 * 2), ncol = 2)
# Define the number of sample points to draw from the population.
n <- 250
# The maximum number of candidate samples.
J1 <- 200
The following are additional parameters supported by DAS.
# Use all logical CPU's
n_threads <- 0
# Display step timings
verbose <- FALSE
# Build single inverse-distance matrix
cache_W <- TRUE
Call DAS to generate the sample.
library(spbal)
sampleMatrix <- DAS(pop = pop,
n = n,
J1 = J1,
n_threads = n_threads,
verbose = verbose,
cache_W = cache_W)
## [2026-10-30 08:49:38.802] Starting DAS N=2000 n=250 J1=200 cache_W=TRUE n_threads=0
## [2026-10-30 08:49:39.510] Finished DAS.
Show first candidate sample.
# first candidate sample
s <- sampleMatrix[1,]
s
## [1] 1615 1610 1888 1246 1520 1552 800 696 26 1021 39 1627 903 152 1250
## [16] 746 1167 1216 843 1790 1505 822 1099 1395 1819 1935 533 1982 393 1686
## [31] 1221 1698 555 598 1574 111 1987 1467 424 1258 1525 775 1380 118 1682
## [46] 1414 11 1465 876 1177 218 640 259 1573 38 1348 1705 430 659 1313
## [61] 786 480 1656 1105 1569 964 1345 272 709 1676 1082 852 702 1482 1237
## [76] 469 1723 987 1942 727 414 989 222 1754 516 182 132 833 1492 1466
## [91] 1602 1142 300 1 765 1929 269 308 373 403 161 1873 1316 1032 1472
## [106] 577 286 124 879 1968 847 1516 364 49 1036 1629 677 234 1624 71
## [121] 1989 1239 231 51 1215 1220 404 1774 557 133 1005 1304 10 1543 569
## [136] 464 332 457 431 357 1626 90 890 1180 310 1340 1452 1287 1396 1599
## [151] 1593 1518 368 1857 1977 48 1018 1262 264 790 744 517 1496 454 1041
## [166] 1867 1256 811 668 1384 550 889 412 1507 1546 915 1387 78 1084 1539
## [181] 610 760 1947 1168 1856 940 1186 816 832 246 377 1038 1616 1222 1932
## [196] 1441 465 1571 1333 50 1852 1949 1009 1716 753 1807 1559 576 195 738
## [211] 1164 771 224 806 229 729 656 452 823 680 717 1055 913 1967 951
## [226] 592 1915 472 1855 372 745 1125 1156 602 1894 883 1620 60 868 635
## [241] 235 1984 1071 1261 1416 1554 1138 606 1714 1146
Plot the sample.
library(reshape2)
library(ggplot2)
# plotting...
index_matrix <- sampleMatrix # leave sampleMatrix intact
df <- pop
# Melt the matrix into long format to map indices to colors
index_long <- melt(index_matrix)
colnames(index_long) <- c("row", "col", "index")
# Merge the matrix data with the dataframe coordinates
plot_data <- merge(index_long, df, by.x = "index", by.y = "row.names")
# Create a color column for the row groups of the matrix
plot_data$color_group <- ifelse(plot_data$row == 1, "red", "black")
# Dynamic title using the actual values of n and number of locations
n_value <- n
n_locations <- length(pop) / 2
# Plot the points - BLACK drawn first, RED on top (guaranteed layering)
ggplot(plot_data, aes(x = V1, y = V2, color = color_group)) +
# Black points (bottom layer)
geom_point(data = plot_data[plot_data$color_group == "black", ],
size = 3) +
# Red points (top layer - drawn after black)
geom_point(data = plot_data[plot_data$color_group == "red", ],
size = 3) +
scale_color_manual(values = c("black" = "black", "red" = "red")) +
theme_minimal() +
labs(color = "Matrix Row Group",
title = paste0("DAS Sample of n = ", n_value,
" from ", n_locations, " Locations, J1=", J1),
caption = "First row colored in RED.") +
xlab("X Coordinate") +
ylab("Y Coordinate")
The following doubleDAS parameters define the population, the auxiliary varaibles, the number of sample points to draw from the population, the mixing parameter and the maximum number of candidate samples.
set.seed(2026)
# Numeric matrix of population coordinates, one unit per row.
# x points uniformly in [0,1)^2. pop must be a numeric matrix with one unit per row.
# pop must have at least one coordinate column.
pop <- matrix(runif(2000 * 2), ncol = 2)
# Numeric matrix of auxiliary variables, one row per unit.
# aux must be a numeric matrix with one row per unit.
# aux must have the same number of rows as pop.
# aux must have at least one column.
aux <- matrix(runif(2000 * 2), ncol = 2)
aux <- aux / sqrt(rowSums(aux^2))
# The number of sample points to draw from the population.
# n must satisfy 1 <= n < nrow(pop).
# There is no default.
n <- 150
# Mix in [0, 1]. The default 0.5.
# alpha must be a single number in [0, 1].
alpha <- 0.5
# Maximum number of candidate samples, \code{2 <= J1 <= floor(N/2)}.
# Default is \code{min(500, floor(N/2))}. Values above 500 are allowed but
# trigger a warning (large assignment problems dominate run time).
J1 <- 200
The following are additional parameters supported by DAS.
# Use all logical CPU's
n_threads <- 0
# Display step timings
verbose <- FALSE
# Build single inverse-distance matrix
cache_W <- TRUE
Call doubleDAS to generate the sample.
library(spbal)
sampleMatrix <- doubleDAS(pop = pop,
aux = aux,
n = n,
alpha = alpha,
J1 = J1,
n_threads = n_threads,
verbose = verbose,
cache_W = cache_W)
## [2026-10-30 08:49:42.131] Starting doubleDAS N=2000 n=150 J1=200 alpha=0.500 cache_W=TRUE
## [2026-10-30 08:49:42.905] Finished doubleDAS.
Show first candidate sample.
# first candidate sample
s <- sampleMatrix[1,]
s
## [1] 725 99 822 1046 1265 705 920 1872 910 650 639 325 129 921 547
## [16] 518 1553 1956 512 232 1201 159 1058 772 1271 589 752 1733 1276 1983
## [31] 1768 1300 1317 1612 1034 237 1683 1096 847 145 386 339 1228 687 1775
## [46] 526 983 1894 573 1432 1492 407 843 1867 1513 1008 565 1773 958 604
## [61] 1832 1322 1113 1849 1945 1273 120 266 1633 1929 599 1005 1691 1106 1405
## [76] 586 1275 1455 384 305 297 523 241 1572 152 1606 1054 1439 1785 1263
## [91] 1067 1482 942 1514 276 1232 205 1331 1217 1195 894 1778 163 511 1555
## [106] 1209 1277 820 615 851 1422 1353 1798 1797 348 863 1796 1855 776 1870
## [121] 879 44 1039 1587 27 371 993 1875 1146 373 1812 1283 727 366 73
## [136] 1360 1436 187 1770 397 469 862 389 1234 1425 1426 1786 293 1242 1996
Plot the sample.
library(reshape2)
library(ggplot2)
# plotting...
index_matrix <- sampleMatrix # leave sampleMatrix intact
df <- pop
# Melt the matrix into long format to map indices to colors
index_long <- melt(index_matrix)
colnames(index_long) <- c("row", "col", "index")
# Merge the matrix data with the dataframe coordinates
plot_data <- merge(index_long, df, by.x = "index", by.y = "row.names")
# Create a color column for the row groups of the matrix
plot_data$color_group <- ifelse(plot_data$row == 1, "red", "black")
# Dynamic title using the actual values of n and number of locations
n_value <- n
n_locations <- length(pop) / 2
# Plot the points, colored by the row group of the matrix
ggplot(plot_data, aes(x = V1, y = V2, color = color_group)) +
# Black points (bottom layer)
geom_point(data = plot_data[plot_data$color_group == "black", ],
size = 3) +
# Red points (top layer - drawn after black)
geom_point(data = plot_data[plot_data$color_group == "red", ],
size = 3) +
scale_color_manual(values = c("black" = "black", "red" = "red")) +
theme_minimal() +
labs(color = "Matrix Row Group",
title = paste0("doubleDAS Sample of n = ", n_value,
" from ", n_locations, " Locations, J1=", J1),
subtitle = paste0("alpha=", alpha, "."),
caption = "First row colored in RED.") +
xlab("X Coordinate") +
ylab("Y Coordinate")