bbolker / broom.mixed

tidy methods for mixed models in R
228 stars 23 forks source link

`augment()` with newdata gives wrong results #125

Open JohannesNE opened 2 years ago

JohannesNE commented 2 years ago

I originally posted this as a comment to another issue, but it should probably be a separate issue. https://github.com/bbolker/broom.mixed/issues/123#issuecomment-1057844093_

In the data frame returned by augment, the values .mu and rightwards are directly drawn from the model object.

This is still the case if newdata is used. In this case, these values (e.g. .mu) do not correspond to the newdata (which the user probably expects). If newdata has another length than the model data, a warning is shown.

library(lme4)
#> Loading required package: Matrix
library(broom.mixed)

d <- sleepstudy 

fm1 <- lmer(Reaction ~ Days + (Days | Subject), d)

d_rev <- d[nrow(d):1,]

d_rev_aug <- augment(fm1, newdata = d_rev)

d_rev_aug[, c(".fitted", ".mu")]
#> # A tibble: 180 × 2
#>    .fitted   .mu
#>      <dbl> <dbl>
#>  1    369.  254.
#>  2    358.  273.
#>  3    346.  293.
#>  4    334.  313.
#>  5    322.  332.
#>  6    311.  352.
#>  7    299.  372.
#>  8    287.  391.
#>  9    275.  411.
#> 10    264.  431.
#> # … with 170 more rows

# newdata other length than model data
augment(fm1, newdata = d[-1,])                                                        
#> A tibble: 179 × 11
#> [tibble ...]  
#> Warning message:
#> In indices[which(stats::complete.cases(original))] <- seq_len(nrow(x)) :
#> number of items to replace is not a multiple of replacement length