Skip to content

Commit

Permalink
fix styling issues
Browse files Browse the repository at this point in the history
  • Loading branch information
IndrajeetPatil committed Apr 27, 2024
1 parent 64dd6ee commit ee7b9ad
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
4 changes: 0 additions & 4 deletions R/estimate_means.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@ estimate_means <- function(model,
ci = 0.95,
backend = "emmeans",
...) {

if (backend == "emmeans") {
# Emmeans ------------------------------------------------------------------
estimated <- get_emmeans(model, at, fixed, transform = transform, ...)
means <- .format_emmeans_means(estimated, model, ci, transform, ...)

} else {
# Marginalmeans ------------------------------------------------------------
estimated <- .get_marginalmeans(model, at, ci = ci, ...)
Expand Down Expand Up @@ -112,5 +110,3 @@ estimate_means <- function(model,
if (all(table_footer == "")) table_footer <- NULL

Check warning on line 110 in R/estimate_means.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/estimate_means.R,line=110,col=11,[nzchar_linter] Use !nzchar(x) instead of x == "". Note that unlike nzchar(), EQ coerces to character, so you'll have to use as.character() if x is a factor. Whenever missing data is possible, please take care to use nzchar(., keepNA = TRUE); nzchar(NA) is TRUE by default.
c(table_footer, "blue")
}


2 changes: 1 addition & 1 deletion R/get_emmeans.R
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ model_emmeans <- get_emmeans
for (var_at in names(args$emmeans_at)) {
term <- terms[grepl(var_at, terms, fixed = TRUE)]
if (any(grepl(paste0("as.factor(", var_at, ")"), term, fixed = TRUE)) ||
any(grepl(paste0("as.character(", var_at, ")"), term, fixed = TRUE))) {
any(grepl(paste0("as.character(", var_at, ")"), term, fixed = TRUE))) {
args$retransform[[var_at]] <- args$emmeans_at[[var_at]]
args$emmeans_at[[var_at]] <- as.numeric(as.character(args$emmeans_at[[var_at]]))
}
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test-estimate_means.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ test_that("estimate_means() - core", {
model <- lm(vs ~ cyl, data = dat)
estim1 <- suppressMessages(estimate_means(model))
expect_equal(dim(estim1), c(3, 5))

Check warning on line 16 in tests/testthat/test-estimate_means.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-estimate_means.R,line=16,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.
estim2 <- suppressMessages(estimate_means(model, backend="marginaleffects"))
estim2 <- suppressMessages(estimate_means(model, backend = "marginaleffects"))
expect_equal(dim(estim2), c(3, 5))
expect_true(max(estim1$Mean - estim2$Mean) < 1e-10)

# Interaction (factor * continuous)
model <- lm(mpg ~ wt * gear, data = dat)
estim1 <- suppressMessages(estimate_means(model))
expect_equal(dim(estim1), c(3, 5))
estim2 <- suppressMessages(estimate_means(model, backend="marginaleffects"))
estim2 <- suppressMessages(estimate_means(model, backend = "marginaleffects"))
expect_equal(dim(estim2), c(3, 6))
expect_true(max(estim1$Mean - estim2$Mean) < 1e-10)

# At specific levels
model <- lm(Sepal.Width ~ Species, data = iris)
estim1 <- suppressMessages(estimate_means(model, at = "Species=c('versicolor', 'virginica')"))
expect_equal(dim(estim1), c(2, 5))
estim2 <- suppressMessages(estimate_means(model, at = "Species=c('versicolor', 'virginica')", backend="marginaleffects"))
estim2 <- suppressMessages(estimate_means(model, at = "Species=c('versicolor', 'virginica')", backend = "marginaleffects"))
expect_equal(dim(estim2), c(2, 5))
expect_true(max(estim1$Mean - estim2$Mean) < 1e-10)

Expand All @@ -42,22 +42,22 @@ test_that("estimate_means() - core", {
model <- lm(Sepal.Width ~ Species * Petal.Length_factor, data = dat)
estim1 <- suppressMessages(estimate_means(model, at = "all"))
expect_equal(dim(estim1), c(6, 6))
estim2 <- suppressWarnings(suppressMessages(estimate_means(model, at = "all", backend="marginaleffects")))
estim2 <- suppressWarnings(suppressMessages(estimate_means(model, at = "all", backend = "marginaleffects")))
expect_equal(dim(estim2), c(6, 6))

# No interaction (two factors)
model <- lm(Petal.Length ~ Sepal.Width + Species, data = iris)
estim1 <- suppressMessages(estimate_means(model))
expect_equal(dim(estim1), c(3, 5))
estim2 <- suppressMessages(estimate_means(model, backend="marginaleffects"))
estim2 <- suppressMessages(estimate_means(model, backend = "marginaleffects"))
expect_equal(dim(estim2), c(3, 6))
expect_true(max(estim1$Mean - estim2$Mean) < 1e-10)


# At specific levels of continuous
estim1 <- suppressMessages(estimate_means(model, at = "Sepal.Width"))
expect_equal(dim(estim1), c(10, 5))
estim2 <- suppressMessages(estimate_means(model, at = "Sepal.Width", backend="marginaleffects"))
estim2 <- suppressMessages(estimate_means(model, at = "Sepal.Width", backend = "marginaleffects"))
expect_equal(dim(estim2), c(10, 6))
# Note that the absolute values are different here... for unclear reasons
expect_true(max(diff(estim1$Mean) - diff(estim2$Mean)) < 1e-10)
Expand Down Expand Up @@ -177,14 +177,14 @@ test_that("estimate_means() - mixed models", {

estim1 <- suppressMessages(estimate_means(model))
expect_equal(dim(estim1), c(3, 5))
estim2 <- suppressMessages(estimate_means(model, backend="marginaleffects"))
estim2 <- suppressMessages(estimate_means(model, backend = "marginaleffects"))
expect_equal(dim(estim2), c(3, 5))
expect_true(max(estim1$Mean - estim2$Mean) < 1e-10)

model <- lme4::glmer(Sepal.Width ~ Species + (1 | Petal.Length_factor), data = dat, family = "Gamma")
estim1 <- suppressMessages(estimate_means(model))
expect_equal(dim(estim1), c(3, 5))
estim2 <- suppressMessages(estimate_means(model, backend="marginaleffects"))
estim2 <- suppressMessages(estimate_means(model, backend = "marginaleffects"))
expect_equal(dim(estim2), c(3, 5))
expect_true(max(estim1$Mean - estim2$Mean) < 1e-10)
})

0 comments on commit ee7b9ad

Please sign in to comment.