Skip to content

Commit

Permalink
Merge pull request #509 from banfai/508-fix-mising-levs
Browse files Browse the repository at this point in the history
Fix ref_grid with missing levels
  • Loading branch information
rvlenth authored Sep 26, 2024
2 parents a39b44b + 3d69943 commit ca91c9c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ title: "NEWS for the emmeans package"

## emmeans 1.10.4.90xxxx
* Fix for long-standing `weights` bug in `lme()` (#356)
* Fix for inconsistent contrasts in case of missing levels (#508)


## emmeans 1.10.4
Expand Down
2 changes: 1 addition & 1 deletion R/0nly-internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
### Internal function to implement 'allow.na.levs' option
.chk.fac = function(x) {
if(get_emm_option("allow.na.levs"))
x
factor(x, exclude = NULL)
else
factor(x)
}
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-ref_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,13 @@ MOats.emm = emmeans(MOats.lm, ~ Variety * rep.meas)
test_that("We get same predictions with and without nuisance specs", {
expect_equal(predict(MOats.rgn), predict(MOats.emm), 0.001)
})

# Missing and NA levels
miss.df = data.frame(x = factor(c("a", "a", "b", NA), levels = c("a", "b", "c", NA)), y = 1:4)
miss.lm = lm(y ~ x, data = miss.df)
miss.rg1 = ref_grid(miss.lm)
miss.rg2 = ref_grid(miss.lm, data = miss.df)
test_that("Reference grid handles missing values", {
expect_equal(length(miss.rg1@levels$x), 2)
expect_equal(length(miss.rg2@levels$x), 2)
})

0 comments on commit ca91c9c

Please sign in to comment.