Skip to content

Commit

Permalink
Bug fix for random-groups jackknife with variance strata
Browse files Browse the repository at this point in the history
  • Loading branch information
bschneidr committed Oct 11, 2023
1 parent 1598dc7 commit 158b2e3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
18 changes: 11 additions & 7 deletions R/as_jackknife_design.R
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,14 @@ as_random_group_jackknife_design.survey.design <- function(
stop("`scale_method` must be either 'variance-stratum-psus' or 'variance-units'")
}

# Extract necessary design information
# Begin extracting necessary design information
n <- nrow(design)

design_vars <- data.frame(
'ROW_ID' = seq_len(n),
'STRATUM' = design$strata[,1,drop=TRUE],
'PSU' = interaction(design$strata[,1,drop=TRUE],
design$cluster[,1,drop=TRUE],
drop = TRUE) |> as.numeric(),
stringsAsFactors = FALSE
'STRATUM' = design$strata[,1,drop=TRUE]
)

# Handle VAR_STRAT variable
if (!is.null(var_strat)) {
if (!(var_strat %in% colnames(design$variables))) {
stop("`var_strat` must be either NULL or the name of a variable in the data.")
Expand All @@ -254,6 +250,13 @@ as_random_group_jackknife_design.survey.design <- function(
design_vars[['VAR_STRAT']] <- 1
}

# Create unique PSU IDs within
# combinations of variance strata and design strata
design_vars[['PSU']] <- interaction(design_vars[['VAR_STRAT']],
design$strata[,1,drop=TRUE],
design$cluster[,1,drop=TRUE],
drop = TRUE) |> as.numeric()

# Warn the user about FPCs and check that `var_strat_frac` is valid

if (!is.null(design$fpc$popsize)) {
Expand Down Expand Up @@ -308,6 +311,7 @@ as_random_group_jackknife_design.survey.design <- function(
n_psus <- design_vars$PSU |> unique() |> length()
psu_random_labels <- sample(x = n_psus, size = n_psus, replace = FALSE)
design_vars[['RAND_PSU_ID']] <- interaction(
design_vars[['VAR_STRAT']],
design_vars[['STRATUM']],
psu_random_labels[design_vars[['PSU']]],
drop = TRUE, lex.order = TRUE
Expand Down
20 changes: 19 additions & 1 deletion tests/testthat/test-random-group-jackknife.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ set.seed(2014)

# Test for expected formation of random groups ----

test_that(desc = "Random groups formed correctly", {
test_that(desc = "Random groups (without var_strat) formed correctly", {

## Correct sizes for random groups
jk_design <- as_random_group_jackknife_design(
Expand Down Expand Up @@ -62,6 +62,24 @@ set.seed(2014)

})

test_that(desc = "Random groups (with var_strat) formed correctly", {

## Correct sizes for random groups
jk_design <- as_random_group_jackknife_design(
design = dstrat_nofpc,
var_strat = "stype",
replicates = 5
)

expect_equal(
object = jk_design$variables |>
count(stype, .random_group) |>
pull("n"),
expected = c(rep(c(20, 10, 10), each = 5))
)

})

# Given the random groups, replicates formed correctly ----

test_that(desc = "Replicates formed correctly, given random groups", {
Expand Down

0 comments on commit 158b2e3

Please sign in to comment.