我有一个简单的危险函数,标记了导致错误的行。
h <- function(t,u) {
x <- 1 - Sa(t)
y <- 1 - Sm(u)
invx <- as.numeric(qt(x,df=d1))
invy <- as.numeric(qt(x,df=d1))
[ERROR LINE] copula <- pmvt(lower=as.numeric(cbind(-9999,-9999)),upper=cbind(invx,invy),df=d1,corr=matrix(cbind(1,d2,d2,1),byrow=T,ncol=2) )
density <- dmvt(cbind(invx,invy),sigma=matrix(cbind(1,d2,d2,1),byrow=T,ncol=2),df=d1)
num <- (sa(t)*sm(u))*density/dt(invx,df=d1)/dt(invy,df=d1)
den <- 1 - x - y + copula
hazard <- num/den
return(hazard)
}
然后通过可能性函数调用此危险函数:
# log Likelihood function for each individual car i
lli <- function(data) {
result <- 0;
# for all claims, evaluate hazard function at that point
if (nrow(data)> 2) {
for (k in 1:nrow(data)) {
if (data[k,3] == 1) {
result <- result + log(h(data[k,2],data[k,1]));
}
}
}
# integrate hazard function over areas between claims
for (k in 1:(nrow(data)-1)) {
integral <- quad2d(h,data[k,2],data[k+1,2],data[k,1],data[k+1,1]);
result <- result - integral;
}
return(result)
}
现在,此可能性函数由第三个函数调用,以在我的整个数据集中使用。 但是导致错误的是上述函数,而不是下面的函数
# log Likelihood function over all vehicles
ll <- function(x) {
# Unpack parameters
d1 <<- x[1];
d2 <<- x[2];
total <- 0;
# Get log Likelihood for each vehicle
for (i in 1:length(alldata)) {
total <- total + lli(alldata[[i]]);
#print(sprintf("Found candidate solution %d value: %f",i,total));
}
#print(sprintf("Found candidate solution value: %f",total));
if (is.nan(total)) { #If it is undefined, make it a large negative number
total <- -2147483647 ;
}
return(-1*total); # Minimise instead of maximise
}
错误信息如下:
> ll(cbind(50,0.923))
Error in checkmvArgs(lower = lower, upper = upper, mean = delta, corr = corr, :
‘diag(corr)’ and ‘lower’ are of different length
使用pmvnorm时,我一直遇到相同的错误,最终不得不使用pbivnorm软件包来解决此问题。我找不到二元t分布的替代软件包。我不明白问题是什么。当我自己调用函数h(t,u)时,它执行就没有问题。但是,当lli(data)调用h(t,u)时,它不起作用。更奇怪的是它们的长度相同。
> length(as.numeric(cbind(-9999,-9999)))
[1] 2
> length(diag(matrix(cbind(1,d2,d2,1),byrow=T,ncol=2)))
[1] 2
我为乱码表示歉意。我不太用R。无论如何,这让我完全陷入了困境。
数据文件在这里:https://files.fm/u/yx9pw2b3
我忘记包括的其他代码基本上包括一些常量和边际CDF函数:
边际 yield :
p1 <- 0.4994485;
p2 <- 0.2344439;
p3 <- 0.1151654;
p4 <- 0.1509421;
b1 <- 0.7044292
t1 <- 1713.3170267
mu1 <- 7.014415
sig1 <- 1.394735
mu2 <- 6.926146
sig2 <- 1.056647
mu3 <- 6.7995896
sig3 <- 0.7212853
b2 <- 0.6444582
t2 <- 762.9962093
b3 <- 1.494303
t3 <- 410.828780
b1 <- 0.903
t1 <- 864.896
b2 <- 0.9109
t2 <- 314.2946
# Marginal survival distribution and density
Sa <- function(t) {return(exp(-(t / t1) ** b1))}
Sm <- function(u) {return(exp(-(u / t2) ** b2))}
sa <- function(t) {return((t / t1) ** b1 * b1 * exp(-(t / t1) ** b1) / t ) }
sm <- function(u) {return((u / t2) ** b2 * b2 * exp(-(u / t2) ** b2) / u ) }
最佳答案
摘要:
问题是调用lower
时upper
和pvmt
之间的长度差是多少,该upper
的长度为2048,而lower
的长度为2。
推理:
1. pmvt
通过调用checkmvArgs
包中的mvtnorm
来检查即将到来的参数。
2.在checkmvArgs
中,lower
将upper
,mean
和rec <- cbind(lower, upper, mean)
放在一起。在这里,新数据rec
具有2048行而不是2行。
3.然后将lower
替换为lower <- rec[, "lower"]
,现在lower
的长度为2048而不是2。
4.给定corr
仍然是2 * 2矩阵,检查length(corr) != length(lower)
时发生错误
解决方案:
invx <- as.numeric(qt(x,df=d1))
invy <- as.numeric(qt(x,df=d1))
upper
的意思是长度为2的 vector ,因此invx
和invy
必须是单个数字。由于不确定要定义的上限是多少,我无法进一步解决。可能的一个是:
invx <- as.numeric(qt(x,df=d1))
invy <- as.numeric(qt(x,df=d1))
copula <- pmvt(lower=as.numeric(cbind(-9999,-9999)),upper=range(c(invx,invy)),df=d1,corr=matrix(c(1,d2,d2,1),byrow=T,ncol=2) )
使用
invx
和invy
的范围作为输入。因此,dmvt
不会受到影响。注意:
由于未提供值
a
,因此错误行下面的下一行(称为dmvt
)失败。编辑:
要使问题更具体:
1.
quad2d
将生成一个Gauss-Legendre正交,默认情况下将在给定范围内将其创建为32。和,2.然后从该Gauss-Legendre正交函数中使用x和y调用函数
h
。因此,在t
中定义的u
和h
不是单个成员,而是一个 vector 。
关于r - 不确定是什么导致R中的此错误(pmvt-软件包: mvtnorm)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42101885/