给定函数的分位数 R 代码错误?
Quantile from given function R Code Error?
我正在使用 R 从给定函数中获取分位数,该函数给出的错误低于我不太明白它的含义。尽管错误消息似乎不言自明,但这个 R Noob 无法解决。任何帮助表示赞赏。
错误:
Error in cum(x) - p : non-numeric argument to binary operator
代码如下:
library(nleqslv)
dens<-function(x)
{
(dbeta(x, shape1 = 1, shape2 = 5) + dbeta(x, shape1 = 3, shape2 = 5) + dbeta(x, shape1 = 10,
shape2 = 5))*1/3
}
cum<-function(x)
{
integrate(dens,-Inf,x)
}
quant<-function(p)
{
nleqslv(0, function(x){cum(x)-p})
}
print(quant(0.50))
integrate()
不是 return 一个数字,而是一个列表:
names(cum(1))
## [1] "value" "abs.error" "subdivisions" "message" "call"
您只需要 $value
元素:
quant <- function(p) {
nleqslv(0, function(x) { cum(x)$value-p })
}
(既然你要加起来 dbeta()
值,也许你的积分下限可以是 0 而不是 -Inf
?)
我正在使用 R 从给定函数中获取分位数,该函数给出的错误低于我不太明白它的含义。尽管错误消息似乎不言自明,但这个 R Noob 无法解决。任何帮助表示赞赏。
错误:
Error in cum(x) - p : non-numeric argument to binary operator
代码如下:
library(nleqslv)
dens<-function(x)
{
(dbeta(x, shape1 = 1, shape2 = 5) + dbeta(x, shape1 = 3, shape2 = 5) + dbeta(x, shape1 = 10,
shape2 = 5))*1/3
}
cum<-function(x)
{
integrate(dens,-Inf,x)
}
quant<-function(p)
{
nleqslv(0, function(x){cum(x)-p})
}
print(quant(0.50))
integrate()
不是 return 一个数字,而是一个列表:
names(cum(1))
## [1] "value" "abs.error" "subdivisions" "message" "call"
您只需要 $value
元素:
quant <- function(p) {
nleqslv(0, function(x) { cum(x)$value-p })
}
(既然你要加起来 dbeta()
值,也许你的积分下限可以是 0 而不是 -Inf
?)