How to understand the weights from a survey design object in R?

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 = T)
wt <- weights(nhanes)

Is wt calculated from WTMEC2YR and SDMVSTRA?
I read some examples and the source code of weights(), but they can't solve this problem.

I find that looking at the help(svydesign) example helped me to understand at least where the weight argument comes from—it's in the data object. So the weights argument here has to come from api because nothing else is in scope. After data(api) it appears that there are four objects brought into scope and apistrat is one of them and that supplies the data argument.

The other arguments must derive from apistrat because nothing by their names are yet in namespace. So, I look with apistrat() and find them. Why those particular values I have no idea; that's a domain question.

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)
# stratified sample example from help(svydesign)
dstrat<-svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc)
colnames(apistrat)
#>  [1] "cds"      "stype"    "name"     "sname"    "snum"     "dname"   
#>  [7] "dnum"     "cname"    "cnum"     "flag"     "pcttest"  "api00"   
#> [13] "api99"    "target"   "growth"   "sch.wide" "comp.imp" "both"    
#> [19] "awards"   "meals"    "ell"      "yr.rnd"   "mobility" "acs.k3"  
#> [25] "acs.46"   "acs.core" "pct.resp" "not.hsg"  "hsg"      "some.col"
#> [31] "col.grad" "grad.sch" "avg.ed"   "full"     "emer"     "enroll"  
#> [37] "api.stu"  "pw"       "fpc"

Created on 2022-12-23 by the reprex package (v2.0.1)

Thanks for your advice. I will try the same process you mentioned and see if I can find a clue.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.