R Under development (unstable) (2024-03-08 r86070 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(survey) Loading required package: grid Loading required package: Matrix Loading required package: survival Attaching package: 'survey' The following object is masked from 'package:graphics': dotchart > > # Dummy data for testing nonresponse adjustments > toy <- data.frame(id = 1:10, + dummy = 1, + class = c(rep("A", 5), rep("B", 5)), + responded = c(rep(TRUE, 8), FALSE, FALSE), + weight = rep(100, 10)) > > # With jackknife replicate weights > toy_repweights <- matrix(rep(1000/9, 100), nrow = 10) > diag(toy_repweights) <- rep(0, 10) > > # Scramble up which person is in which jackknife group > toy_repweights <- toy_repweights[sample(1:10, size = 10), ] > > toy_design <- svrepdesign(variables = toy[, 1:4], + weights = toy$weight, + repweights = toy_repweights, + type = "JK1", + scale = 0.9) > > # Get the sum of the weights for the full sample > poptotals <- as.data.frame(svyby(formula = ~dummy, + by = ~class, + design = toy_design, + FUN = survey::svytotal)) > > poptotals$Freq <- poptotals$dummy > poptotals$dummy <- NULL > poptotals$se <- NULL > > # Adjust the weights of the responding sample to match to the full sample > adjusted <- postStratify(design = subset(toy_design, responded), + strata = ~class, + population = poptotals) > > # This works for the weights... > svyby(formula = ~dummy, + by = ~class, + design = adjusted, + FUN = survey::svytotal) class dummy se A A 500 0 B B 500 0 > > # ...and some, but not all, the replicate weights > stopifnot(all.equal(colSums(toy_design$repweights), colSums(adjusted$repweights))) > > > > proc.time() user system elapsed 1.09 0.07 1.17