-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tbl_uvregression()
to accept survey objects
#740
Changes from 4 commits
96b4956
96f2856
c07fcae
fb1ceaf
e8b8361
953fab4
c34481d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
#' | ||
#' @param data Data frame to be used in univariate regression modeling. Data | ||
#' frame includes the outcome variable(s) and the independent variables. | ||
#' Survey design is also accepted. | ||
#' @param method Regression method (e.g. [lm], [glm], [survival::coxph], and more). | ||
#' @param y Model outcome (e.g. `y = recurrence` or `y = Surv(time, recur)`). | ||
#' All other column in `data` will be regressed on `y`. | ||
|
@@ -106,6 +107,12 @@ tbl_uvregression <- function(data, method, y = NULL, x = NULL, method.args = NUL | |
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a third example in the examples using a survey object |
||
} | ||
|
||
# checking input ------------------------------------------------------------- | ||
# data is a data frame | ||
if (!is.data.frame(data) && !is_survey(data)) { | ||
stop("`data` argument must be a data frame or survey object.", call. = FALSE) | ||
} | ||
|
||
# setting defaults ----------------------------------------------------------- | ||
pvalue_fun <- | ||
pvalue_fun %||% | ||
|
@@ -138,14 +145,18 @@ tbl_uvregression <- function(data, method, y = NULL, x = NULL, method.args = NUL | |
y <- rlang::enexpr(y) | ||
x <- | ||
tryCatch({ | ||
.select_to_varnames(select = !!x, data = data, arg_name = "x") | ||
.select_to_varnames(select = !!x, | ||
data = switch(is.data.frame(data), data) %||% .remove_survey_cols(data), | ||
arg_name = "x") | ||
}, error = function(e) { | ||
rlang::expr_text(x) | ||
}) | ||
|
||
y <- | ||
tryCatch({ | ||
.select_to_varnames(select = !!y, data = data, arg_name = "y") | ||
.select_to_varnames(select = !!y, | ||
data = switch(is.data.frame(data), data) %||% .remove_survey_cols(data), | ||
arg_name = "y") | ||
}, error = function(e) { | ||
rlang::expr_text(y) | ||
}) | ||
|
@@ -163,19 +174,19 @@ tbl_uvregression <- function(data, method, y = NULL, x = NULL, method.args = NUL | |
include <- | ||
.select_to_varnames( | ||
select = {{ include }}, | ||
data = data, | ||
data = switch(is.data.frame(data), data) %||% .remove_survey_cols(data), | ||
arg_name = "include" | ||
) | ||
exclude <- | ||
.select_to_varnames( | ||
select = {{ exclude }}, | ||
data = data, | ||
data = switch(is.data.frame(data), data) %||% .remove_survey_cols(data), | ||
arg_name = "exclude" | ||
) | ||
show_single_row <- | ||
.select_to_varnames( | ||
select = {{ show_single_row }}, | ||
data = data, | ||
data = switch(is.data.frame(data), data) %||% .remove_survey_cols(data), | ||
arg_name = "show_single_row" | ||
) | ||
|
||
|
@@ -205,7 +216,7 @@ tbl_uvregression <- function(data, method, y = NULL, x = NULL, method.args = NUL | |
label <- | ||
.formula_list_to_named_list( | ||
x = label, | ||
data = data, | ||
data = switch(is.data.frame(data), data) %||% .remove_survey_cols(data), | ||
arg_name = "label" | ||
) | ||
|
||
|
@@ -214,12 +225,6 @@ tbl_uvregression <- function(data, method, y = NULL, x = NULL, method.args = NUL | |
stop("Each `label` specified must be a string of length 1.", call. = FALSE) | ||
} | ||
|
||
# data ----------------------------------------------------------------------- | ||
# data is a data frame | ||
if (!is.data.frame(data)) { | ||
stop("`data` argument must be a data frame.", call. = FALSE) | ||
} | ||
|
||
# will return call, and all object passed to in table1 call | ||
# the object func_inputs is a list of every object passed to the function | ||
tbl_uvregression_inputs <- as.list(environment()) | ||
|
@@ -229,7 +234,7 @@ tbl_uvregression <- function(data, method, y = NULL, x = NULL, method.args = NUL | |
|
||
# get all vars not specified ------------------------------------------------- | ||
all_vars <- | ||
names(data) %>% | ||
names(switch(is.data.frame(data), data) %||% .remove_survey_cols(data)) %>% | ||
# removing x or y variable | ||
setdiff(paste(c(y, x), "~ 1") %>% stats::as.formula() %>% all.vars()) %>% | ||
# removing any other variables listed in the formula | ||
|
@@ -263,10 +268,17 @@ tbl_uvregression <- function(data, method, y = NULL, x = NULL, method.args = NUL | |
formula_chr = glue(formula), | ||
model = map( | ||
.data$formula_chr, | ||
~list(method, formula = as.formula(.x), data = data) %>% | ||
function(.x) { | ||
call_list1 <- # defining formula and data call (or formula and design) | ||
switch(is.data.frame(data), | ||
list(method, formula = as.formula(.x), data = data)) %||% | ||
list(method, formula = as.formula(.x), design = data) | ||
|
||
call_list1 %>% | ||
c(as.list(method.args)[-1]) %>% | ||
as.call() %>% | ||
eval() | ||
} | ||
), | ||
# removing backticks | ||
y = switch(is.null(.env$y), all_vars) %||% y, | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add [survey::svyglm] in the list of possibilities ?