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

augment() with newdata gives wrong results #125

Open
JohannesNE opened this issue Apr 1, 2022 · 0 comments
Open

augment() with newdata gives wrong results #125

JohannesNE opened this issue Apr 1, 2022 · 0 comments

Comments

@JohannesNE
Copy link

JohannesNE commented Apr 1, 2022

I originally posted this as a comment to another issue, but it should probably be a separate issue. #123 (comment)_

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant