I often use the survey package to deal with the complex survey data in R. I know using the function weights() can extract the weights from the survey design object. I want to understand the process behind this function. Does it consider the design of this complex survey?

For example, I use the nhanes dataset.

nhanes <- svydesign(id = ~SDMVPSU, weights = ~WTMEC2YR, 
                    strata = ~ SDMVSTRA, data = nhanes_full, 
                    nest = TRUE)
wt <- weights(nhanes)

Is wt calculated from WTMEC2YR and SDMVSTRA?


1 Answer 1


TL;DR: No it just returns the individual weights.

The rationale for weights is to make sample observations representative of the population. Weights are usually individual or person-weights (aka p-weights) that care for the distribution in the population (e.g. age, sex). PSU-IDs denote primary sampling units, usually households, where observations are expected to be more similar within these PSUs than between. Finally, PSUs might be classified in strata of specific characteristics, e.g. specific region, migration. Functions like R's svydesign (or similarly Stata's svyset) bring all this information together in a survey.design object which can be used for regression in svyglm(..., design=<survey.design>).

The survey:::weights.survey.design method, however, calculates the inverse of the probability of being included in the sample, previously calculated by svydesign,

$$ w_i = \frac{1}{P_i}$$

so in the end, it just returns the original weights specified in the svydesign(., weights=) argument. Thus, information of PSU, and strata will not be used.

data('NHANESraw', package='NHANES')
nhanes <- svydesign(id=~SDMVPSU, weights=~WTMEC2YR, strata=~SDMVSTRA,
                    data=NHANESraw, nest=TRUE)

all.equal(unname(weights(nhanes)), NHANESraw$WTMEC2YR)
# [1] TRUE

Data available:

data('NHANESraw', package='NHANES')
  • $\begingroup$ Thanks for your advice! Is there any method to extract the exact weights including the information of weights, PSU, and strata? I want to use the mediation package. $\endgroup$
    – you lin
    Commented Dec 23, 2022 at 14:04
  • $\begingroup$ @youlin There might be a misconception on what svydesign does. Instead of doing just kind of a conversion, it also takes up interactions of PSU and strata. This information gets stored in the survey.design object which is used in e.g. svyglm in place of the usual data object. I doubt that what you intend will be successful. $\endgroup$
    – jay.sf
    Commented Dec 23, 2022 at 14:21
  • $\begingroup$ Thanks for your explanation. To use this information stored in the survey.design object as you mentioned, I must use the functions in survey package like svyglm. It can't be extracted like weights. Is this understanding correct? $\endgroup$
    – you lin
    Commented Dec 23, 2022 at 14:32
  • $\begingroup$ @youlin Yes, that's what I'm saying. You may look into the str(nhanes) on how the information is stored in the object (which is what functions like survey:::svyglm.survey.design have available then). $\endgroup$
    – jay.sf
    Commented Dec 23, 2022 at 14:43
  • $\begingroup$ I see. Thanks for your explanation! $\endgroup$
    – you lin
    Commented Dec 23, 2022 at 15:00

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.