【发布时间】:2020-04-17 11:13:35
【问题描述】:
我正在尝试使用生成的数据集计算一种基尼指数。 但是,我在最后一个集成功能中遇到了问题。 如果我尝试集成名为 f1 的函数, R 说
Error in integrate(Q, 0, p) : length(upper) == 1 is not TRUE
我的代码是
# set up parameters b>a>1 and the number of observations n
n <- 1000
a <- 2
b <- 4
# generate x and y
# where x follows beta distribution
# y = 10x+3
x <- rbeta(n,a,b)
y <- 10*x+3
# the starting point of the integration having problem
Q <- function(q) {
quantile(y,q)
}
# integrate the function Q from 0 to p
G <- function(p) {
integrate(Q,0,p)
}
# compute a function
L <- function(p) {
numer <- G(p)$value
dino <- G(1)$value
numer/dino
}
# the part having problem
d <- 3
f1 <- function(p) {
((1-p)^(d-2))*L(p)
}
integrate(f1,0,1) # In this integration, the aforementioned error appears
我认为,重复集成可能会产生问题,但我不知道确切的问题是什么。 请帮帮我!
【问题讨论】:
-
f1(0.1)(例如)会引发错误,因此尝试集成本身需要调试的函数似乎有点奇怪。L(0.1)也会引发错误。为什么不在尝试定义使用它的其他函数之前调试一个函数? -
这是一个很奇怪的问题。
Q似乎在整合时表现得病态,尽管curve(Q(x),0,1)显示了一个相当温和的图表。我认为默认的quantile函数给出了顺序统计的分段线性插值,所以这种行为看起来很奇怪,甚至可能是其中一个 R 函数中的错误。 -
玩弄这个,如果我注释掉你的最后一行,源它,并评估
integrate(Q,0,1)大约三分之一的时间我收到错误消息Error in integrate(Q, 0, 1) : maximum number of subdivisions reached但其他时候我源它该错误不会发生。