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 do fails on lm(y ~ poly(x)) #289

Closed
fja062 opened this issue Mar 7, 2018 · 6 comments
Closed

augment with do fails on lm(y ~ poly(x)) #289

fja062 opened this issue Mar 7, 2018 · 6 comments

Comments

@fja062
Copy link

fja062 commented Mar 7, 2018

I am getting an error when I use augment within do. My quantile regression model uses splines::bs(...) as the predictor, but the problem can be replicated with lm using poly.

library(MASS)
data(mcycle)

bind_rows(a = mcycle, b = mcycle, .id = "ver") %>%
   group_by(ver) %>% 
   do({
     fit = lm(accel ~ poly(times, degree = 2), data = .)
     augment(fit)
   })

Error in bind_rows_(x, .id) : Argument 2 must be length 133, not 266

@dgrtwo
Copy link
Collaborator

dgrtwo commented Mar 7, 2018

The problem is that in the augment output, the poly(times, degree = 2) column isn't a vector, but a 2-column matrix.

This is definitely a problem for augment in general (should we always force the output columns from model.frame() to be vectors? What kind of error could we give?) But it has a workaround in this case of giving the data argument to augment(), which means you get the original columns like accel and times and not a poly() matrix column:

bind_rows(a = mcycle, b = mcycle, .id = "ver") %>%
   group_by(ver) %>% 
   do({
     fit = lm(accel ~ poly(times, degree = 2), data = .)
     augment(fit, data = .)
   })

@fja062
Copy link
Author

fja062 commented Mar 8, 2018

Ah ok, that makes sense. And the workaround works perfectly - thanks!

@alexpghayes
Copy link
Collaborator

My take is that augment should add columns to original data but not keep any columns from model.frame. I think this specification resolves this issue, although we'll need to check that it gets enforced.

@alexpghayes
Copy link
Collaborator

Closing in favor of #405. Eventually matrix-column support in tibbles will arrive and this will just work.

@jcdoesds
Copy link

jcdoesds commented Sep 25, 2020

I am having difficulty seeing how to apply this solution to nested data. In that case, If I declare the augment with data = . or data = .x, I get an error.

test <- mtcars %>%
  nest(data = -cyl) %>%
  mutate(
    fit = map(data, ~ lm(mpg ~ disp + poly(wt, 2), data = .x)),
    tidied = map(fit, tidy),
    glanced = map(fit, glance),
    augmented = map(fit, augment) 
    # augmented = map(augment(fit, data = .)) # fails w subscript out of bounds
  )
test %>%
  unnest(augmented) # this fails

test$augmented[[1]]# this works

After some discussion on SO, I see that we can get the output via:

test %>%
   unnest_wider(augmented)

@github-actions
Copy link

github-actions bot commented Mar 6, 2021

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants