I tried to replicate this demo from here, but get this error:
Error in calcKTau[[j]]: the object of type 'closure' is not subsettable.
I used chatGpt to fix the error, but it also did not work. I read some posts similar to my issue but cannot fix mine.
I understand it, but I do not know how to fix it.
Here is the complete code:
`library("VineCopula")
library("spcopula")
data("EU_RB_2005")
## spatio-temporal copula
# binning, using only 90 out of all temporal instances
stBins <- calcBins(EU_RB_2005, "PM10", nbins=40, t.lags=-(0:4),
instances=10)
#
calcKTau <- fitCorFun(stBins, rep(3, 5), tlags=-(0:4))
families <- c(normalCopula(0), tCopula(0), claytonCopula(0),
frankCopula(1), gumbelCopula(1), VC2copula::joeBiCopula(),
cqsCopula(), asCopula())
loglikTau <- list()
#This lines which produce the error:
for(j in 1:3) { # j <-1
tmpBins <- list(meanDists=stBins$meanDists,
lagData=lapply(stBins$lagData, function(x) x[,c(2*j-1,2*j)]))
loglikTau[[j]] <- loglikByCopulasLags(tmpBins, families, calcKTau[[j]])
}
bestFitTau <- list()
bestFitTau[[1]] <- apply(apply(loglikTau[[1]]$loglik[,-8], 1, rank),
2, which.max)
bestFitTau[[2]] <- apply(apply(loglikTau[[2]]$loglik, 1, rank),
2, which.max)
bestFitTau[[3]] <- apply(apply(loglikTau[[3]]$loglik, 1, rank),
2, which.max)
# gather the fitted copulas and representative distances
listCops <- NULL
for(t.level in 1:3) {
listCops[[t.level]] <- sapply(1:35,
function(i) {
tmpTau <- calcKTau[[t.level]](stBins$meanDists[i])
if (tmpTau < 0.05) {
return(indepCopula(dim=2))
} else {
return(loglikTau[[t.level]]$copulas[[bestFitTau[[t.level]][i]]][[i]])
}})
}
listDists <- NULL
listDists[[1]] <- stBins$meanDists[1:35]
listDists[[2]] <- stBins$meanDists[1:35]
listDists[[3]] <- stBins$meanDists[1:35]
# build the spatio-temporal copula
stConvCop <- stCopula(components=listCops, distances=listDists,
t.lags=c(0,-1,-2))
# get the neighbours
stNeigh <- getStNeighbours(EU_RB_2005, var="rtPM10", spSize=4,
t.lags=-(0:2), timeSteps=10, min.dist=10)
stVineFit <- fitCopula(stVineCopula(stConvCop,vineCopula(9L)), stNeigh,
method="indeptest")
stVine <- stVineFit@copula
# retrieve the log-likelihood
stVineFit@loglik`