[英]Error in fzero in R missing value where TRUE/FALSE needed
I am trying to find the power values with fzero function in R using myfunction in the following way: 我正在尝试通过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)
}
}
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 is the matter and how I should fix it? 但是我收到以下错误消息:if(fb == 0)return(list(x = b,fval = fb))中的错误:缺少需要TRUE / FALSE的值,我不明白这是怎么回事,我该怎么办修理它? Could someone please help me?
有人可以帮我吗?
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
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.