【问题标题】:Error in integrate(f): evaluation of function gave a result of wrong length积分(f)中的错误:函数评估给出了错误长度的结果
【发布时间】:2013-04-15 13:00:43
【问题描述】:

我正在使用 R 进行一些多变量分析。对于这项工作,我需要集成三元 PDF。执行此操作时出现错误:

Error in integrate(Vectorize(function(x) { : 
  evaluation of function gave a result of wrong length

如您所见,矢量化不是解决方案,因此出现了问题。

如果有人能告诉我发生了什么,我将非常感激。

这是我的代码:

library(mvtnorm)
f1<-function(x, y, z) {dmvnorm(x=as.matrix(cbind(x,y,z)), mean=mu.t, sigma)} #PDF with all three
#integrate out x
integrate(Vectorize(function(x) {f1(x=c1, y=c2, z=c3)}), 0.1,0.5, rel.tol=1.5e-20)$value

【问题讨论】:

标签: r


【解决方案1】:

如果没有关于您的输入和输出的更多信息,无法确定,但是f1 的输出中的length 是什么?如果它与您的输入长度不同,我相信这会导致您遇到错误,如 integrate 的帮助页面:

f   an R function taking a numeric first argument and returning a numeric vector of the same length. Returning a non-finite element will generate an error.

【讨论】:

  • 我想我知道出了什么问题。 f1 输出是 x (n) 的 nrow 的长度,我想要一个相同长度的向量。我这样做了: for(i in 1:n) { f1=function(x, y, z) {dmvnorm(x=as.matrix(cbind(x,y,z)), mean=mu.t, sigma= sigma)} #积分出 x 积分(Vectorize(function(x) {f1(x=c1[i], y=c2[i], z=c3[i])}), lower = -Inf, upper = - xb1[i], rel.tol=1.e-06)$value} }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多