[R] approxfun returning value higher than I would expect

stephen sefick ssefick at gmail.com
Wed Sep 15 22:32:18 CEST 2010


x <- c(-0.48,-0.48,-0.42,-0.26,0.58,0.48,0.47,0.54,0.5,0.52,0.52,0.56,0.58,0.61,0.68)
y <- c(0,0.2,0.4,0.6,0.8,1,1.2,1.4,1.6,1.8,2,2.2,2.4,2.6,2.8)

s <- approxfun(x[4:5], y[4:5], ties=mean)

s(0)

#This is the value that I want.  The first zero crossing in the order
of y.  #In other words in between 0.6 and 0.8.

#The data is a channel cross-section, and should really be visualized

plot(x~y, ylim=c(1,-1)) #this is half of a cross-section

#The bankfull depth is where x is equal to 0

On Wed, Sep 15, 2010 at 3:12 PM, stephen sefick <ssefick at gmail.com> wrote:
> Thanks and I'll strip the code down even more in future posts.
>
> Stephen
>
> On Wed, Sep 15, 2010 at 3:05 PM, Duncan Murdoch
> <murdoch.duncan at gmail.com> wrote:
>>  On 15/09/2010 3:48 PM, stephen sefick wrote:
>>>
>>> Below is the code that I am using in a much larger function.  I would
>>> expect a bankfull measure at zero to be between 0.6 and 0.8 approxfun
>>> is returning 0.8136986.  I am sure that I am missing something.
>>>
>>> measure_bkf<- (structure(list(measurment_num = c(0, 0.2, 0.4, 0.6,
>>> 0.8, 1, 1.2,
>>> 1.4, 1.6, 1.8, 2, 2.2, 2.4, 2.6, 2.8, 3, 3.2, 3.4), bankfull_depths_m
>>> = c(-0.48,
>>> -0.48, -0.42, -0.26, 0.58, 0.48, 0.47, 0.54, 0.5, 0.52, 0.52,
>>> 0.56, 0.58, 0.61, 0.68, 0.62, 0.67, 0.66)), .Names = c("measurment_num",
>>> "bankfull_depths_m"), row.names = c("6124", "612", "613", "614",
>>> "615", "616", "617", "618", "619", "620", "621", "622", "623",
>>> "624", "625", "626", "627", "628"), class = "data.frame"))
>>>
>>>
>>> measure_bkf_not_zero<- subset(measure_bkf,
>>> measure_bkf$bankfull_depths_m!=0)
>>>
>>> bkf_min<- which.max(measure_bkf_not_zero[,"bankfull_depths_m"]<0)
>>>
>>> bkf_max<- which.max(measure_bkf_not_zero[,"bankfull_depths_m"])
>>>
>>> #bkf_min<- ifelse(length(bkf_min)>1, bkf_min[1], bkf_min)
>>> #bkf_max<- ifelse(length(bkf_max)>1, bkf_max[1], bkf_max)
>>>
>>> #s<- with(measure_bkf_not_zero, approx(measurment_num,
>>> bankfull_depths_m,
>>> xout=seq(measure_bkf_not_zero[bkf_min,"measurment_num"],
>>> measure_bkf_not_zero[bkf_max,"measurment_num"], length=2000)))
>>> #int_bkf<- with(s, x[which.min(y[y>0])])
>>>
>>> s<- with(measure_bkf_not_zero[bkf_min:bkf_max,],
>>> approxfun(bankfull_depths_m, measurment_num), ties=mean)
>>>
>>> int_bkf<- s(0)
>>>
>>
>> It is easier to see the problem if you don't leave all the complications in
>> the beginning.  Just define some variables and
>> show the interpolation on a plot:
>>
>> x <-
>> c(-0.48,-0.48,-0.42,-0.26,0.58,0.48,0.47,0.54,0.5,0.52,0.52,0.56,0.58,0.61,0.68)
>> y <- c(0,0.2,0.4,0.6,0.8,1,1.2,1.4,1.6,1.8,2,2.2,2.4,2.6,2.8)
>> plot(x, y)
>> s <- approxfun(x, y, ties=mean)
>> curve(s, add=TRUE)
>>
>> On my system, this looks okay in 2.11.1, but not in R-patched or R-devel
>> (soon to be 2.12.0).  It is fixed if the x values are ordered, but it's not
>> supposed to need that.  I'll take a look.
>>
>> Duncan Murdoch
>>
>
>
>
> --
> Stephen Sefick
> ____________________________________
> | Auburn University                                   |
> | Department of Biological Sciences           |
> | 331 Funchess Hall                                  |
> | Auburn, Alabama                                   |
> | 36849                                                    |
> |___________________________________|
> | sas0025 at auburn.edu                             |
> | http://www.auburn.edu/~sas0025             |
> |___________________________________|
>
> Let's not spend our time and resources thinking about things that are
> so little or so large that all they really do for us is puff us up and
> make us feel like gods.  We are mammals, and have not exhausted the
> annoying little problems of being mammals.
>
>                                 -K. Mullis
>
> "A big computer, a complex algorithm and a long time does not equal science."
>
>                               -Robert Gentleman
>



-- 
Stephen Sefick
____________________________________
| Auburn University                                   |
| Department of Biological Sciences           |
| 331 Funchess Hall                                  |
| Auburn, Alabama                                   |
| 36849                                                    |
|___________________________________|
| sas0025 at auburn.edu                             |
| http://www.auburn.edu/~sas0025             |
|___________________________________|

Let's not spend our time and resources thinking about things that are
so little or so large that all they really do for us is puff us up and
make us feel like gods.  We are mammals, and have not exhausted the
annoying little problems of being mammals.

                                -K. Mullis

"A big computer, a complex algorithm and a long time does not equal science."

                              -Robert Gentleman



More information about the R-help mailing list