Just piggybacking off the previous answer, there actually is a package in R designed to yield derivatives of natural splines, and you can pretty quickly obtain the derivatives of the fitted spline and then use a root finding package.
require(splines2)
require(rootSolve)
set.seed(1234)
n = 1000
x = rnorm(n);
basis = model.matrix(~naturalSpline(x,df=6,
Boundary.knots = c(-4,4),
knots = quantile(x,probs = seq(0.1,0.9,length.out=6))))
beta = rnorm(8,sd=3)
y = basis%*%beta + rnorm(n)
fit <- lm(y~-1+basis)
xeval = seq(min(x),max(x),0.01)
derivBasis = model.matrix(~-1+naturalSpline(xeval,
Boundary.knots = c(-4,4),
knots = quantile(x,probs = seq(0.1,0.9,length.out=6)),
derivs = 1))
derivs = derivBasis%*%coef(fit)[-1]
deriv_func <- function(xpoint){
naturalSpline(xpoint,
Boundary.knots = c(-4,4),
knots = quantile(x,probs = seq(0.1,0.9,length.out=6)),
derivs = 1)%*%coef(fit)[-1]
}
zeroes = uniroot.all(deriv_func,interval = range(x))
plot(x,y,xlab='X',ylab='Y');abline(v=zeroes,lty=2)
lines(sort(x),fit$fitted.values[order(x)],col='red',lwd=5)
plot(xeval,derivs,xlab='X',ylab="df(X)/dx");abline(h=0);abline(v=zeroes,lty=2)
