Given a function or expression computing a statistic based on sampling weights, withReplicates evaluates the statistic and produces a replicate-based estimate of variance. produces the variance estimate from a set of replicates and the design object.

withReplicates(design, theta,..., return.replicates=FALSE)
# S3 method for
withReplicates(design, theta, rho = NULL, ..., 
     scale.weights=FALSE, return.replicates=FALSE)
# S3 method for svrepvar
withReplicates(design, theta,  ...,  return.replicates=FALSE)
# S3 method for svrepstat
withReplicates(design, theta,  ...,  return.replicates=FALSE)
# S3 method for svyimputationList
withReplicates(design, theta,  ...,  return.replicates=FALSE)
# S3 method for
vcov(object, replicates, centre,...)



A survey design with replicate weights (eg from svrepdesign) or a suitable object with replicate parameter estimates


A function or expression: see Details below


If design uses BRR weights, rho optionally specifies the parameter for Fay's variance estimator.


Other arguments to theta


Divide the probability weights by their sum (can help with overflow problems)


Return the replicate estimates as well as the variance?


The replicate-weights design object used to create the replicates


A set of replicates


The centering value for variance calculation. If object$mse is TRUE this is the result of estimation using the sampling weights, and must be supplied. If object$mse is FALSE the mean of the replicates is used and this argument is silently ignored.


The method for objects evaluates a function or expression using the sampling weights and then each set of replicate weights. The method for svrepvar objects evaluates the function or expression on an estimated population covariance matrix and its replicates, to simplify multivariate statistics such as structural equation models.

For the method, if theta is a function its first argument will be a vector of weights and the second argument will be a data frame containing the variables from the design object. If it is an expression, the sampling weights will be available as the variable .weights. Variables in the design object will also be in scope. It is possible to use global variables in the expression, but unwise, as they may be masked by local variables inside withReplicates.

For the svrepvar method a function will get the covariance matrix as its first argument, and an expression will be evaluated with .replicate set to the variance matrix.

For the svrepstat method a function will get the point estimate, and an expression will be evaluated with .replicate set to each replicate. The method can only be used when the svrepstat object includes replicates.

The svyimputationList method runs withReplicates on each imputed design (which must be replicate-weight designs).


If return.replicates=FALSE, the weighted statistic, with the variance matrix as the "var" attribute. If

return.replicates=TRUE, a list with elements theta for the usual return value and replicates for the replicates.


repweights<-2*cbind(c(1,0,1,0,1,0), c(1,0,0,1,0,1), c(0,1,1,0,0,1),
scdrep<-svrepdesign(data=scd, type="BRR", repweights=repweights)
#> Warning: No sampling weights provided: equal probability assumed

a<-svyratio(~alive, ~arrests, design=scdrep)
#>         arrests
#> alive 0.1535064
#>              [,1]
#> [1,] 8.870627e-05
withReplicates(scdrep, quote(sum(.weights*alive)/sum(.weights*arrests)))
#>        theta     SE
#> [1,] 0.15351 0.0094
withReplicates(scdrep, function(w,data)
#>        theta     SE
#> [1,] 0.15351 0.0094

dclus1<-svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)
withReplicates(varmat, quote( factanal(covmat=.replicate, factors=2)$unique) )
#>             theta     SE
#> api00    0.005000 0.0651
#> api99    0.040393 0.0316
#> ell      0.452180 0.6865
#> meals    0.181142 0.0569
#> hsg      0.995913 0.0409
#> mobility 0.930017 0.0488

nhanesdesign <- svydesign(id=~SDMVPSU, strata=~SDMVSTRA, weights=~WTMEC2YR, nest=TRUE,data=nhanes)
logistic <- svyglm(HI_CHOL~race+agecat+RIAGENDR, design=as.svrepdesign(nhanesdesign),
family=quasibinomial, return.replicates=TRUE)
fitted<-predict(logistic, return.replicates=TRUE, type="response")
sensitivity<-function(pred,actual) mean(pred>0.1 & actual)/mean(actual)
withReplicates(fitted, sensitivity, actual=logistic$y)
#>        theta     SE
#> [1,] 0.77891 0.0246

if (FALSE) {
## one-stage cluster sample
dclus1<-svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)
## convert to bootstrap
bclus1<-as.svrepdesign(dclus1,type="bootstrap", replicates=100)

## median regression
withReplicates(bclus1, quote(coef(rq(api00~api99, tau=0.5, weights=.weights))))

## pearson correlation
dstrat <- svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc)
bstrat<- as.svrepdesign(dstrat,type="subbootstrap")

v <- svyvar(~api00+api99, bstrat, return.replicates=TRUE)
correps<-apply(vreps,1, function(v) v[2]/sqrt(v[1]*v[4]))

vcov(bstrat,correps, centre=vcor)
#> [1] 1.73213e-05
#> attr(,"means")
#> [1] 0.9754495