Skip to content

Commit

Permalink
NA + simulate + re.form = NULL (GH #737) [run ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
bbolker committed Nov 7, 2023
1 parent ce9bd2d commit 1e678b6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: lme4
Version: 1.1-35.1
Version: 1.1-35.1.9000
Title: Linear Mixed-Effects Models using 'Eigen' and S4
Authors@R: c(
person("Douglas","Bates", role="aut",
Expand Down
3 changes: 3 additions & 0 deletions R/predict.R
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,9 @@ predict.merMod <- function(object, newdata=NULL, newparams=NULL,
newRE <- mkNewReTrms(object, rfd, re.form, na.action=na.action,
allow.new.levels=allow.new.levels)
REvals <- base::drop(as(newRE$b %*% newRE$Zt, "matrix"))
if (class(fit.na.action) %in% c("omit", "exclude") && length(fit.na.action)>0) {
REvals <- REvals[-fit.na.action]
}
pred <- pred + REvals
if (random.only) {
fit.na.action <- attr(newRE$Zt,"na.action")
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-predict.R
Original file line number Diff line number Diff line change
Expand Up @@ -471,3 +471,13 @@ test_that("prediction standard error", {
expect_equal(p7, p9)

})

test_that("NA + re.form = NULL + simulate OK", {
d <- lme4::sleepstudy
d$Reaction[1] <- NA
fm1 <- lmer(Reaction ~ Days + (Days | Subject), d)
expect_equal(c(head(simulate(fm1, seed = 101, re.form = NULL)[[1]])),
c(266.139101412856, 308.148180398426,
296.081377893883, 338.367909016478,
360.294339946214, 401.91050930589))
})

2 comments on commit 1e678b6

@mmaechler
Copy link
Member

@mmaechler mmaechler commented on 1e678b6 Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm.... class(.) %in% c("..", "...") is "the same bad" as the other cases mentioned in my think again! blog ...
even though you may argue here that there are only a few possible string values for class() here, this may be true, typically, but not necessarily...

@bbolker
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Would you suggest inherits(fit.na.action, "omit") || inherits(fit.na.action, "exclude") ?

I'm not sure whether the fact that the na.action attributes are classed is explicitly documented anywhere (except maybe in the White Book, although I doubt it ...)

From ?model.frame:

A ‘data.frame’ containing the variables used in ‘formula’ plus
those specified in ‘...’. It will have additional attributes,
including ‘"terms"’ for an object of class ‘"terms"’ derived from
‘formula’, and possibly ‘"na.action"’ giving information on the
handling of ‘NA’s (which will not be present if no special
handling was done, e.g. by ‘na.pass’).

From ?na.action:

Information from the action which was applied to ‘object’ if ‘NA’s
were handled specially, or ‘NULL’.

?na.omit does not have a "Value" section, so we just have to Use the Source to get this information.

(I haven't checked the R Language Definition ...)

Please sign in to comment.