简体   繁体   English

牛顿Raphson代码,具有Diff函数的R错误

[英]Newton Raphson Code for R Error with Diff Function

I am having issues running my R code. 我在运行R代码时遇到问题。 I keep getting the error: Error in while (Diff > 1e-06) { : missing value where TRUE/FALSE needed 我不断收到错误消息:while while(Diff> 1e-06){时出错:需要TRUE / FALSE的缺少值

Here is the code I am using: 这是我正在使用的代码:

     Dose     with.tumor   total.animals
  1    0             3            50
  2  188             6            50
  3  375             6            50
  4  750            12            50

  X1=as.matrix(rep(1,4))
  X2a=as.matrix(c(0,188,375,750))
  X2=as.matrix(log(X2a))
  X=cbind(X1,X2)

a=1
b=1

B=matrix(c(a,b),nrow=2,ncol=1,byrow=F)

Diff=1
while(Diff>.000001){
P=(1-exp(-exp(a+b*X)))^-1
ni=c(rep(50,4))
V=diag(4)
for(i in 1:4)
{V[i,i]=diag(ni[i]%*%P[i]%*%(1-P[i]))}
R=c(3,6,6,12)
S=matrix(data=1,nrow=4,ncol=1)
for (i in 1:4)
{S[i]=R[i]-ni[i]*P[i]}

new.B=B+solve(t(X)%*%V%*%X)%*%(t(X)%*%S)
Diff=max(abs(new.B-B))
B=new.B}

Any insight would be appreciated. 任何见识将不胜感激。

You have NaN when computing new.B . 计算new.B时,您会遇到NaN Usually, this error pops up because you are comparing NaN with a number. 通常,会弹出此错误,因为您正在将NaN与数字进行比较。

Your t(X) has Inf as one of its elements. 您的t(X)具有Inf作为元素之一。 This arises from the fact that you are taking log(0) when you do X2a=as.matrix(c(0,188,375,750)) and then X2=as.matrix(log(X2a)) 这是由于您在执行X2a=as.matrix(c(0,188,375,750))然后X2=as.matrix(log(X2a))时正在获取log(0) X2=as.matrix(log(X2a))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM