我正在尝试计算肯德尔的tau给出的Frank copula参数。这是我试图在R中以数字方式解决的问题:
到目前为止,这是我的代码:
copula <- function(tau, method = c("clayton", "gumbel", "frank")){
if(method == "clayton"){
tmp <- (2*tau)/(1-tau)
} else if(method == "gumbel"){
tmp <- 1/(1-tau)
} else if(method == "frank"){
integrand <- function(t) {t/(exp(t)-1)}
frank_fn <- function(theta) {(((tau - 1)/4) - (((integrate(integrand, 0, theta)[1]/theta) - 1)/theta))^2}
tmp <- optim(frank_fn, 4, method = "BFGS")
} else{
tmp <- "Error: Choice of method is undefined."
}
return(tmp)
}
此功能适用于clayton和gumbel但不坦率。它会引发以下错误:
Error in optim(frank_fn, 4, method = "BFGS") :
cannot coerce type 'closure' to vector of type 'double'
有人可以帮助解释我的代码有什么问题吗?我对R中的数值优化问题相当新。
更新:
正确的代码如下:
copula <- function(tau, method = c("clayton", "gumbel", "frank")){
if(method == "clayton"){
tmp <- (2*tau)/(1-tau)
} else if(method == "gumbel"){
tmp <- 1/(1-tau)
} else if(method == "frank"){
integrand <- function(t) {t/(exp(t)-1)}
frank_fn <- function(theta) {(((tau - 1)/4) - ((((integrate(integrand, 0, theta)$value)/theta) - 1)/theta))^2}
tmp <- optim(4, frank_fn, method = "BFGS")
} else{
tmp <- "Error: Choice of method is undefined."
}
return(tmp)
}
答案 0 :(得分:0)
您在集成方面需要$ value吗?
copula <- function(tau, method = c("clayton", "gumbel", "frank")){
if(method == "clayton"){
tmp <- (2*tau)/(1-tau)
} else if(method == "gumbel"){
tmp <- 1/(1-tau)
} else if(method == "frank"){
integrand <- function(t) {t/(exp(t)-1)}
frank_fn <- function(theta) {(((tau - 1)/4) - (((integrate(integrand, 0, theta)[1]/theta)$value - 1)/theta))^2}
tmp <- optim(frank_fn, 4, method = "BFGS")
} else{
tmp <- "Error: Choice of method is undefined."
}
return(tmp)
}
copula(30,method='clayton')