Svytable ignoring recoding of variables

I'm having problems with svytable not recognizing or ignoring some recoding I've done. This is for a shiny app, and I have the same code with the same data in another app and am having no problems with it, but any other version I try, even with the exact same dataset with a different name, it doesn't work. It seems to be ignoring the names and the responses that I combined. Am I missing something? Thanks for any help you might be able to provide!

SexualOrientation<-c(1, 2, 1, 1, 1, 3, 4, 2, 1, 2, 2, 3, 4, 2, 2, 3, 3, 4, 4, 1, 1, 2)
q121<-c(1, 1, 1, 2, 3, 4, 1, 1, 1, 1, 2, 2, 2, 1, 2, 1, 1, 2, 1, 1, 1, 1)
q122<-c(2, 1, 1, 2, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 2, 1, 2, 2, 2)
weight<-c(10.9384, 16.68, 16.68, 26.6481, 11.7255, 10.9384, 10.9384, 10.9384, 16.68, 11.7255, 8.5349, 10.9384, 16.68, 16.68, 16.68, 16.68, 10.9384, 14.2632, 11.7255, 12.57901, 19.182, 9.8151)
stratum<-c(23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23)
psu<-c(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1)

d<-data.frame(SexualOrientation,q121,q122,weight,stratum,psu)

d$homeless <- ifelse(d$q121>1, 1, 0)
d$unaccompanied <- ifelse(d$q122==1, 1, 0)
d$homelessgroups[d$homeless==1 & d$unaccompanied==0] <- "Homeless with Family"
d$homelessgroups[d$homeless==0 & d$unaccompanied==1] <- "Homeless, Unaccompanied"
d$homelessgroups[d$homeless==1 & d$unaccompanied==1] <- "Both Homeless"
d$homelessgroups[d$homeless==0 & d$unaccompanied==0] <- "Housed"
d$homelessgroups <- ordered(d$homelessgroups, levels = c("Housed", "Homeless with Family", "Homeless, Unaccompanied", "Both Homeless"))
d$allhomeless <- ifelse(d$homelessgroups=="Housed", "Housed", "Homeless")

d_design <- svydesign(id=~psu, weight=~weight, strata=~stratum, nest = T, data=d)

d$SexualOrientation[d$SexualOrientation==1] <- "Heterosexual"
d$SexualOrientation[d$SexualOrientation==2 | d$SexualOrientation==3] <- "Gay or Lesbian or Bisexual"
d$SexualOrientation[d$SexualOrientation==4] <- "Not Sure"
d$SexualOrientation <- ordered(d$SexualOrientation, levels = c("Heterosexual", "Gay or Lesbian or Bisexual", "Not Sure"))
soweight <- as.data.frame(svytable(~SexualOrientation + allhomeless, design = d_design))
soweight <- rename(soweight, Var1 = SexualOrientation, Var2 = allhomeless, value = Freq)
soweight$value <- round(soweight$value, 0)

This is the output I'm getting (with the sample data above)
31%20PM

And this is the output I want/achieved with the same code elsewhere (and the real data).
45%20PM

Hi @rachelbbbbb, it looks like it's displaying your factor levels instead of the data, maybe setting the stringsAsFactors argument in the line where you create the initial data.frame (d) will force the actual names to be used

Oops, forgot to mention to set stringsAsFactors to FALSE

That doesn't seem to change anything, but thank you for the suggestion!

Darn it. What happens if you run it with out the highlighted line?

That worked with this example but does not work when I try it with my actual data.

Interesting. Is there an error, or does it simply revert back to factor numbers?

No error, just reverts back to factor numbers and factor levels.

If the real data is using the same fields, could it be that the SexualOrientation var has some additional values?

What do you mean by additional values? Also, it doesn't seem to be working for the sample data anymore.

@edgararuiz I finally figured it out! It seems the survey design has to be run after the variables have been recoded. I probably should have figured this out sooner but thank you for trying to help!

1 Like

All svy* summary functions use data defined in svydesign(). Subsequent recodes should be done on the design object itself, or else update the design object after making changes to your original dataset. See http://r-survey.r-forge.r-project.org/survey/html/update.survey.design.html

1 Like