[R] Sample Weights

Thomas Lumley tlumley at u.washington.edu
Mon Mar 31 18:02:52 CEST 2003


On Sun, 30 Mar 2003, Reed, Richard W wrote:

> Dear R Users--
>
> 	I am trying to apply a sample weight variable to my calculations.
> The dataset is from the Department of Labor. I believe that NORC constructed
> the sample and did the sampling. They sampled nearly 17,000 young adults.
> The sample weight variable converts the sample into the 34,000,000 in the US
> population that they represent. I imported AFQT from SPSS. When I query,
> "is.data.frame(AFQT)" the answer is TRUE.
>
> 	I am trying to apply svydesign from the Survey Package:
>
> svydesign(ids, probs=NULL, strata = NULL, variables = NULL, fpc=NULL, data =
> NULL, nest = FALSE, check.strata = !nest, weights=NULL)
>
> Below represents my effort to apply the function and the error message
> (which I don't understand).
> > AFQTs<-svydesign(id=~0,weights=AFQT$ASVABSW,data=AFQT)
> Error in apply(probs, 1, prod) : dim(X) must have a positive length

It's a bug.  It seems that weights must be a formula or dataframe, vectors
aren't allowed.

That is, you want
    AFQTs<-svydesign(id=~0, weights=~ASVABSW, data=AFQT)

This formulation works on the `fpc' example in the package
data(fpc)
svydesign(id=~0, weights=~weight, data=fpc)

You can also do
      svydesign(id=~0,weights=data.frame(AFQT$ASVABSW), data=AFQT)
In this case you wouldn't want to, but if the weight vector wasn't a
componet of the data frame it might be necessary

Thanks for pointing this out.

	-thomas



More information about the R-help mailing list