Skip to content
Discussion options

You must be logged in to vote

With the big caveat that the derivatives here are on the scale of the linear predictor not the response (although in this specific example and the one in the blog post these are the same thing — if you want derivatives on the response scale, see gratia::response_derivatives() or marginaleffects::slopes()) here's how to do this.

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

# example data
df <- data_sim("eg1", seed = 42)

# model
m <- bam(y ~ s(x2), data = df)

# best to make sure everything uses the same data, so new data evenly over x2
ds <- m |>
  data_slice(
    x2 = evenly(x2)
  ) |>
  mutate(
    .row = row_number() # add a row number just so you can assure y…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@MasonWirtz
Comment options

Answer selected by gavinsimpson
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