Skip to content

Commit

Permalink
Allow NA factor levels (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
rvlenth committed Jul 8, 2024
1 parent af54337 commit da84560
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 13 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: emmeans
Type: Package
Title: Estimated Margin2l Means, aka Least-Squares Means
Version: 1.10.3-090002
Date: 2024-07-06
Version: 1.10.3-090003
Date: 2024-07-08
Authors@R: c(person("Russell V.", "Lenth", role = c("aut", "cre", "cph"),
email = "russell-lenth@uiowa.edu"),
person("Ben", "Bolker", role = "ctb"),
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ title: "NEWS for the emmeans package"
* Refinements in tracking static offsets
* Made d.f. consistent for `geeglm` and `glmgee` (#496)
* Fixed suggestion for installing from GitHub (#497)
* Change that allows factors to have `NA` levels (#500). This
was previously not allowed, and we added an `"allow.na.levs"` option
(defaults to `TRUE`) just in case we broke anything that used to work.


## emmeans 1.10.3
Expand Down
8 changes: 8 additions & 0 deletions R/0nly-internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@
ok
}

### Internal function to implement 'allow.na.levs' option
.chk.fac = function(x) {
if(get_emm_option("allow.na.levs"))
x
else
factor(x)
}



## Alternative to all.vars, but keeps vars like foo$x and foo[[1]] as-is
Expand Down
5 changes: 5 additions & 0 deletions R/emmGrid-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,10 @@ update.emmGrid = function(object, ..., silent = FALSE) {
#' \code{\link{summary.emmGrid}}, \code{\link{predict.emmGrid}}, \code{\link{test.emmGrid}},
#' \code{\link{confint.emmGrid}}, and \code{\link{emmip}}. The only option that can
#' affect the latter four is \code{"predict.method"}.}
#' \item{\code{allow.na.levs}}{A logical value that if \code{TRUE} (the default), allows
#' \code{NA} to be among the levels of a factor. Older versions of \pkg{emmeans} did not
#' allow this. So if problems come up (say in a messy dataset that includes incomplete cases),
#' try setting this to \code{FALSE}.}
#' \item{\code{sep}}{A character value to use as a separator in labeling factor combinations.
#' Such labels are potentially used in several places such as \code{\link{contrast}} and
#' \code{\link{plot.emmGrid}} when combinations of factors are compared or plotted.
Expand Down Expand Up @@ -680,6 +684,7 @@ emm_defaults = list (
emmeans = list(infer = c(TRUE, FALSE)),
contrast = list(infer = c(FALSE, TRUE)),
save.ref_grid = FALSE, # save new ref_grid in .Last.ref_grid
allow.na.levs = TRUE, # Do we allow factors to have NA as a level?
cov.keep = "2", # default for cov.keep arg in ref_grid
sep = " ", # separator for combining factor levels
parens = c("-|\\+|\\/|\\*", "(", ")"), # patterns for what/how to parenthesize in contrast
Expand Down
6 changes: 3 additions & 3 deletions R/ref-grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,8 @@ ref_grid <- function(object, at, cov.reduce = mean, cov.keep = get_emm_option("c

# Save the original levels of factors, no matter what
if (is.factor(x) && !(nm %in% coerced$covariates))
xlev[[nm]] = levels(factor(x))
# (applying factor drops any unused levels)
xlev[[nm]] = levels(.chk.fac(x))
# (drops any used levels unless allow.na.levs option is TRUE)
else if (is.character(x)) # just like a factor
xlev[[nm]] = sort(unique(x))

Expand All @@ -559,7 +559,7 @@ ref_grid <- function(object, at, cov.reduce = mean, cov.keep = get_emm_option("c
ref.levels[[nm]] = at[[nm]]
# factors not in 'at'
else if (is.factor(x) && !(nm %in% coerced$covariates))
ref.levels[[nm]] = levels(factor(x))
ref.levels[[nm]] = levels(.chk.fac(x))
else if (is.character(x) || is.logical(x))
ref.levels[[nm]] = chrlev[[nm]] = sort.unique(x)
# matrices
Expand Down
2 changes: 1 addition & 1 deletion R/summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -1364,7 +1364,7 @@ print.summary_emm = function(x, ..., digits=NULL, quote=FALSE, right=TRUE, expor
x[[estn]] = format(est, digits=digits)
x[[estn]][is.na(est)] = "nonEst"
}
xc = as.matrix(format.data.frame(x, digits=digits, na.encode=FALSE))
xc = as.matrix(format.data.frame(x, digits=digits, na.encode=TRUE))
m = apply(rbind(just, names(x), xc), 2, function(x) {
w = max(sapply(x, nchar))
if (x[1] == "R") format(x[-seq_len(2)], width = w, justify="right")
Expand Down
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,20 @@ easily produce these results, as well as various graphs of them
may be viewed.

* **GitHub** To install the latest development version from GitHub,
install the newest version of the **devtools**
package; then run
install the newest version of the **remotes**
package. If you are a Windows user, you should also first download and
install the latest version of
[`Rtools`](https://cran.r-project.org/bin/windows/Rtools/).
Then run

```r
remotes::install_github("rvlenth/emmeans", dependencies = TRUE, build_vignettes = TRUE)
```
Omitting the `build_vignettes` argument can save some time if you don't want the vignettes. They can always be found on the (emmeans pkgdown site)[https://rvlenth.github.io/emmeans/].
Omitting the `build_vignettes` argument can save some time if you don't want the vignettes.
They can always be found [for the latest CRAN version](https://cran.r-project.org/package=emmeans) or
-- perhaps more up-to-date -- the [emmeans site](https://rvlenth.github.io/emmeans/).

*Note:* If you are a Windows user, you should also first download and
install the latest version of
[`Rtools`](https://cran.r-project.org/bin/windows/Rtools/).
*Note:*

For the latest release notes on this development version, see the
[NEWS file](https://github.com/rvlenth/emmeans/blob/master/NEWS.md)
Expand Down
6 changes: 5 additions & 1 deletion man/emm_options.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit da84560

Please sign in to comment.