对于以下R代码,我在执行绘制其曲线的最后一个代码行时收到错误Error in (k - 3) * h1 : non-numeric argument to binary operator
。什么是可能的解决方案?
k = 4
g4 = function(theta) {
h1 = function(x) { dchisq(x,df=k+1,ncp=theta)/x }
h4 = function(x) { dchisq(x,df=k+3,ncp=theta)/x }
(k-1) * integrate((1-(k-3) * h1)^2, 0, k-3)$value +
theta * (2 * integrate((1-(k-3) * h1), 0, k-3)$value -
integrate((1-(k-3) * h4)^2, 0, k-3)$value)
}
RPS = function(theta) { sapply(theta,g4) }
curve(RPS, from=0, to=20, lwd=2, lty=1, xlab="theta", ylab="AQR", ylim=c(0,8))
虽然以下代码效果很好
k = 4
g3 = function(theta){
h1 = function(x) { dchisq(x,df=k+1,ncp=theta)/x }
h2 = function(x){ dchisq(x,df=k+1,ncp=theta)/(x^2) }
h3 = function(x){ dchisq(x,df=k+3,ncp=theta)/(x^2) }
k+(theta*(k-3)*(k+1)*integrate(h3,0,Inf)$value)-(k-1)*(k-3)*(2*integrate(h1,0,Inf)$value-(k-3)*integrate(h2,0,Inf)$value)
}
RJS=function(theta){ sapply(theta,g3) }
curve(RJS, from=0, to=20, lwd=2, lty=1, xlab="theta", ylab="AQR", ylim=c(0,8))
我获得了所需的曲线但不是第一个代码。