Skip to content
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

Added ordering for dplyr locale issue. #27

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions R/variance-linear.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ vcov_sr_diag <- function(data, mod, residual=NULL){
if(is.null(residual)){
residual <- stats::residuals(mod)
}

result <- mod$data %>%
dplyr::mutate(resid=residual) %>%
result <- dplyr::tibble(
resid=residual,
treat=data$treat
) %>%
dplyr::group_by(.data$treat) %>%
dplyr::summarize(se=stats::sd(.data$resid), .groups="drop") %>%
dplyr::mutate(se=.data$se*sqrt(1/data$pie))
Expand Down
55 changes: 55 additions & 0 deletions tests/testthat/test-factor-ordering.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
library(MASS)
library(dplyr)

DATA <- RobinCar:::data_sim
DATA$A <- as.factor(DATA$A)
DATA$y_bin <- ifelse(DATA$y > 2, 1, 0)

# Ensure that the naming/capitalization
# of the treatment variable does not impact results.

DATA <- DATA %>% mutate(
A2=paste0("TREAT", A),
A3=ifelse(A2 == "TREAT1", "treat1", A2)
)

# Doing DATA %>% group_by(A3) %>% summarize(n()) will
# order the levels by c("TREAT0", "TREAT2", "treat1")
#
# whereas factor(DATA$A3) will order the levels
# by c("TREAT0", "TREAT1", "TREAT2").

test_that("GLM full function -- linear (ANOVA)", {

# The locale is actually the issue -- testthat
# automatically sets locale C but the issue only
# appears under locale != C. So here we set it to
# en_US.UTF-8.

original_locale <- Sys.getlocale("LC_COLLATE")
Sys.setlocale("LC_COLLATE", "en_US.UTF-8")

# English language ordering
langENG <- robincar_glm(
df=DATA,
response_col="y",
treat_col="A2",
car_scheme="simple",
formula="y ~ A2 + x1")

# C language ordering
langC <- robincar_glm(
df=DATA,
response_col="y",
treat_col="A3",
car_scheme="simple",
formula="y ~ A3 + x1")

expect_equal(langENG$result$estimate, langC$result$estimate)
expect_equal(c(langENG$varcov), c(langC$varcov))

# Reset the locale
Sys.setlocale("LC_COLLATE", original_locale)

})

Loading