2

I am trying to use the svyttest function in a for loop in the survey package. I want to test for differences in proportions of responses between subpopulations in likert-scale type data. For example, in a survey question (1=strongly disagree, 5 = strongly agree), are there statistically significant differences in the proportion of "strongly disagree" responses between Groups 1 and 2?

I understand that I can also use the svyglm function from the survey package, but I have been unable to successfully use that in a for loop.

I also understand that there is a wtd.t.test in the weights package and the glm function in the stats package has a weights argument, but neither of these two options get the correct results. I need to use either the svyttest or the svyglm functions in the survey package.

For reference I have been looking here and here for some help but have been unable to adapt these examples to my problem.

Thank you for your time and effort.

# create example survey data

ids <- 1:1000
stratas <- rep(c("strata1", "strata2","strata3","strata4"), each=250)
weight <- rep(c(5,2,1,1), each=250)
group <- rep(c(1,2), times=500)
q1 <- sample(1:5, 1000, replace = TRUE)
  
survey_data <- data.frame(ids, stratas, weight, group, q1)

# create example svydesign

library(survey)

survey_design <- svydesign(ids = ~0, 
                           probs = NULL,
                           strata = survey_data$stratas, 
                           weights = survey_data$weight,
                           data = survey_data)

# look at the proportions of q1 responses by group

prop.table(svytable(~q1+group, design = survey_design), margin = 2)

# t-test for significant differences in the proportions of the first item in q1

svyttest(q1== 1 ~ group, design = survey_design)

# trying a for loop for all five items

for(i in c(1:5)){
  print(svyttest(q1== i ~ group, design = survey_design))
}

# I receive the following error:

Error in svyglm.survey.design(formula, design, family = gaussian()) : 
  all variables must be in design= argument

3 Answers3

2

When dynamically updating a formula inside a function or a loop you need to invoke the as.formula() function to preserve the attributes of objects as variables. This should work:

# trying a for loop for all five items
for(i in c(1:5)){
  print(svyttest(as.formula(paste("q1==", i, "~group")), 
                 design = survey_design))
}
Sicabí
  • 133
  • 2
  • 8
0

I tried some trick, you can use array, which you can use for your loop:

x=c()

for(i in c(1:5)){
  x=append(x,as.formula(paste("q1==",i,"~ group")))
  print(svyttest(x[[i]], design = survey_design))
}

With regards

Aleksei

0

I would use bquote

for(i in 1:5){
   print(eval(
      bquote(svyttest(q1== .(i) ~ group, design = survey_design))
    ))
}

In this example as.formula works just as well, but bquote is more general.

Thomas Lumley
  • 1,893
  • 5
  • 8