0

In R using GLM to include all variables you can simply use a . as shown How to succinctly write a formula with many variables from a data frame?

for example:

y <- c(1,4,6)
d <- data.frame(y = y, x1 = c(4,-1,3), x2 = c(3,9,8), x3 = c(4,-4,-2))
mod <- lm(y ~ ., data = d)

however I am struggling to do this with svydesign. I have many exploratory variables and an ID and weight variable, so first I create my survey design:

des <-svydesign(ids=~id, weights=~wt, data = df)

Then I try creating my binomial model using weights:

binom <- svyglm(y~.,design = des, family="binomial")

But I get the error:

Error in svyglm.survey.design(y ~ ., design = des, family = "binomial") : 
  all variables must be in design = argument

What am I doing wrong?

user19250276
  • 101
  • 5
  • As far as I can tell from the source of the `svyglm.survey.design` method, using `.` in place of all other variables in the data set is not supported here – shs Jul 30 '22 at 20:12
  • Is the only alternative to then enter them manually? – user19250276 Aug 01 '22 at 09:15
  • If your concern is copy and pasting, you can also construct formulas from strings – shs Aug 01 '22 at 10:54

1 Answers1

2

You typically wouldn't want to do this, because "all the variables" would include design metadata such as weights, cluster indicators, stratum indicators, etc

You can use col.names to extract all the variable names from a design object and then reformulate, probably after subsetting the names, eg with the api example in the package

> all_the_names <- colnames(dclus1)
> all_the_actual_variables <- all_the_names[c(2, 11:37)]
> reformulate(all_the_actual_variables,"y")
y ~ stype + pcttest + api00 + api99 + target + growth + sch.wide + 
    comp.imp + both + awards + meals + ell + yr.rnd + mobility + 
    acs.k3 + acs.46 + acs.core + pct.resp + not.hsg + hsg + some.col + 
    col.grad + grad.sch + avg.ed + full + emer + enroll + api.stu
Thomas Lumley
  • 1,893
  • 5
  • 8
  • Thank you! I was wondering why it wasn't possible. Thank you also for your work in producing and maintaining the package. – user19250276 Aug 05 '22 at 09:48