Nai-Wei Chen
2010-Feb-18 17:05 UTC
[R] an error about " return some vectors from some functions within a function"
Dear all, When I try to return some vectors from some functions within a function, it indicate an error," Error in rbind(ck1, ck2, ck3) : object 'ck1' not found", in one of the iterations and stop. Since I am not experienced in programming, can anyone give me a suggestion to inspect this error? The followings are the functions I created : ################### # functions in the convg # ################### check1 <- function(sumgt,beta1.0,gamma.0,sigma.0){ if (any(!is.finite(sumgt))){ count1 <- count1+1 return(c(count1,beta1.0,gamma.0,sigma.0)) } else {return(c(NaN,NaN,NaN,NaN))} } check2 <- function(v0,maxit,iter,beta1.0,gamma.0,sigma.0){ if (is.nan(sum(v0))==TRUE | any(!is.finite(v0)) | maxit == iter){ count1 <- count1+1 return(c(count1,beta1.0,gamma.0,sigma.0)) } else {return(c(NaN,NaN,NaN,NaN))} } check3 <- function(maxit,diff,error,beta1.0,gamma.0,sigma.0){ if (diff < error) { return(c(count,beta1.0,gamma.0,sigma.0)) } else {return(c(NaN,NaN,NaN,NaN))} } convg <- function(count1,count,sub,rep,n,data1,beta1.0,gamma.0,sigma.0,v0,L,diff,error,iter,maxit){ while(diff > error && maxit < iter && max(abs(c(beta1.0,gamma.0,sigma.0))) < 10 && is.nan(sum(v0))==FALSE && any(is.finite(v0))){ : : ck1 <- check1(sumgt,beta1.0,gamma.0,sigma.0) : : ck2 <- check2(v0,maxit,iter,beta1.0,gamma.0,sigma.0) : : ck3 <- check3(maxit,diff,error,beta1.0,gamma.0,sigma.0) } return(rbind(ck1,ck2,ck3)) } Thank you so much Sincerely, Joe [[alternative HTML version deleted]]
jim holtman
2010-Feb-18 18:12 UTC
[R] an error about " return some vectors from some functions within a function"
Might be the case that the 'while' loop was not executed and therefore 'ck1' was not defined. You might put a check to see if that was happening. Also you are incrementing 'count1' in the functions and it is not being passed in as a parameter. What are you expecting it to do? Is it defined in the global workspace? The incrementing of 'count1' is local to the function. Are you expecting it to count the number of times things happen. If so, you need to do: count1 <<- count1 + 1 but be advised "<<-" is not for novices if you do not understand the scoping rules of R. On Thu, Feb 18, 2010 at 12:05 PM, Nai-Wei Chen <s90225007 at yahoo.com.tw> wrote:> Dear all, > > When I?try to return?some vectors from some functions within a function,?it indicate an error,"?Error in rbind(ck1, ck2, ck3) : object 'ck1' not found", in one of the iterations and stop.? Since I am not experienced in programming, can anyone give me a suggestion to inspect this error? > The followings are the functions I created : > > ################### > # functions in the convg # > ################### > check1 <- function(sumgt,beta1.0,gamma.0,sigma.0){ > ?? if (any(!is.finite(sumgt))){ > ?????? count1 <- count1+1 > ?????? return(c(count1,beta1.0,gamma.0,sigma.0)) > ?? } > ? else {return(c(NaN,NaN,NaN,NaN))} > ?} > check2 <- function(v0,maxit,iter,beta1.0,gamma.0,sigma.0){ > ??? if (is.nan(sum(v0))==TRUE | any(!is.finite(v0)) | maxit == iter){ > ?????? count1 <- count1+1 > ?????? return(c(count1,beta1.0,gamma.0,sigma.0)) > ?? } > ?? else {return(c(NaN,NaN,NaN,NaN))} > ?} > check3 <- function(maxit,diff,error,beta1.0,gamma.0,sigma.0){ > ??? if (diff < error) { > ?????? return(c(count,beta1.0,gamma.0,sigma.0)) > ?? } > ?? else {return(c(NaN,NaN,NaN,NaN))} > ?} > > convg <- function(count1,count,sub,rep,n,data1,beta1.0,gamma.0,sigma.0,v0,L,diff,error,iter,maxit){ > > ?while(diff > error && maxit < iter && max(abs(c(beta1.0,gamma.0,sigma.0))) < 10 && is.nan(sum(v0))==FALSE && any(is.finite(v0))){ > ?????????? : > ?????????? : > ?ck1 <- check1(sumgt,beta1.0,gamma.0,sigma.0) > ?????????? : > ?????????? : > ?ck2 <- check2(v0,maxit,iter,beta1.0,gamma.0,sigma.0) > ?????????? : > ?????????? : > ?ck3 <- check3(maxit,diff,error,beta1.0,gamma.0,sigma.0) > > } > return(rbind(ck1,ck2,ck3)) > } > > Thank you so much > > > Sincerely, > > > Joe > > > > ? ? ? ?[[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 Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?