Data set up
library(survey)
library(gtsummary)
#Load data
data(api)
#Declare survey design
dclus1<-svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)
I would like to construct a function that takes in an independent variable and produces a gt summary table with the results.
For example; I want to run a linear model to predict having "awards" based on having "both" parents in the household. I tried this:
mod_create <- function(y){
model <- svyglm(y~both, design = dclus1, family = quasibinomial())
tbl <- tbl_regression(model, exponentiate = TRUE) %>% add_global_p()
return(tbl)
}
mod_create(awards)
I get this error:
Error in svyglm.survey.design(y ~ awards, design = dclus1, family = quasibinomial()) :
all variables must be in design= argument
4.stop("all variables must be in design= argument")
3.svyglm.survey.design(y ~ awards, design = dclus1, family = quasibinomial())
2.svyglm(y ~ awards, design = dclus1, family = quasibinomial())
1.mod_create(both)
CodePudding user response:
You should be able to use reformulate
here:
library(survey)
library(dplyr)
library(gtsummary)
data(api)
dclus1 <- svydesign(id = ~ dnum, weights = ~ pw, data = apiclus1, fpc = ~ fpc)
mod_create <- function(y){
model <- svyglm(reformulate(paste0(y, " ~ both")),
design = dclus1, family = quasibinomial())
tbl <- tbl_regression(model, exponentiate = TRUE) %>% add_global_p()
return(tbl)
}
mod_create("awards") # note the ""