-
Notifications
You must be signed in to change notification settings - Fork 28
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
Allow draw() to plot on the response scale #79
Comments
shortcut of `response = TRUE` or similar to make this easier to use.
Hello! Are there any plans to incorporate this option into smooth_estimates() any time soon? That would be super useful... |
@barbmuhling the library("mgcv")
library("gratia")
library("dplyr")
dat <- data_sim("eg1", n = 400, dist = "normal", scale = 2, seed = 2)
m1 <- gam(y ~ s(x0) + s(x1) + s(x2) + s(x3), data = dat, method = "REML")
my_const <- coef(m1)[1]
smooth_estimates(m1) |>
add_confint() |>
mutate(across(tidyselect::all_of(c("est", "lower_ci", "upper_ci")),
.fns = \(x) x + my_const)) |>
transform_fun(fun = \(x) x^2) to apply any function you want to the estimate and credible interval. (This is what is used internally by the I could add something to |
Ah, this will work perfectly, thanks so much! |
Hello Gavin, I came across this thread looking for help plotting by factors on top of each other. Apologies for the clunky code, but would this be the appropriate method? Please let me know if this should go on stack overflow instread!
|
@tkiff That works, but only for simple examples like this with a single factor. I think you're better off using library("mgcv")
library("gratia")
library("dplyr")
library("ggplot2")
dat <- data_sim("eg4", seed = 42)
m1 <- gam(y ~ fac + s(x2, by = fac) + s(x0) + s(x1), data = dat, method = "REML")
ds <- data_slice(m1, x2 = evenly(x2), fac = evenly(fac))
fv <- fitted_values(m1, data = ds)
fv |>
ggplot(aes(x = x2, y = .fitted, group = fac)) +
geom_ribbon(aes(ymin = .lower_ci, ymax = .upper_ci, fill = fac), alpha = 0.2) +
geom_line(aes(colour = fac)) Notice that In this case, you can exclude the effects of the other smooths when generating fitted values fv2 <- fitted_values(m1, data = ds, exclude = c("s(x0)", "s(x1)"))
fv2 |>
ggplot(aes(x = x2, y = .fitted, group = fac)) +
geom_ribbon(aes(ymin = .lower_ci, ymax = .upper_ci, fill = fac), alpha = 0.2) +
geom_line(aes(colour = fac)) There's no difference here because of how |
If you have questions about gratia you could ask them on SO but better might be to use the Discussion here on GitHub: https://github.com/gavinsimpson/gratia/discussions |
This is essentially complete except for the two stretch goals |
Implement the equivalent of the
trans
andshift
arguments thatmgcv:::plot.gam()
has so that users can pass in a function (e.g. the inverse link function) and a constant (e.g. the intercept), to get plots on the response scale.Should these be arguments to the
evaluate_smooth()
methods or somethingdraw()
does internally?Either way, the confidence bands aren't going to include the uncertainty in the constant, soThis is done inshift
is purely to move the origin (y-axis) around.draw()
and the various methods.Want to have a simpler way for the user to express that they want the plots on the response scale. So need a new argument (
response_scale
?) that if set toTRUE
, automatically setconstant
andfun
to the correct values/functions such that we get values and intervals on the response scale. This will override anything passed toconstant
andfun
obviously.Should we include the overall uncertainty as we're bringing in the model constant here?
What about factor
by
smooths? These should really have the constant plus the parametric term for their level added as a constant.Checklist:
constant
andfun
#253by
smooths when drawing on the response scale #254The text was updated successfully, but these errors were encountered: