[R] error: Error in if (is.na(f0$objective)) { : argument is of length zero

jim holtman jholtman at gmail.com
Mon Feb 18 22:44:26 CET 2013


Following up on what Bert said, also put

options(error=utils::recover)

at the start of the script so that on errors you get the traceback
printed out and the you can also use the browser to look at the values
of the objects at the point of the error.  This is debugging 101.

On Mon, Feb 18, 2013 at 4:28 PM, Aya Anas <aanas at feps.edu.eg> wrote:
> Dear all,
>
> I tried running the following syntax but it keeps running for about 4 hours
> and then i got the following errors:
>
> Error in if (is.na(f0$objective)) { : argument is of length zero
> In addition: Warning message:
> In is.na(f0$objective) :
>   is.na() applied to non-(list or vector) of type 'NULL'
>
> Here is the syntax itself:
>
> library('nloptr')
> library('pracma')
> # objective function
> f <- function(x,s) {m<-100
> t<-2*m+1
> H<-matrix(data=NA,nrow=t,ncol=t)
> I<-diag(t)
> delta<-2*x[1]/t
> z<--x[1]+.5*delta
> range1<-t
> for (i in 1:range1){
> z_i=-x[1]+(i-.5)*delta
> for (j in 1:range1){
>               up<-(((-x[1]+j*delta-(1-x[2])*z_i)/x[2]))-s
>               down<-(((-x[1]+(j-1)*delta-(1-x[2])*z_i)/x[2]))-s
>               H[i,j]<-pnorm(up,mean=0,sd=1)-pnorm(down,mean=0,sd=1)}
> }
>
> K<-solve(I-H)
> one <- matrix(1,nrow=t,ncol=1)
> u <- matrix(0,nrow=m,ncol=1)
> y<-c(u, 1, u)
> z<-t(y)
> ARLV<-K%*%one
> ARLV2<-t(ARLV)
>     return((ARLV2%*%y)/3)
> }
> # constraint function
> eval_g0 <- function(x) {m1<-100
> t1<-2*m1+1
> H1<-matrix(data=NA,nrow=t1,ncol=t1)
> I1<-diag(t1)
> delta1<-2*x[1]/t1
> z1<--x[1]+.5*delta1
> range11<-t1
> for (i1 in 1:range11){
> z_i1=-x[1]+(i1-.5)*delta1
> for (j1 in 1:range11){
>               up1<-((-x[1]+j1*delta1-(1-x[2])*z_i1)/x[2])
>               down1<-((-x[1]+(j1-1)*delta1-(1-x[2])*z_i1)/x[2])
>               H1[i1,j1]<-pnorm(up1,mean=5,sd=1)-pnorm(down1,mean=5,sd=1)}
> }
>
> K1<-solve(I1-H1)
> one1 <- matrix(1,nrow=t1,ncol=1)
> u1 <- matrix(0,nrow=m1,ncol=1)
> y1<-c(u1, 1, u1)
> z1<-t(y1)
> ARLV1<-K1%*%one1
> ARLV21<-t(ARLV1)
>
>     return( abs((ARLV21%*%y1)-500)-1)
> }
> eval_f0 <- function(x) romberg(function(s) f(x, s), 0, 6)
> of <- nloptr( x0=c(0.653,0.09),
>                 eval_f=eval_f0,
>                 lb = c(0,0),
>                 ub = c(6,1),
>                 eval_g_ineq = eval_g0,
>                 opts = list("algorithm"="NLOPT_LN_COBYLA", "maxeval"=1000),
>                                 )
> print(of)
>
> Regards,
> Aya
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.



More information about the R-help mailing list