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

Adding sizer to draw() in gratia #117

Closed
asanders11 opened this issue Sep 30, 2021 · 6 comments
Closed

Adding sizer to draw() in gratia #117

asanders11 opened this issue Sep 30, 2021 · 6 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@asanders11
Copy link

Is it possible to add an argument similar to 'sizer' from your derivSimulCI() and CI fuctions to plot the first derivative of the spline, with sizer=TRUE to color increasing and decreasing parts of the spline?

Thanks!

@gavinsimpson gavinsimpson added the enhancement New feature or request label Oct 5, 2021
@gavinsimpson gavinsimpson added this to the 0.8 milestone Jul 7, 2022
@gavinsimpson gavinsimpson self-assigned this Jul 7, 2022
@gavinsimpson gavinsimpson modified the milestones: 0.8, 0.9 Jan 26, 2023
gavinsimpson added a commit that referenced this issue Mar 14, 2023
gavinsimpson added a commit that referenced this issue Mar 14, 2023
@gavinsimpson
Copy link
Owner

@asanders11 I now have sizer-like options available through a new function add_sizer() which works for objects created by derivatives(), as well as new code in draw.derivatives() that allows the sizer-like information to be shown:

plot (8)

That plot was created by

dat <- data_sim("eg1", n = 400, dist = "normal", scale = 2, seed = 42)
mod <- gam(y ~ s(x0) + s(x1) + s(x2) + s(x3), data = dat, method = "REML")

## first derivatives of all smooths using central finite differences
d <- derivatives(mod, type = "central")

draw(d, add_change = TRUE, change_type = "sizer")

with the latest version of gratia here (0.8.1.19 or later)

Does that cover what you were looking for or were you also thinking about plotting smooths with significant periods of change indicated on them too?

@asanders11
Copy link
Author

This is great, Gavin..thank you! And yes, I was hoping to plot significant periods of change as well.

gavinsimpson added a commit that referenced this issue Mar 15, 2023
@gavinsimpson
Copy link
Owner

I don't think I can implement this within draw.gam() itself as the way it works is quite complex already. I have implemented this in the draw() codes for smooth_estimates() objects however.

Now you can use smooth_estimates() to evaluate the smooths, then add_sizer() adds the derivative info needed, which the draw() method will recognise and plot:

su_eg4 <- data_sim("eg4", n = 400,  dist = "normal", scale = 2, seed = 1)
su_m_factor_by <- gam(y ~ fac + s(x2, by = fac) + s(x0),
                      data = su_eg4, method = "REML")
smooth_estimates(su_m_factor_by) |>
  add_sizer(derivatives = d, type = "sizer")  |>
  draw(grouped_by = TRUE)

produces:

plot-10

for example, showing that this works also with the new groped_by argument to group factor-by smooths.

So I'm going to call that closed for now. Hope that's OK? If I've missed something, let me know.

@asanders11
Copy link
Author

Appreciate it, Gavin. This is exactly what I needed!

@asanders11
Copy link
Author

Hi Gavin - sorry to bring you back to this topic, but do you know when add_sizer() will be added to gratia ? Trying to use it with some data, but it doesn't seem to be in the new update (v 0.8.1). TIA!

@gavinsimpson
Copy link
Owner

gavinsimpson commented Mar 22, 2023

It'll be a few weeks at least before I can get this on CRAN, but you don't need to wait for that; this is in the version on GitHub already, and the README.md file contains instructions on installing the package from the wonderful r-universe repo and build system. https://github.com/gavinsimpson/gratia#installing-gratia

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants