I have survey data that needs to be weighted, and to help me with this task, I have access to the full joint distributions of the variables I want to use. As I understand it, I should use the postStratify() function in the survey package for this task, but I must admit that I am quite lost when it comes to the required syntax.

I have tried to use marginal distributions and the rake() function, this is quite straight-forward: http://www.r-bloggers.com/survey-computing-your-own-post-stratification-weights-in-r/

But how do I incorporate all my joint distributions into the postStratify() function? The manual only lists a simple example and is a bit vague, to me at least. I have full joint distributions for 5 different variables.

Moreover, if want to use my weighted result with a function that does not take weight as an input, how should I go about and "duplicate" my rows in the final data? When I tried the rake() function I could only trim weights to an interval with a min value of 0.87. I was thinking I could round all weights to integers.

  • $\begingroup$ some syntax examples github.com/ajdamico/usgsd/… $\endgroup$ Commented Mar 10, 2015 at 1:07
  • $\begingroup$ Please demonstrate your syntax. $\endgroup$
    – StasK
    Commented Aug 11, 2016 at 13:00
  • $\begingroup$ I just answered a similar question on postStratify() syntax here. $\endgroup$
    – StasK
    Commented Aug 15, 2016 at 18:50

2 Answers 2


I made an example for using post-stratification as below. I find that in the 'strata' argument, if specify as a formula with two or more variables, it implies that the post-stratification is done for their joint distribution. (Initially, I thought I need to add an interaction term in the formula, but it turned out to be unnecessary!)



# original design
# suppose 'pw' is sampling weight
dclus1<-svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)

### post-stratify on only one categorical variable ###
# on school type
pop.types <- xtabs(~stype, data=apipop)
dclus1p<-postStratify(dclus1, ~stype, pop.types)

# compare sampling weights with post-stratified weights
summary( attr(dclus1p$postStrata[[1]], "oldweights" ) )
summary( attr(dclus1p$postStrata[[1]], "weights" ) )

### post-stratify on joint distribution of two categorical variables ###
# school type and sch.wide

apiclus1$stype.sch.wide <- interaction( apiclus1$stype, apiclus1$sch.wide )
apipop$stype.sch.wide <- interaction( apipop$stype, apipop$sch.wide )
pop.types <- xtabs(~stype.sch.wide, data=apipop)

dclus1<-svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)

# first method
dclus2p<-postStratify(dclus1, ~stype.sch.wide, pop.types)

# second method
pop.types <- xtabs(~ stype + sch.wide, data=apipop )
dclus3p<-postStratify(dclus1, ~stype + sch.wide, pop.types)

If you are having syntax trouble calculating post stratification weights and trimming I can recommend using www.spinnakerresearch.nl. No syntax needed. I believe you can trim max weights. However rounding weights to integers does not make sense as you will need to weight some cases down. Meaning a weight factor between 0 and 1.

  • 1
    $\begingroup$ Hi, welcome to CrossValidated. Please note that you must reveal any affiliation to products you recommend, each time that you do so. Please see this section of the help center, in particular the section under "Avoid overt self-promotion". You should also make certain when you do so that you clearly address the specific issues raised in the question in your answer. $\endgroup$
    – Glen_b
    Commented Jun 27, 2015 at 2:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.