Let's consider the following data, adapted from here and provided as example:
library(mgcViz)
set.seed(123)
n <- 10e4
dat <- data.frame("x1" = rnorm(n), "x2" = rnorm(n), "x3" = rnorm(n))
dat$y <- with(dat, sin(x1) + 0.5*x2^2 + 0.2*x3 + pmax(x2, 0.2) * rnorm(n))
b <- bam(y ~ te(x1, x2, k=c(6,9), bs=c("cr","cr")), data = dat, method = "fREML", discrete = TRUE)
b <- getViz(b)
plot(sm(b, 1)) + l_fitRaster() + l_fitContour() + l_points()
Question:
Is there a way to extract both x1 and x2 locations of knots from te()
interaction?
This can be done as proposed here:
sm <- b[["smooth"]][[1]]
x1_k <- sm[["margin"]][[1]][["xp"]]
x2_k <- sm[["margin"]][[2]][["xp"]]
plot(sm(b, 1)) + l_fitRaster() + l_fitContour() + l_points() +
geom_vline(xintercept = x1_k, linetype="dashed", color = "red", size = 1) +
geom_hline(yintercept = x2_k, linetype="dashed", color = "red", size = 1)