错误:min(p,na.rm = TRUE)> = 0不为TRUE

时间:2013-12-05 04:54:55

标签: r

我在第32页遇到interesting presentation,我开始复制并理解提供的代码

演示文稿的代码如下:

#Unicredit banks code
library(evir)
library(fExtremes)
# Quantile function of lognormal-GPD severity distribution
qlnorm.gpd = function(p, theta, theta.gpd, u)
  {
    Fu = plnorm(u, meanlog=theta[1], sdlog=theta[2])
    x = ifelse(p<Fu,
               qlnorm( p=p, meanlog=theta[1], sdlog=theta[2] ),
               qgpd( p=(p - Fu) / (1 - Fu) , xi=theta.gpd[1], mu=theta.gpd[2], beta=theta.gpd[3]) )
  return(x)
  }
# Random sampling function of lognormal-GPD severity distribution

rlnorm.gpd = function(n, theta, theta.gpd, u)
  {
  r = qlnorm.gpd(runif(n), theta, theta.gpd, u)
  }

set.seed(1000)
nSim = 1000000 # Number of simulated annual losses
H = 1500 # Threshold body-tail
lambda = 791.7354 # Parameter of Poisson body
theta1 = 2.5 # Parameter mu of lognormal (body)
theta2 = 2 # Parameter sigma of lognormal (body)
theta1.tail = 0.5 # Shape parameter of GPD (tail)
theta2.tail = H # Location parameter of GPD (tail)
theta3.tail = 1000 # Scale parameter of GPD (tail)
sj = rep(0,nSim) # Annual loss distribution inizialization
freq = rpois(nSim, lambda) # Random sampling from Poisson
for(i in 1:nSim) # Convolution with Monte Carlo method
  sj[i] = sum(rlnorm.gpd(n=freq[i], theta=c(theta1,theta2), theta.gpd=c(theta1.tail, theta2.tail, theta3.tail), u=H))

但是我得到了这个我无法解决的错误:

Error: min(p, na.rm = TRUE) >= 0 is not TRUE

附录问题

非常感谢Shadow。

我不知道如何更改功能参考。是否像qgpd.fExtremes一样简单到qgpd.evir?

再次感谢Shadow再次指出这一点。 对于任何希望从不同的包中更改函数引用的人(在上面的例子中,从fExtremes到evir,就像添加evir :: :(函数)一样简单。

示例:

evir:::qgpd( p=(p - Fu) / (1 - Fu) , xi=theta.gpd[1], mu=theta.gpd[2], beta=theta.gpd[3]) )

1 个答案:

答案 0 :(得分:0)

这里出现错误的原因是包fExtremesevir都实现了函数qgpd的不同版本。在evir版本中,p可以小于0,而fExtremes包仅为qgpd实现p>=0

最简单的解决方法是将qgpd函数调用更改为evir:::qgpd

相关问题