Git Product home page Git Product logo

Comments (2)

lhare avatar lhare commented on May 28, 2024 1

from gratia.

gavinsimpson avatar gavinsimpson commented on May 28, 2024

I wouldn't use get_by_smooth() for this — it is exported only as a helper for developers wanting to identify and extract a by variable smoother, but what it extracts is a smooth object as presented inside the fitted GAM object.

Use evaluate_smooth() or draw() on the fitted model and select the smooth you want. However, in trying draw() I am seeing a bug which I'll have to fix.

For example:

library('gratia')
library('mgcv')
set.seed(42)
dat <- gamSim(4, n = 400, verbose = FALSE)
m <- gam(y ~ fac + s(x2, by = fac) + s(x0), data = dat)
draw(m, select = 's(x2)')

The latter is supposed to work, but it isn't and I'll raise an issue to get this fixed.

Following on from the above example you could use evaluate_smooth() to get back the data the draw() is extracting internally and then draw the plots yourself:

es <- evaluate_smooth(m, smooth = 's(x2)')
es

library('dplyr')
## add 95% credible interval
es <- mutate(es, lower = est - (2 * se), upper = est + (2 * se))

## plot
library('ggplot2')
ggplot(es, aes(x = x2, y = est)) +
  geom_ribbon(aes(ymax = upper, ymin = lower, x = x2), inherit.aes = FALSE,
              alpha = 0.2) +
  geom_line() +
  facet_wrap(~ smooth)

Which produces

gratia-draw-by-smooth-manually

And then you can modify from there. Or do as draw() does and prepare separate plots for each smooth, but filtering the es object on the smooth column to select each of the smooths in turn and prepare a plot as above for each smooth separately and then use cowplot::plot_grid() to arrange them on the plot device.

from gratia.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.