[R] interpolation to abscissa

David Winsemius dwinsemius at comcast.net
Thu Jan 15 17:01:13 CET 2009


On Jan 15, 2009, at 10:04 AM, e-letter wrote:

> On 13/01/2009, David Winsemius <dwinsemius at comcast.net> wrote:
>> It's fairly clear from the documentation that approxfun() will not
>> extrapolate.
>>
>> help.search("extrapolate")
>> library(Hmisc)
>> ?approxExtrap
>>
>> Some sort of minimization approach:
>>
>>> approxExtrap(x=c(0,5,10,15,20),  
>>> y=c(16,45,77,101,125),xout=c(-4,0,4))
>> $x
>> [1] -4  0  4
>>
>> $y
>> [1] -7.2 16.0 39.2
>>
>>> approxExtrap(x=c(0,5,10,15,20),
>> y=c(16,45,77,101,125),xout=seq(-2.8,-2.6, by=0.01))
>> $x
>>  [1] -2.80 -2.79 -2.78 -2.77 -2.76 -2.75 -2.74 -2.73 -2.72 -2.71
>> -2.70 -2.69 -2.68
>> [14] -2.67 -2.66 -2.65 -2.64 -2.63 -2.62 -2.61 -2.60
>>
>> $y
>>  [1] -0.240 -0.182 -0.124 -0.066 -0.008  0.050  0.108  0.166  0.224
>> 0.282  0.340
>> [12]  0.398  0.456  0.514  0.572  0.630  0.688  0.746  0.804  0.862
>> 0.920
>>
>> How accurate do you need the answer?
>>
>> I tried Hmisc's inverseFunction(), but it returned 0 for an argument
>> of zero:
>>
>>> invF <- inverseFunction(x=c(0,5,10,15,20), y=c(16,45,77,101,125))
>>> invF(0)
>>
>> So I then hacked Harrell's inverseFunction by substituting
>> approxExtrap in every in instance
>> where approx appeared, creating invFunc2:
>>
>> then
>>
>>> invF <- invFunc2(x=c(0,5,10,15,20), y=c(16,45,77,101,125))
>>>
>>> invF(0)
>> [1] -2.758621
> I have compared your answer to those obtained from gnuplot, scilab and
> qtiplot; all report a result of x=-3.28. Why is r different?

Perhaps a coding error on my part (or on your part). Perhaps different  
methods (none of which you describe)?

I suspect that my method only used the first two points (I just  
checked by plotting and -2.7 is closer to the paper and pen result I  
get than is -3.28. Perhaps you made an extrapolation from a linear fit  
of a dataset that is not co-linear?

 > lm(c(0,5) ~ c(16,45))

Call:
lm(formula = c(0, 5) ~ c(16, 45))

Coefficients:
(Intercept)    c(16, 45)
     -2.7586       0.1724

It not that "R is different",  it is merely that I used it differently  
than you used your other tools.

Here's another method ( using all points and again reversing the roles  
of x and y) :
 > lm(c(0,5,10,15,20) ~ c(16,45,77,101,125))

Call:
lm(formula = c(0, 5, 10, 15, 20) ~ c(16, 45, 77, 101, 125))

Coefficients:
             (Intercept)  c(16, 45, 77, 101, 125)
                 -3.2332                   0.1818



>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.




More information about the R-help mailing list