"整合" R中的函数给出了错误的结果

时间:2016-01-23 18:15:19

标签: r integrate

我想将函数M2_11(如下所示)整合到x上,用于固定App.phptheta = c(2,0.8)c = 1.1a=c(1,1)

A = matrix(c(1/0.8,0.03,0.03,2/0.8),nrow=2,ncol=2)

R的积分函数给出以下结果

M2_11 = function(x, theta, c, a, A){
return((score1(x,theta)-a[1])^2* (weight(x, theta, c, a, A))^2 * f(x, theta))
}
  

0.0006459957,绝对误差< 4.5E-05

以另一种方式进行集成会产生相同的结果

theta = c(2,0.8)
c = 1.1
a=c(1,1)
A = matrix(c(1/0.8,0.03,0.03,2/0.8),nrow=2,ncol=2)
integrate(M2_11, lower = 1e-100, upper = 10 ,subdivisions = 10000, theta,c,a,A)
  

0.0006459957,绝对误差< 4.5E-05

然而,整合功能给出的结果显然是错误的:

fM2_11 = function(x){M2_11(x,theta,c,a,A)}
integrate(fM2_11, lower = 1e-100, upper = 10,subdivisions = 10000)

enter image description here

曲线下面积明显大于0.00066

我还使用循环检查结果

x = seq(1e-100,10,by=0.001)
integrand = sapply(x,fM2_11)

TRUE

10001

发生了什么事?

1 个答案:

答案 0 :(得分:1)

非常感谢Nicola。问题解决了。

integrate(Vectorize(fM2_11), lower = 1e-100, upper = 10 ,subdivisions = 10000) 
  

0.1588699,绝对误差< 1.7E-07

sum(integrand)*0.001 
  

0.1588705

永远不要指望答案如此简单!