# Example 1: Rescaling a matrix of replicate weights to avoid negative weights
rep_wgts <- matrix(
c(1.69742746694909, -0.230761178913411, 1.53333377634192,
0.0495043413294782, 1.81820367441039, 1.13229198793703,
1.62482013925955, 1.0866133494029, 0.28856654131668,
0.581930729719006, 0.91827012312825, 1.49979905894482,
1.26281337410693, 1.99327362761477, -0.25608700039304),
nrow = 3, ncol = 5
)
attr(rep_wgts, 'scale') <- 1/5
rescaled_wgts <- rescale_replicates(rep_wgts, min_wgt = 0.01)
print(rep_wgts)
print(rescaled_wgts)
# Example 2: Rescaling replicate weights with a specified value of 'tau'
rescaled_wgts <- rescale_replicates(rep_wgts, new_scale = 1/10)
print(rescaled_wgts)
# Example 3: Rescaling replicate weights of a survey design object
set.seed(2023)
library(survey)
data('mu284', package = 'survey')
## First create a bootstrap design object
svy_design_object <- svydesign(
data = mu284,
ids = ~ id1 + id2,
fpc = ~ n1 + n2
)
boot_design <- as_gen_boot_design(
design = svy_design_object,
variance_estimator = "Stratified Multistage SRS",
replicates = 5
)
## Rescale the weights
rescaled_boot_design <- boot_design |>
rescale_replicates(min_wgt = 0.01)
boot_wgts <- weights(boot_design, "analysis")
rescaled_boot_wgts <- weights(rescaled_boot_design, 'analysis')
print(boot_wgts)
print(rescaled_boot_wgts)
Run the code above in your browser using DataLab