From f4be547ed071dcd41b1e3c83009d959a12ddc184 Mon Sep 17 00:00:00 2001 From: Andreas Gammelgaard Damsbo Date: Fri, 15 Nov 2024 21:57:38 +0100 Subject: [PATCH] handling empty formula --- R/regression_model.R | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/R/regression_model.R b/R/regression_model.R index 602f782..bc718b8 100644 --- a/R/regression_model.R +++ b/R/regression_model.R @@ -35,7 +35,7 @@ regression_model <- function(data, args.list = NULL, fun = NULL, vars = NULL) { - if (!is.null(formula.str)) { + if (!is.null(formula.str) | formula.str != "") { formula.str <- glue::glue(formula.str) } else { assertthat::assert_that(outcome.str %in% names(data), @@ -44,10 +44,10 @@ regression_model <- function(data, formula.str <- glue::glue("{outcome.str}~.") if (!is.null(vars)) { - if (outcome.str %in% vars){ + if (outcome.str %in% vars) { vars <- vars[vars %in% outcome.str] } - data <- data |> dplyr::select(dplyr::all_of(c(vars,outcome.str))) + data <- data |> dplyr::select(dplyr::all_of(c(vars, outcome.str))) } } @@ -57,16 +57,15 @@ regression_model <- function(data, # browser() if (auto.mode) { - if (is.numeric(data[[outcome.str]])){ + if (is.numeric(data[[outcome.str]])) { fun <- "stats::lm" - } else if (is.factor(data[[outcome.str]])){ - if (length(levels(data[[outcome.str]]))==2){ + } else if (is.factor(data[[outcome.str]])) { + if (length(levels(data[[outcome.str]])) == 2) { fun <- "stats::glm" - args.list = list(family = binomial(link = "logit")) - - } else if (length(levels(data[[outcome.str]]))>2){ + args.list <- list(family = binomial(link = "logit")) + } else if (length(levels(data[[outcome.str]])) > 2) { fun <- "MASS::polr" - args.list = list( + args.list <- list( Hess = TRUE, method = "logistic" ) @@ -76,7 +75,6 @@ regression_model <- function(data, } else { stop("Output variable should be either numeric or factor for auto.mode") } - } assertthat::assert_that("character" %in% class(fun),