# Specifying the survey design correctly in the R survey package

I have the results from a survey where a geographic area was stratified and power allocation (using the number of households in each stratum) was used to determine the number of surveys to send out to each stratum.

From each household that returned a survey (which has a unique survey ID), I have the weight results from 1 or more people in the household. Thus in some instances a single survey from a single household has 5 responses to the weight question. I convert the weight from each response into one of four weight classes.

I want to obtain a point estimate for the proportion of the population who falls into each weight class along with the associated confidence interval, based on the age group of the respondent (Child versus Adult). To do so, I have placed the data from the surveys into a data.frame with the following columns:

• SurveyID: the ID of the survey that the response came from
• Stratum: identifies the Stratum that the survey was returned from
• PostStratWeights: the post-stratified weight that accounts for survey non-response
• AgeGroup: the age group that the response belongs to (Child/Adult)
• WeightClass: the weight class of the respondent (one of four classes)

where each row in the data.frame corresponds to a single response from a survey (so multiple rows will have the same SurveyID if there was more than one weight response from a single survey).

I have specified the survey design as

mydesign = svydesign(ids=~SurveyID,
strata=~Stratum,
weights=~PostStratWeights,
data=survey_response_data)


Do I need to add in fpc for this survey design? I know both the estimated number of households in each stratum and the estimated number of people in each stratum.

Under the assumption that the survey design was correct, I then tried to compute the mean WeightInLbs as follows:

svyby(formula=~WeightClass,
by=~AgeGroup+Stratum,
FUN=svymean,
design=mydesign,
keep.names=FALSE)



However, running this produces the following error:

Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels


So I suspect something is wrong with the way I have specified my survey design but don't quite know what.

• Those look correct, so I think we need some information about your data May 10 at 1:13
• What information would you like about the data? I will edit the information into the original post. May 10 at 1:26
• I have edited the post because I actually break the WeightInLbs into four classes, and so I am using the WeightClass column (a categorical variable) instead of the WeightInLbs column, a continuous variable. I want to get point estimates for the proportion of the population in each weight class. Sorry for having the wrong question in there initially. May 10 at 1:34

Ok. It sounds as if svymean() is converting ~WeightInLbs to a factor variable (maybe it starts as character?), which will happen separately for each combination of the by variables, and that some combination of the by variables has only one weight category present.
If this is what's happening, you can create the factor variable in advance using update
mydesign<-update(mydesign, WeightClassFactor = factor(WeightClass))