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

Missing .resid when terms in the formula are function calls #1121

Closed
AaronRendahl opened this issue Oct 5, 2022 · 1 comment · Fixed by #1123
Closed

Missing .resid when terms in the formula are function calls #1121

AaronRendahl opened this issue Oct 5, 2022 · 1 comment · Fixed by #1123

Comments

@AaronRendahl
Copy link

AaronRendahl commented Oct 5, 2022

This is a request to reopen #937 , with a potential diagnosis and fix.
This bug describes how the .resid column is missing when terms in the formula are function calls of variables, eg. log(var); see here for the diagnosis in that issue.

I agree the problem is in the response function in utilities.R; it attempts to create the response variable by calling model.response on a new version of the data set, created using model.frame(terms(object), data = newdata). The problem with this is that if newdata was created using model.frame in the first place (which it is by default in augment_newdata), it will already have the results from the function calls instead of the data itself, so recreating the function calls fails.

The solution is to check if newdata already has a response (ie, was created from model.frame), and if so, to use that directly, instead of recreating it. If not, then the current code should be run. The below code adds this, wrapping it in a possibly, as if it doesn't have a response associated with it, an error is likely.

See ?model.frame/?get_all_vars (the last paragraph of Details) for a brief explanation of what model.frame does, in contrast with with get_all_vars does. I don't think we can't use get_all_vars instead because it requires the raw data set as a parameter.

response <- function(object, newdata = NULL) {
  ## if the newdata was created as a model.frame, it will have a response already
  ## wrap this in possibly, because if not, an error is likely
  safe_model.response <- purrr::possibly(model.response, NULL)
  resp <- safe_model.response(newdata)
  ## if not, need to recreate it from the newdata
  if(is.null(resp)) {
    resp <- model.response(model.frame(terms(object), data = newdata, na.action = na.pass))
  }
  resp
}
@github-actions
Copy link

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

Successfully merging a pull request may close this issue.

1 participant