# Re: [R] Calculate x-values from a spline

From: Simon Wood <s.wood_at_bath.ac.uk>
Date: Sat 29 Jul 2006 - 00:01:49 EST

On Friday 28 July 2006 13:31, Cuvelier Etienne wrote:
> Gunther Höning a écrit :
> > Hello,
> >
> > I calculate splines from messured points(x,y) of an unknown function
> > f(x). e.g.
> > x <- c(0.004115, 0.012345, 0.037037, 0.111110, 0.333330, 1.000000)
> > y <- c(37, 50, 45, 60, 50, 66)
> > w <- c(0.8540541, 0.8320000, 0.8822222, 0.7983333, 0.8220000, 0.8151515)
> > as weights
> > f <- smooth.spline(x,y,w)
> >
> > Now I have y-values and want to calculate the x-value(s) from this
> > spline. There is no further limitation to the spline, like monotonicity
> > or anything else..
> >
> > Does anyone know how to do this ?
>
> Perhaps this ?
>
> finv = splinefun(f\$y,f\$x)
>
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

> > Gunther
> >
> >
> >
> > gunther.hoening@ukmainz.de
> >
> > ______________________________________________
> > R-help@stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > http://www.R-project.org/posting-guide.html and provide commented,
> > minimal, self-contained, reproducible code.

```--

> Simon Wood, Mathematical Sciences, University of Bath, Bath, BA2 7AY UK

> +44 1225 386603  www.maths.bath.ac.uk/~sw283

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help