[R] sampling design runs with no errors but returns empty data set
Thomas Lumley
tlumley at uw.edu
Wed Mar 30 22:01:44 CEST 2011
On Thu, Mar 31, 2011 at 4:01 AM, Simon Kiss <sjkiss at gmail.com> wrote:
> Dear colleagues,
> I'm working with the 2008 Canada Election Studies (http://www.queensu.ca/cora/_files/_CES/CES2008.sav.zip), trying to construct a weighted national sample using the survey package.
> Three weights are included in the national survey (a household weight, a provincial weight and a national weight which is a product of the first two).
> In the following code I removed variables with missing national weights and tried to construct the sample from advice I've gleaned from the documentation for the survey package and other help requests.
> There are no errors, but the data frame (weight_test) contains no
> What am I missing?
> Yours, Simon Kiss
> P.S. The code is only reproducible if the data set is downloadable. I'm nt sure
>
> ces<-read.spss(file.choose(), to.data.frame=TRUE, use.value.labels=FALSE)
> missing_data<-subset(ces1, !is.na(ces08_NATWGT))
> weight_test<-svydesign(id=~0, weights=~ces08_NATWGT, data=missing_data)
>
The code isn't reproducible even with the data. The code refers to a
data frame ces1, which isn't defined, and to a variable ces08_NATWGT
that isn't in the data set.
However, a bit of Googling suggests that the variable CES08_NA is
probably the one you mean, giving the following code
library(survey)
library(foreign)
ces<-read.spss("CES2008.sav", to.data.frame=TRUE, use.value.labels=FALSE)
missing_data<-subset(ces, !is.na(CES08_NA))
weight_test<-svydesign(id=~0, weights=~CES08_NA, data=missing_data)
which seems to produce a perfectly reasonable survey design object.
> weight_test
Independent Sampling design (with replacement)
svydesign(id = ~0, weights = ~CES08_NA, data = missing_data)
> dim(weight_test)
[1] 3257 531
> svymean(~factor(GENDER),weight_test)
mean SE
factor(GENDER)1 0.47362 0.01
factor(GENDER)5 0.52638 0.01
Since you don't say how you concluded the object contained no, I don't
know what you were seeing. Note that weight_test is not supposed to
be a data frame. It's a survey design object.
-thomas
--
Thomas Lumley
Professor of Biostatistics
University of Auckland
More information about the R-help
mailing list