当需要真/假时,在f0中出现错误

时间:2023-01-12 22:29:54

I am trying found power values with fzero function in R using with myfunction by the following way:

我试着用f0函数在R中查找幂值,用myfunction如下方法:

myfunction = function(delta,data,cv){
if(mean(data^delta)!=0)
y=cv-sd((data^delta),na.rm=TRUE)/mean((data^delta),na.rm=TRUE)
return(y)}
b=repmat(NaN,12,Nest)
for (m in 1:12) {
        if (m==1) 
                indDates=which(is.element(month, c(12, 1, 2)))
        else if (m==12) 
                indDates=which(is.element(month, c(11, 12, 1)))
        else 
                indDates=which(is.element(month, c(m-1, m, m+1)))
        cvO=apply(prO[indDates,],2,sd,na.rm=TRUE)/colMeans(prO[indDates,], na.rm=TRUE)
        for (i in 1:Nest) {
                if (!is.na(cvO[i]))  
                        b[m,i]=fzero(function(x) myfunction(x,abs(prM[indDates,i]),cvO[i]),1)
        }
}

But I get the following error message: Error in if (fb == 0) return(list(x = b, fval = fb)) : missing value where TRUE/FALSE needed I do not understand what the matter is and how I should fix it? Could someone help me?

但是我得到了如下的错误信息:if (fb = 0)返回(list(x = b, fval = fb):缺失值,当真/假需要时,我不明白是什么问题,该如何修复?有人能帮我吗?

1 个解决方案

#1


0  

Finaly, I could solve this matter by the following way:

最后,我可以通过以下方式来解决这个问题:

date=DatevecV(t1) % t1 are time values from netCDF file   
ndata=nrow(prO)
Nest=ncol(prO)
b=repmat(NaN,12,Nest) 
month=unlist(date[2])

varCoeficient = function(delta,data,cv){
y=cv-sd((data^delta),na.rm=TRUE)/mean((data^delta),na.rm=TRUE)
return(y)}

for (m in 1:12) {
         if (m==1) 
                indDates=which(is.element(month, c(12, 1, 2)))
          else if (m==12) 
                  indDates=which(is.element(month, c(11, 12, 1)))
          else 
                 indDates=which(is.element(month, c(m-1, m, m+1)))
          cvO=apply(prO[indDates,],2,sd,na.rm=TRUE)/colMeans(prO[indDates,], na.rm=TRUE)
 for (i in 1:Nest) {
   if (!is.na(cvO[i])) {
     bi <- try(fzero(function(x)
 varCoeficient(x,abs(prM[indDates,i]),cvO[i]),1),silent=T)
     if ("try-error" %in% class(bi)) {  # an error occurred
       b[m,i] <- NA
     } else {
       b[m,i] <- bi$x
     }
   }
 }
 }

#1


0  

Finaly, I could solve this matter by the following way:

最后,我可以通过以下方式来解决这个问题:

date=DatevecV(t1) % t1 are time values from netCDF file   
ndata=nrow(prO)
Nest=ncol(prO)
b=repmat(NaN,12,Nest) 
month=unlist(date[2])

varCoeficient = function(delta,data,cv){
y=cv-sd((data^delta),na.rm=TRUE)/mean((data^delta),na.rm=TRUE)
return(y)}

for (m in 1:12) {
         if (m==1) 
                indDates=which(is.element(month, c(12, 1, 2)))
          else if (m==12) 
                  indDates=which(is.element(month, c(11, 12, 1)))
          else 
                 indDates=which(is.element(month, c(m-1, m, m+1)))
          cvO=apply(prO[indDates,],2,sd,na.rm=TRUE)/colMeans(prO[indDates,], na.rm=TRUE)
 for (i in 1:Nest) {
   if (!is.na(cvO[i])) {
     bi <- try(fzero(function(x)
 varCoeficient(x,abs(prM[indDates,i]),cvO[i]),1),silent=T)
     if ("try-error" %in% class(bi)) {  # an error occurred
       b[m,i] <- NA
     } else {
       b[m,i] <- bi$x
     }
   }
 }
 }