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

Dynamic predictor in effect_plot and interact_plot #37

Closed
haishiro opened this issue Aug 16, 2018 · 3 comments

Comments

Projects
None yet
2 participants
@haishiro
Copy link

commented Aug 16, 2018

It'd be nice if it were possible to pass in the predictor for effect_plot and interact_plot as a string variable, since one might want to look at the effects of each variable in a model without having to explicitly type out each predictor.

Something like

for(var in colnames(df)){
    effect_plot(model,pred=var)
}
@jacob-long

This comment has been minimized.

Copy link
Owner

commented Aug 16, 2018

I have been thinking about ways I can do what you're suggesting without causing existing code to stop working.

In the meantime, a way around this is to use the functions make_predictions and plot_predictions, which use standard evaluation — that is, var will be evaluated as the string for the column name rather than as "var".

Try this:

for (var in colnames(df)) {
  predictions <- make_predictions(model, pred = var, interval = TRUE)
  plot_predictions(predictions)
}

As a side note, you'll either need interval = TRUE in the call to make_predictions or interval = FALSE in the call to plot_predictions due to a bug I just found while preparing the answer to your question.

@haishiro

This comment has been minimized.

Copy link
Author

commented Aug 18, 2018

Thanks for the workaround - that works for what I'm doing at the moment.

On trying to implement it without breaking existing code, I've come up with two suggestions:

  1. Check the length of pred. It's unlikely that someone is using data with only 1 row, so if the length is 1 then one could assume that the user intends for the predictor to be treated as a string instead of as the bare name.
data(cars)
attach(cars)

# Returns 1
length("speed")

# Returns nrow(cars)
length(speed)

# Returns 1
pred <- "speed"
length(pred)
  1. Verify that pred actually exists in the model. I'm not sure how clunky this would be considering each type of model is structured a little bit differently, but you can extract a character vector of term labels used in the model:
# lm
attr(model$terms,"term.labels")
# lmer
attr(attr(model@frame,"terms"),"term.labels")

If the parsed pred doesn't exist in this vector, then use pred as a string instead. Could even go one step further and give a warning if both are not found in term labels, since make_predictions will throw an error that is not terribly descriptive when someone attempts to pass in a predictor that doesn't exist.

@jacob-long

This comment has been minimized.

Copy link
Owner

commented Jan 30, 2019

Starting in jtools version 2.0.0 (commit 6675236) — which is not yet released but is available in the interactions-split branch — the pred arguments are evaluated using tidy evaluation. In interactive use, this won't change anything.

To use within a function/loop/etc., you just need the "bang bang" syntax that will be familiar for those who have programmed with recent versions of dplyr. For instance, this

for (var in colnames(df)) {
    effect_plot(model, pred = !! var)
}

will work. The !! is exported from the rlang package (and dplyr and perhaps others) and tells effect_plot to treat var as a variable to be evaluated rather than the literal name of the column.

@jacob-long jacob-long closed this Jan 30, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.