title: R: x 'probs' outside [0,1]
body: <p>I am using the R programming language.</p>
  <p>I am trying to follow the instructions from the &quot;optimization&quot; package in R (<a href="https://cran.r-project.org/web/packages/optimization/optimization.pdf" rel="nofollow noreferrer">https://cran.r-project.org/web/packages/optimization/optimization.pdf</a>)  and use functions from this package on a specific function.</p>
  <p>For this example, I first generate some random data:</p>
  <pre><code>#load libraries
  library(dplyr)
  
  
  # create some data for this example
  a1 = rnorm(1000,100,10)
  b1 = rnorm(1000,100,5)
  c1 = sample.int(1000, 1000, replace = TRUE)
  train_data = data.frame(a1,b1,c1)
  </code></pre>
  <p>From here, I define the function that I want to optimize (&quot;fitness&quot;). This function takes 7 inputs  and calculates a &quot;total&quot; mean (a single scalar value). The inputs required for this function are:</p>
  <ul>
  <li>&quot;random_1&quot; (between 80 and 120)</li>
  <li>&quot;random_2&quot; (between &quot;random_1&quot; and 120)</li>
  <li>&quot;random_3&quot; (between 85 and 120)</li>
  <li>&quot;random_4&quot; (between random_2 and 120)</li>
  <li>&quot;split_1&quot; (between 0 and 1)</li>
  <li>&quot;split_2&quot; (between 0 and 1)</li>
  <li>&quot;split_3&quot; (between 0 and 1 )</li>
  </ul>
  <p>The function to optimize is defined as follows:</p>
  <pre><code>library(optimization)
  
  fitness &lt;- function(x) {
    #bin data according to random criteria
    train_data &lt;- train_data %&gt;% 
                   mutate(cat = ifelse(a1 &lt;= x[1] &amp; b1 &lt;= x[3], &quot;a&quot;, 
                                 ifelse(a1 &lt;= x[2] &amp; b1 &lt;= x[4], &quot;b&quot;, &quot;c&quot;)))
  
   train_data$cat = as.factor(train_data$cat)
      
      #new splits
      a_table = train_data %&gt;%
          filter(cat == &quot;a&quot;) %&gt;%
          select(a1, b1, c1, cat)
      
      b_table = train_data %&gt;%
          filter(cat == &quot;b&quot;) %&gt;%
          select(a1, b1, c1, cat)
      
      c_table = train_data %&gt;%
          filter(cat == &quot;c&quot;) %&gt;%
          select(a1, b1, c1, cat)
      
     
      
      #calculate  quantile (&quot;quant&quot;) for each bin
      
      table_a = data.frame(a_table%&gt;% group_by(cat) %&gt;%
                               mutate(quant = quantile(c1, prob = x[5])))
      
      table_b = data.frame(b_table%&gt;% group_by(cat) %&gt;%
                               mutate(quant = quantile(c1, prob = x[6])))
      
      table_c = data.frame(c_table%&gt;% group_by(cat) %&gt;%
                               mutate(quant = quantile(c1, prob = x[7])))
      
      
      
      
      #create a new variable (&quot;diff&quot;) that measures if the quantile is bigger tha the value of &quot;c1&quot;
      table_a$diff = ifelse(table_a$quant &gt; table_a$c1,1,0)
      table_b$diff = ifelse(table_b$quant &gt; table_b$c1,1,0)
      table_c$diff = ifelse(table_c$quant &gt; table_c$c1,1,0)
      
      #group all tables
      
      final_table = rbind(table_a, table_b, table_c)
  # calculate the total mean : this is what needs to be optimized
      mean = mean(final_table$diff)
      
      
  }
  </code></pre>
  <p>From here, I am trying to run the following optimization function:</p>
  <pre><code>Output &lt;- optim_nm(fitness, k = 7, trace = TRUE)
  
  plot(output)
   plot(Output, 'contour')
  </code></pre>
  <p>But these return the following errors:</p>
  <pre><code> Error: Problem with `mutate()` column `quant`.
  i `quant = quantile(c1, prob = x[6])`.
  x 'probs' outside [0,1]
  Run `rlang::last_error()` to see where the error occurred. 
  
  Error in plot(Output) : object 'Output' not found
  </code></pre>
  <p>I think error is that the &quot;split_1&quot;, &quot;split_2&quot; and &quot;split_3&quot; variables are being assigned values outside of 0 and 1 : since the function is using these variables are used to calculate percentiles (e.g. <code>quant = quantile(c1, prob = x[5]</code> ) , this will naturally result in an error?</p>
  <p>I tried to use another optimization algorithm from this package where the ranges for these 7 inputs are explicitly defined, but this is also producing the same error :</p>
  <pre><code>ro_sa &lt;- optim_sa(fun = fitness,
  start = c(runif(7, min = -1, max = 1)),
  lower = c(80,80,80,80,0,0,0),
  upper = c(120,120,120,120,1,1,1),
  trace = TRUE,
  control = list(t0 = 100,
  nlimit = 550,
  t_min = 0.1,
  dyn_rf = FALSE,
  rf = 1,
  r = 0.7
  )
  )
  
   Error: Problem with `mutate()` column `quant`.
  i `quant = quantile(c1, prob = x[6])`.
  x 'probs' outside [0,1]
  </code></pre>
  <p>This also doesn't work if you provide an initial starting point:</p>
  <pre><code>optim_nm(fitness, start = c(80,80,80,80,0.5,0.6,0.7))
  
   Error: Problem with `mutate()` column `quant`.
  i `quant = quantile(c1, prob = x[5])`.
  x 'probs' outside [0,1]
  i The error occurred in group 1: cat = a.
  </code></pre>
  <p><strong>Question:</strong> Can someone please show me how to fix this, so that I can run the optimization functions?</p>
  <pre><code>#desired functions to run:
  
  Output &lt;- optim_nm(fitness, k = 7, trace = TRUE)
  
  plot(output)
   plot(Output, 'contour')
  
  ro_sa &lt;- optim_sa(fun = fitness,
  start = c(runif(7, min = -1, max = 1)),
  lower = c(80,80,80,80,0,0,0),
  upper = c(120,120,120,120,1,1,1),
  trace = TRUE,
  control = list(t0 = 100,
  nlimit = 550,
  t_min = 0.1,
  dyn_rf = FALSE,
  rf = 1,
  r = 0.7
  )
  )
  
  optim_nm(fitness, start = c(80,80,80,80,0.5,0.6,0.7))
  </code></pre>
  <p>Thanks</p>