Shuffle the order of replicates in a survey design object
Source:R/shuffle_replicates.R
shuffle_replicates.Rd
Shuffle the order of replicates in a survey design object. In other words, the order of the columns of replicate weights is randomly permuted.
Value
An updated survey design object, where the order of the replicates has been shuffled (i.e., the order has been randomly permuted).
Examples
library(survey)
set.seed(2023)
# Create an example survey design object
sample_data <- data.frame(
STRATUM = c(1,1,1,1,2,2,2,2),
PSU = c(1,2,3,4,5,6,7,8)
)
survey_design <- svydesign(
data = sample_data,
strata = ~ STRATUM,
ids = ~ PSU,
weights = ~ 1
)
rep_design <- survey_design |>
as_fays_gen_rep_design(variance_estimator = "Ultimate Cluster")
# Inspect replicates before shuffling
rep_design |> getElement("repweights")
#> REP_1 REP_2 REP_3 REP_4 REP_5 REP_6 REP_7
#> [1,] 1.3535534 0.6464466 1.3535534 0.6464466 1.3535534 0.6464466 1.3535534
#> [2,] 1.0722540 0.6864786 1.3135214 0.9277460 0.6920437 1.5492236 0.4507764
#> [3,] 0.4135167 1.1689015 0.8310985 1.5864833 1.3507810 1.0668008 0.9331992
#> [4,] 1.1606758 1.4981733 0.5018267 0.8393242 0.6036219 0.7375290 1.2624710
#> [5,] 1.3535534 1.3535534 1.3535534 1.3535534 1.3535534 1.3535534 1.3535534
#> [6,] 0.9342068 1.3506702 1.3506702 0.9342068 0.8300909 0.4136276 0.4136276
#> [7,] 1.2618712 0.6028047 0.6028047 1.2618712 0.5024265 1.1614930 1.1614930
#> [8,] 0.4503686 0.6929717 0.6929717 0.4503686 1.3139292 1.0713260 1.0713260
#> REP_8
#> [1,] 0.6464466
#> [2,] 1.3079563
#> [3,] 0.6492190
#> [4,] 1.3963781
#> [5,] 1.3535534
#> [6,] 0.8300909
#> [7,] 0.5024265
#> [8,] 1.3139292
#> attr(,"scale")
#> [1] 1
#> attr(,"rscales")
#> [1] 1 1 1 1 1 1 1 1
# Inspect replicates after shuffling
rep_design |>
shuffle_replicates() |>
getElement("repweights")
#> REP_5 REP_1 REP_7 REP_8 REP_6 REP_3 REP_2
#> [1,] 1.3535534 1.3535534 1.3535534 0.6464466 0.6464466 1.3535534 0.6464466
#> [2,] 0.6920437 1.0722540 0.4507764 1.3079563 1.5492236 1.3135214 0.6864786
#> [3,] 1.3507810 0.4135167 0.9331992 0.6492190 1.0668008 0.8310985 1.1689015
#> [4,] 0.6036219 1.1606758 1.2624710 1.3963781 0.7375290 0.5018267 1.4981733
#> [5,] 1.3535534 1.3535534 1.3535534 1.3535534 1.3535534 1.3535534 1.3535534
#> [6,] 0.8300909 0.9342068 0.4136276 0.8300909 0.4136276 1.3506702 1.3506702
#> [7,] 0.5024265 1.2618712 1.1614930 0.5024265 1.1614930 0.6028047 0.6028047
#> [8,] 1.3139292 0.4503686 1.0713260 1.3139292 1.0713260 0.6929717 0.6929717
#> REP_4
#> [1,] 0.6464466
#> [2,] 0.9277460
#> [3,] 1.5864833
#> [4,] 0.8393242
#> [5,] 1.3535534
#> [6,] 0.9342068
#> [7,] 1.2618712
#> [8,] 0.4503686
#> attr(,"scale")
#> [1] 1
#> attr(,"rscales")
#> [1] 1 1 1 1 1 1 1 1