Search code examples
rmessageregularized

R problem with regularize.values(x, y, ties, missing(ties)


I would like to ask a question

My code in R:

a=1.1
b=1.3


C=c(0,0.3,1.5,3.5,19.5)
v2=c(0.00, 0.56, 1.00, 1.00, 1.00) 


fun <- 
  function( C, a, b ){
    1 / (1 + (C / a)^b) 
  }


x_d=seq(0,10,by=0.1)
y_s <- fun(v2, a, b)  
y_d <- splinefun(v2, y_s)(x_d)
y_d <- approx(v2, y_s, xout = x_d)$x
which(y_d <= 0.5)
which(y_d <= 0.5)[1]
x_d[which(y_d <= 0.5)[1]]

I'm getting a warning messages:

 y_d <- splinefun(v2, y_s)(x_d)
Warning message:
In regularize.values(x, y, ties, missing(ties)) :
  collapsing to unique 'x' values
> y_d<- approx(v2, y_s, xout = x_d)$x
Warning message:
In regularize.values(x, y, ties, missing(ties), na.rm = na.rm) :
  collapsing to unique 'x' values
> which(y_d <= 0.5)
[1] 11 12 13 14 15 16 17 18
> which(y_d <= 0.5)[1]
[1] 11
> x_d[which(y_d <= 0.5)[1]]
[1] 1

I would like to improve my code so that I don't get such a warning messages. It ruins the results for me. However, I wouldn't want to remove the duplicate values of v2 and its results, I need them later.

I do not hide that it is important for me to find a way where I can use non distinct values for v2 and treat them as if they were unique and continue to use them in my code. Is there such a way in R? Thank you in advance for any help with this problem. I am quite new to R so please help me


Solution

  • First, there is a problem with your function fun(). It only has one argument (variables), but you feed it more than one object (v2, a, b). But if I fix it to the below, I can reproduce your error message:

    fun <- 
      function( C, a, b ){
      1 / (1 + (C / a)^b) 
    }
    

    If you simply want to avoid the warning messages, you can use ties = "ordered" or ties = mean with functions splinefun() and approx(). Please read here under "Details" if ties = "ordered", ties = mean, or something else is best for your specific programming goal.

    a=1.1
    b=1.3
    C=c(0,0.3,1.5,3.5,19.5)
    v2=c(0.00, 0.56, 1.00, 1.00, 1.00) 
    
    fun <- 
      function( C, a, b ){
        1 / (1 + (C / a)^b) 
      }
    
    ## no ties specified
    x_d=seq(0,10,by=0.1)
    y_s <- fun(v2, a, b)  
    y_d <- splinefun(v2, y_s)(x_d)
    #> Warning in regularize.values(x, y, ties, missing(ties)): collapsing to unique
    #> 'x' values
    y_d <- approx(v2, y_s, xout = x_d)$x
    #> Warning in regularize.values(x, y, ties, missing(ties), na.rm = na.rm):
    #> collapsing to unique 'x' values
    which(y_d <= 0.5)
    #> [1] 1 2 3 4 5 6
    which(y_d <= 0.5)[1]
    #> [1] 1
    x_d[which(y_d <= 0.5)[1]]
    #> [1] 0
    rm(x_d, y_s, y_d)
    
    ## ties specified
    x_d=seq(0,10,by=0.1)
    y_s <- fun(v2, a, b)  
    y_d <- splinefun(v2, y_s, ties = "ordered")(x_d)
    y_d <- approx(v2, y_s, xout = x_d, ties = "ordered")$x
    which(y_d <= 0.5)
    #> [1] 1 2 3 4 5 6
    which(y_d <= 0.5)[1]
    #> [1] 1
    x_d[which(y_d <= 0.5)[1]]
    #> [1] 0