Hello everyone. I'm new to R and I'm using spline functions. With the command splinefun (x, y) I get the function of interpolating the values x and y. Later, I can evaluate that function for values of x by obtaining the respective values of y. The point is that I need the inverse operation, with the function, for a value of Y I need to know the value of x. Could you please help me? A cordial greeting [[alternative HTML version deleted]]
If I understand correctly, not in general possible. Suppose for a bunch of different x's the y's are all constant =0. What x would correspond to y = 1. Or suppose (x,y) pairs trace a sine function over several periods. Then there is no unique x corresponding to y = .5, say. Perhaps if you more explicitly specified the nature of your problem (e.g. is y monotonic in x?) some assistance might be provided. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Tue, Aug 14, 2018 at 8:48 AM, Tania Morgado Garcia <tmg1970 at gmail.com> wrote:> Hello everyone. I'm new to R and I'm using spline functions. With the > command splinefun (x, y) I get the function of interpolating the values x > and y. Later, I can evaluate that function for values of x by obtaining the > respective values of y. The point is that I need the inverse operation, > with the function, for a value of Y I need to know the value of x. Could > you please help me? > A cordial greeting > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
The uniroot function can be used to find a value in a specified interval, if it exists. On Tue, Aug 14, 2018 at 3:30 PM Tania Morgado Garcia <tmg1970 at gmail.com> wrote:> > Hello everyone. I'm new to R and I'm using spline functions. With the > command splinefun (x, y) I get the function of interpolating the values x > and y. Later, I can evaluate that function for values of x by obtaining the > respective values of y. The point is that I need the inverse operation, > with the function, for a value of Y I need to know the value of x. Could > you please help me? > A cordial greeting > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com
On 15/08/18 03:48, Tania Morgado Garcia wrote:> Hello everyone. I'm new to R and I'm using spline functions. With the > command splinefun (x, y) I get the function of interpolating the values x > and y. Later, I can evaluate that function for values of x by obtaining the > respective values of y. The point is that I need the inverse operation, > with the function, for a value of Y I need to know the value of x. Could > you please help me?Your question is ill-posed. There could easily be multiple x values corresponding to a single y value, unless the spline function is monotone. If you can specify an interval which encloses the x value that you are trying to and over which the spline function is monotone, then uniroot() might provide what you want. Something like: spf <- splinefun(x,y) uniroot(f=function(x){spf-y0},interval=c(a,b)) where y0 is the y value for which you want the corresponding x value, where spf() is monotone on [a,b], and where there *exists* an x value between a and b such that spf(x) = y0. Good luck. cheers, Rolf Turner -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276
On 14/08/2018 11:48 AM, Tania Morgado Garcia wrote:> Hello everyone. I'm new to R and I'm using spline functions. With the > command splinefun (x, y) I get the function of interpolating the values x > and y. Later, I can evaluate that function for values of x by obtaining the > respective values of y. The point is that I need the inverse operation, > with the function, for a value of Y I need to know the value of x. Could > you please help me?Others have pointed out uniroot(). One other possibility: maybe you don't need both the function and its inverse, or an approximate inverse is good enough. In either of those cases, just swap x and y in the call to splinefun(), and you'll get a new function mapping y values to the corresponding x values. (You'll get nonsense or an error in cases where this mapping is not unique.) It won't match the inverse of the original spline interpolator except at observed (x,y) pairs, but will usually be close, especially if the functions are pretty smooth. Duncan Murdoch