Skip to content

Plotting using gratia::draw #294

Answered by gavinsimpson
rvaradhan asked this question in Q&A
Discussion options

You must be logged in to vote

What you're looking for is to predict from the model. This is usually done via predict(), but gratia::fitted_values() can will facilitate this:

library("mgcv")
library("gratia")
library("ggplot2")

eg_data <- data_sim("eg4", n = 400, dist = "normal", scale = 2, seed = 1)
m <- gam(y ~ fac + s(x2, by = fac), data = eg_data, method="REML", family = gaussian())

ds <- data_slice(m, x2 = evenly(x2), fac = evenly(fac))
fv <- fitted_values(m)

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)) +
  geom_point(data = eg_data, aes(colour = fac, y = y)) +
  facet_wrap(~ fac)

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@rvaradhan
Comment options

@rvaradhan
Comment options

@rvaradhan
Comment options

Answer selected by rvaradhan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants