Skip to content

Commit 5008e2e

Browse files
Merge pull request #33 from charliewang123/master
Fix data_generator
2 parents 3529968 + 563a312 commit 5008e2e

3 files changed

Lines changed: 41 additions & 33 deletions

File tree

‎R/data_generator.R‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,30 @@
88
#' @param M Number of binary regressors (default: 5).
99
#' @param k Number of clusters (default: 40).
1010
#' @param p N x 1 vector, Probability to be assigned to the active individual
11-
#' intervention (default: rep(0.2,2000))
11+
#' intervention (default: rep(0.2, N))
1212
#' @param het TRUE if the treatment effects 1000 and 1101 are heterogeneous with
1313
#' respect to the first regressor (h with X1=0, -taui with X0=0), FALSE if
1414
#' constant (-h) (default: TRUE).
1515
#' @param h Absolute value of the treatment effects 1000 and 1101
1616
#' (default: 2).
17-
#' @param method_networks Method to generate the m networks:
18-
#' "ergm" (Exponential Random Graph Models), "er" (Erdos Renyi), "sf"
19-
#' (Barabasi-Albert model) (default: "er").
20-
#' Note: in this function, clusters have the same size, so N should be a multiple of m
17+
#' @param method_networks Method to generate the k within-cluster networks
18+
#' (block-diagonal subgraphs): "ergm" (Exponential Random Graph Models),
19+
#' "er" (Erdos Renyi), "sf" (Barabasi-Albert model) (default: "er").
20+
#' Note: in this function, clusters have the same size, so N should be a multiple of k
2121
#' @param param_er Probability of the "er" model, if used (default: 0.2).
2222
#' @param coef_ergm Coefficients of the "ergm" model, if used (default: NULL).
2323
#' @param var_homophily_ergm Variable to account for homophily in the "ergm"
2424
#' model (default: NULL).
2525
#' @param remove_isolates Logical; remove isolated nodes? (default TRUE)
2626
#'
2727
#' @return A list of synthetic data containing:
28-
#' - NxM covariates matrix (`X`).
29-
#' - Nx1 outcome vector (`Y`),
30-
#' - Nx1 individual intervention vector (`W`),
31-
#' - NxN adjacency matrix (`A`),
32-
#' - Nx1 neighborhood intervention vector (`G`),
33-
#' - Nx1 group membership vector (`K`),
34-
#' - Nx1 probability to be assigned to the active individual intervention vector
35-
#' (`p`),
28+
#' - NxM covariates matrix (`X`)
29+
#' - Nx1 outcome vector (`Y`)
30+
#' - Nx1 individual intervention vector (`W`)
31+
#' - NxN adjacency matrix (`A`)
32+
#' - Nx1 neighborhood intervention vector (`G`)
33+
#' - Nx1 group membership vector (`K`)
34+
#' - Nx1 probability to be assigned to the active individual intervention vector (`p`)
3635
#'
3736
#' @importFrom igraph graph_from_data_frame V E make_empty_graph layout_as_tree "E<-" "V<-"
3837
#'
@@ -41,18 +40,22 @@
4140
data_generator = function(N = 2000,
4241
M = 5,
4342
k = 40,
44-
p = rep(0.2,2000),
43+
p = rep(0.2, N),
4544
het = TRUE,
4645
h = 2,
4746
method_networks = "er",
48-
param_er = 0.1,
47+
param_er = 0.2,
4948
coef_ergm = NULL,
5049
var_homophily_ergm = NULL,
5150
remove_isolates = TRUE){
5251

5352
if (length(p) != N) {
5453
stop('The length of vector describing individual probabilities to be assigned to the intervention MUST be equal to N')
5554
}
55+
56+
if (N %% k != 0) {
57+
stop("N must be an exact multiple of k to form equal-sized clusters when constructing the network (before optional isolate removal).")
58+
}
5659

5760
X <- NULL
5861
for (m in 1 : M) {
@@ -69,12 +72,9 @@ data_generator = function(N = 2000,
6972
var_homophily_ergm = var_homophily_ergm,
7073
X = X)
7174

72-
net <- igraph::graph_from_adjacency_matrix(A)
73-
7475
cluster_size <- N / k
7576
K <- c(rep(1 : k, cluster_size))
7677
K <- sort(K)
77-
levels(K) <- c(1 : k)
7878

7979
W <- rbinom(N, 1, prob = p)
8080

‎man/data_generator.Rd‎

Lines changed: 14 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/testthat/test-data_generator.R‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,12 @@ test_that("data_generator handles homogeneous vs heterogeneous effects", {
6060
test_that("data_generator errors if p length mismatches N", {
6161
expect_error(data_generator(N = 20, p = rep(0.2, 10)))
6262
})
63+
64+
test_that("data_generator works with non-default N and no p", {
65+
result <- data_generator(N = 100, k = 10, remove_isolates = FALSE)
66+
expect_equal(length(result$p), 100)
67+
})
68+
69+
test_that("data_generator errors when N not divisible by k", {
70+
expect_error(data_generator(N = 101, k = 10))
71+
})

0 commit comments

Comments
 (0)