0

I am trying to interpolate the exceedance probability between 2 time series, the time is not important here.

data1<-c(10,11,12,13,14,15)
data2<-c(20,21,22,23,24,25)
x<-c(1,2)
elevation<-c(10,11,20,24,25)
elevation<-data.table(elevation)

Where x[1] is the x parameter of data1 and data1 are the values at that location. Consider it mile 1 and mile 2. elevation is a sequence from the minimum of data1 to the maximum of data2.

I want to interpolate all the exceedance probabilities at mile 1.5. What I have tried is:

data1prob<-ecdf(data1)
data2prob<-ecdf(data2)
elevation[,prob:=1-as.numeric(approx(x = x, y =c(data1prob(elevation),data2prob(elevation)), xout = 1.5)[2])] 

I receive the error:

Error in .approxfun(x, y, v, method, yleft, yright, f) : (list) object cannot be coerced to type 'double'

Which is from the call data1prob(elevation). I believe the elevation vector is called instead of the individual elevation values for each row. I do not have any problem when I do elevation[,prob:=elevation-as.numeric(approx(x = x, y =c(5,6), xout = 1.5)[2])] , but I do when I try elevation[,prob:=data1prob(elevation)] for example.

Thanks for any input.

Jeff Tilton
  • 1,256
  • 1
  • 14
  • 28

1 Answers1

2

I'm pretty sure the OP is just misusing the vectorization of the approx function.

This works fine:

elevation[, prob := mapply(
    function(x,y1,y2) 1 - approx(x = x, y = c(y1,y2), xout = 1.5)[[2]],
    list(x),
    ecdf(data1)(elevation), 
    ecdf(data2)(elevation)
)]
#    elevation       prob
# 1:        10 0.91666667
# 2:        11 0.83333333
# 3:        20 0.41666667
# 4:        24 0.08333333
# 5:        25 0.00000000
Frank
  • 66,179
  • 8
  • 96
  • 180