Hi:
Here's an example:
x = seq(0, 2 * pi, length = 500)
df <- data.frame(x = x, y = sin(x) + rnorm(500, s = 0.3))
library(splines)
ggplot(df, aes(x = x, y = y)) + geom_point() +
stat_smooth(data = g, aes(x = x, y = y), method = 'lm',
formula = y ~ ns(x, 3), col = 'red', size = 2, se= FALSE)
It should be simpler if you use lm() with one of the spline functions generating the basis matrix. I used ns() here, but one could use other basis functions equally well. Here's a different example using the rcs() function in package rms:
library(rms)
ggplot(df, aes(x = x, y = y)) + geom_point() +
stat_smooth(data = g, aes(x = x, y = y), method = 'lm',
formula = y ~ rcs(x, 4), col = 'red', size = 2, se= FALSE)
The graph should be essentially the same. The se argument is unneeded because confidence curves won't be plotted if you specify se = TRUE.
HTH,
Dennis