简体   繁体   中英

svyglm - how to code for a logistic regression model across all variables?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM