Rilcon42 - 1 year ago 89
R Question

# optimize replaces NA with maximum value

I am trying to optimize a value and I keep getting an error that says

`In optimize(my_func, 3:20, tol = 1, maximum = T, comparator = data\$changeUP, :NA/Inf replaced by maximum positive value`

What
`NA`
value is it replacing? the
`NA`
in my calculated
`xxx`
value? How can I make it not do that?

``````data(ttrc)
data<-data.frame(Close=Cl(ttrc),changeUP=ifelse(Cl(ttrc)-Lag(Cl(ttrc))>0,1,0))
colnames(data)<-c("Close","changeUP")
###################################################################################
xxx<-RSI(Cl(data),n=3) #hardcoded one specific value of MA in order to test the optimization of the cutoff
xxx<-lag(xxx)

res<-optimize(my_func,3:20,tol=1,maximum=T,comparator=data\$changeUP,calculated_statistic=xxx)
res\$maximum
res\$objective

my_func<-function(i,comparator,calculated_statistic){
qq<-as.factor(calculated_statistic<i)
comparison<-table(data\$changeUP==qq,useNA = "no")
comparison[2]/(comparison[1]+comparison[2])#calculate success rate for the cutoff
}
``````

My completed code using loops (still has an odd bug) but this is what I am eventually trying to do with the optimization.

``````data(ttrc)
data<-data.frame(Close=Cl(ttrc),changeUP=ifelse(Cl(ttrc)-Lag(Cl(ttrc))>0,1,0))
colnames(data)<-c("Close","changeUP")
###################################################################################
master_rsi<-lapply(3:20,function(j){ #test every RSI period
xxx<-RSI(Cl(data),n=j)
xxx<-lag(xxx)
cat("indicator parameter=",j,"\n")

tempdf<-lapply(10:90,function(i){ #test every cutoff point for the values
qq<-xxx<i
qq<-as.factor(qq)
comparison<-table(data\$changeUP==qq,useNA = "no")
pp<-comparison[2]/(comparison[1]+comparison[2])#calculate success rate for the cutoff
cat(pp)
pp
})

tempdf<-unlist(tempdf)
rp<-paste0("MA period: ",j," success rate: ",max(tempdf)," cutoff: ",which.max(tempdf)+9,"\n")
cat(rp)
rp
})
unlist(master_rsi)
``````

It might be helpful to force `qq` and `data\$changeUP` to be factors with the same levels 0,1:

``````library(quantmod)
data(ttrc)
data<-data.frame(Close=Cl(ttrc),changeUP=ifelse(Cl(ttrc)-Lag(Cl(ttrc))>0,1,0))
colnames(data)<-c("Close","changeUP")
data\$changeUP <- factor(data\$changeUP, levels=0:1)
###################################################################################
fun <- function(j){ #test every RSI period
xxx <- RSI(Cl(data),n=j)
xxx <- lag(xxx)
cat("indicator parameter=",j,"\n")

tempdf <- lapply(10:90,function(i){ #test every cutoff point for the values
qq <- factor(as.numeric(xxx<i), levels=0:1)
comparison <- table(data\$changeUP==qq,useNA = "no")
pp <- comparison[2]/(comparison[1]+comparison[2])#calculate success rate for the cutoff
##cat(i, pp,"\n")
pp
})

tempdf <- unlist(tempdf)
rp <- paste0("MA period: ",j," success rate: ",max(tempdf)," cutoff: ",which.max(tempdf)+9,"\n")
cat(rp,"\n")
rp
}
master_rsi <- lapply(3:20,fun)
unlist(master_rsi)
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download