R Under development (unstable) (2026-02-23 r89457 ucrt) -- "Unsuffered Consequences" Copyright (C) 2026 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 > > data(api) > dclus1 <- svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc) > rclus1 <- as.svrepdesign(dclus1) > > ## population marginal totals for each stratum > pop.types <- data.frame(stype=c("E","H","M"), Freq=c(4421,755,1018)) > pop.schwide <- data.frame(sch.wide=c("No","Yes"), Freq=c(1072,5122)) > > rclus1r <- rake(rclus1, list(~stype,~sch.wide), list(pop.types, pop.schwide), + control=list(epsilon=1e-10,maxit=30)) > > svymean(~api00, rclus1r) mean SE api00 641.23 26.874 > svytotal(~enroll, rclus1r) total SE enroll 3647280 463582 > > ff<-~stype+sch.wide > poptotals<-colSums(model.matrix(ff,model.frame(ff,apipop))) > rclus1g<-calibrate(rclus1, ~stype+sch.wide, poptotals,calfun="raking",tol=1e-10) > > svymean(~api00,rclus1g) mean SE api00 641.23 26.874 > svytotal(~enroll,rclus1g) total SE enroll 3647280 463582 > > all.equal(as.vector(weights(rclus1g)/weights(rclus1r)),rep(1,183)) [1] "Numeric: lengths (2745, 183) differ" > > ## Do it for a design without replicate weights > dclus1r<-rake(dclus1, list(~stype,~sch.wide), list(pop.types, pop.schwide), + control=list(epsilon=1e-10,maxit=30)) > > svymean(~api00, dclus1r) mean SE api00 641.23 23.704 > svytotal(~enroll, dclus1r) total SE enroll 3647280 400603 > > dclus1g<-calibrate(dclus1, ~stype+sch.wide, poptotals,calfun="raking",tol=1e-10) > > svymean(~api00,dclus1g) mean SE api00 641.23 23.704 > svytotal(~enroll,dclus1g) total SE enroll 3647280 400603 > > all.equal(as.vector(weights(dclus1g)/weights(dclus1r)),rep(1,183)) [1] TRUE > > > ## Example of raking with partial joint distributions > pop.table <- xtabs(~stype+sch.wide,apipop) > pop.imp<-data.frame(comp.imp=c("No","Yes"),Freq=c(1712,4482)) > dclus1r2<-rake(dclus1, list(~stype+sch.wide, ~comp.imp), + list(pop.table, pop.imp), + control=list(epsilon=1e-10,maxit=30)) Warning message: In rake(dclus1, list(~stype + sch.wide, ~comp.imp), list(pop.table, : Raking did not converge after 30 iterations. > svymean(~api00, dclus1r2) mean SE api00 642.61 22.731 > > ff1 <-~stype*sch.wide+comp.imp > > poptotals1<-colSums(model.matrix(ff1,model.frame(ff1,apipop))) > dclus1g2<-calibrate(dclus1, ~stype*sch.wide+comp.imp, poptotals1, calfun="raking",tol=1e-10) > > svymean(~api00, dclus1g2) mean SE api00 642.61 22.731 > > all.equal(as.vector(weights(dclus1g2)/weights(dclus1r2)),rep(1,183)) [1] TRUE > > proc.time() user system elapsed 1.28 0.15 1.43