【问题标题】:Error in fzero in R missing value where TRUE/FALSE needed需要 TRUE/FALSE 的 R 缺失值中的 fzero 错误
【发布时间】:2016-03-29 08:06:59
【问题描述】:

我正在尝试通过以下方式使用 myfunction 在 R 中使用 fzero 函数查找功率值:

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)
        }
}

但我收到以下错误消息: if (fb == 0) return(list(x = b, fval = fb)) 中的错误: 需要 TRUE/FALSE 的缺失值 我不明白这是怎么回事,我应该如何解决?有人可以帮我吗?

【问题讨论】:

  • 这个fzero函数来自哪个包?
  • 我使用了 {pracma} 包。
  • 您的代码显然包含一个对象 Nest,该对象未在您的示例中定义。请确保示例完全可重现。
  • 对不起。巢=ncol(prO)。 prO 是一个二维矩阵,其中 nrow 是 30 年(1971-2000)的天数

标签: r


【解决方案1】:

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

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
     }
   }
 }
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 2022-06-22
    • 2015-01-17
    • 2016-11-06
    • 1970-01-01
    • 2018-07-12
    相关资源
    最近更新 更多