title: optimize replaces NA with maximum value
body: <p>I am trying to optimize a value and I keep getting an error that says </p>
  
  <p><code>In optimize(my_func, 3:20, tol = 1, maximum = T, comparator = data$changeUP,  :NA/Inf replaced by maximum positive value</code> </p>
  
  <p>What <code>NA</code> value is it replacing? the <code>NA</code> in my calculated <code>xxx</code> value? How can I make it not do that?</p>
  
  <pre><code>data(ttrc)
  data&lt;-data.frame(Close=Cl(ttrc),changeUP=ifelse(Cl(ttrc)-Lag(Cl(ttrc))&gt;0,1,0))
  colnames(data)&lt;-c("Close","changeUP")
  ###################################################################################
  xxx&lt;-RSI(Cl(data),n=3) #hardcoded one specific value of MA in order to test the optimization of the cutoff
  xxx&lt;-lag(xxx)
  
  res&lt;-optimize(my_func,3:20,tol=1,maximum=T,comparator=data$changeUP,calculated_statistic=xxx)
  res$maximum
  res$objective
  
  my_func&lt;-function(i,comparator,calculated_statistic){
    qq&lt;-as.factor(calculated_statistic&lt;i)
    comparison&lt;-table(data$changeUP==qq,useNA = "no")
    comparison[2]/(comparison[1]+comparison[2])#calculate success rate for the cutoff
  }
  </code></pre>
  
  <p>My completed code using loops (still has an odd bug) but this is what I am eventually trying to do with the optimization.</p>
  
  <pre><code>data(ttrc)
  data&lt;-data.frame(Close=Cl(ttrc),changeUP=ifelse(Cl(ttrc)-Lag(Cl(ttrc))&gt;0,1,0))
  colnames(data)&lt;-c("Close","changeUP")
  ###################################################################################
  master_rsi&lt;-lapply(3:20,function(j){ #test every RSI period
    xxx&lt;-RSI(Cl(data),n=j)
    xxx&lt;-lag(xxx)
    cat("indicator parameter=",j,"\n")
  
    tempdf&lt;-lapply(10:90,function(i){ #test every cutoff point for the values
      qq&lt;-xxx&lt;i
      qq&lt;-as.factor(qq)
      comparison&lt;-table(data$changeUP==qq,useNA = "no")
      pp&lt;-comparison[2]/(comparison[1]+comparison[2])#calculate success rate for the cutoff
      cat(pp)
      pp
    })
  
    tempdf&lt;-unlist(tempdf)
    rp&lt;-paste0("MA period: ",j," success rate: ",max(tempdf)," cutoff: ",which.max(tempdf)+9,"\n")
    cat(rp)
    rp
  })
  unlist(master_rsi)
  </code></pre>