Skip to content
Prev 96958 / 398500 Next

Calculate x-values from a spline

On Friday 28 July 2006 13:31, Cuvelier Etienne wrote:
I think this solution is a bit unstable, unless the spline is monotonic. For 
example...

set.seed(1)
x <- sort(runif(100)) ## divide this by 2 to get case where it works
y <- x*(1-x)+rnorm(100)*.01
par(mfrow=c(1,2))
plot(x,y,main="data and spline")
f <- smooth.spline(x,y) ## fit spline to simulated non-monotonic data
lines(x,fitted(f),col=2) ## plot fit
finv <- splinefun(f$y,f$x) ## "inverse function"
xx <- seq(0,0.25,length=1000) 
plot(xx,finv(xx),type="l",main="inverse!") ## plot of "inverse"
abline(0,0);abline(1,0) # plot range that x should lie in

The problem is that you can't really avoid dealing with the fact that the 
problem doesn't have a unique solution if the spline is not monotonic.

Simon