0

When running the code below, the function "gdp" does not solve for a few estimations, which is fine, but the loop stops at the first error. What do I need to include in the code so that the loop continues to conclusion even if there is an estimation error in one of the included functions?

for(i in mars){
for(j in 1:5){
    negRets <- -table1.matrix[,j]
    tauSigma <- gpd(negRets,threshold=i)
    tau <- tauSigma$par.ests[1]
    sigma <- tauSigma$par.ests[2]
    #cat("For ", reitPort[j], "portfolio and MAR=", i, "the parameter estimats are:", "\n")
    #cat("Tau= ", tau, "Sigma= ", sigma, "\n")
    exceedence.vector <- sort(subset(negRets, negRets > i))
    returns.sorted <- sort(negRets)
    pdf(paste("C:\\Users\\John Broussard\\Dropbox\\evtHandbookProject\\figuresTables\\mar_",i,"_",reitPort[j],".pdf",sep=""))
        plot(returns.sorted, dgpd(returns.sorted, xi = tau, beta = sigma), type ="l", col="blue", ylim=c(0,90))
        title(main=cat("Tau= ", tau, " Sigma= ", sigma), ylab=" ")
    dev.off()
    #print(j)
    #print(i)
}
}

Thanks

4

1 回答 1

0

您可以使用 try catch 并且不对错误执行任何操作。

for(i in mars){
for(j in 1:5){
    tryCatch({
        negRets <- -table1.matrix[,j]
        tauSigma <- gpd(negRets,threshold=i)
        tau <- tauSigma$par.ests[1]
        sigma <- tauSigma$par.ests[2]
        #cat("For ", reitPort[j], "portfolio and MAR=", i, "the parameter estimats are:", "\n")
        #cat("Tau= ", tau, "Sigma= ", sigma, "\n")
        exceedence.vector <- sort(subset(negRets, negRets > i))
        returns.sorted <- sort(negRets)
        pdf(paste("C:\\Users\\John Broussard\\Dropbox\\evtHandbookProject\\figuresTables\\mar_",i,"_",reitPort[j],".pdf",sep=""))
            plot(returns.sorted, dgpd(returns.sorted, xi = tau, beta = sigma), type ="l", col="blue", ylim=c(0,90))
            title(main=cat("Tau= ", tau, " Sigma= ", sigma), ylab=" ")
        dev.off()
        #print(j)
        #print(i)
    }, error = function(e) {

    })
}
}

参考 http://mazamascience.com/WorkingWithData/?p=912

于 2013-08-01T22:52:44.353 回答