https://github.com/cran/ensembleBMA
Tip revision: f80c44bfcea4b355579f751ff30ba3c239b90084 authored by Chris Fraley on 23 January 2009, 00:00:00 UTC
version 4.1.1
version 4.1.1
Tip revision: f80c44b
quantileForecast.ensembleBMAnormal.R
`quantileForecast.ensembleBMAnormal` <-
function(fit, ensembleData, quantiles = 0.5, dates = NULL, ...)
{
weps <- 1.e-4
matchITandFH(fit,ensembleData)
M <- matchEnsembleMembers(fit,ensembleData)
nForecasts <- ensembleSize(ensembleData)
if (!all(M == 1:nForecasts)) ensembleData <- ensembleData[,M]
## remove instances missing all forecasts
M <- apply(ensembleForecasts(ensembleData), 1, function(z) all(is.na(z)))
ensembleData <- ensembleData[!M,]
## match specified dates with dateTable in fit
dateTable <- dimnames(fit$weights)[[2]]
if (!is.null(dates)) {
dates <- sort(unique(as.character(dates)))
if (length(dates) > length(dateTable))
stop("parameters not available for some dates")
K <- match( dates, dateTable, nomatch=0)
if (any(!K) || !length(K))
stop("parameters not available for some dates")
}
else {
dates <- dateTable
K <- 1:length(dateTable)
}
ensDates <- ensembleValidDates(ensembleData)
## match dates in data with dateTable
if (is.null(ensDates) || all(is.na(ensDates))) {
if (length(dates) > 1) stop("date ambiguity")
nObs <- nrow(ensembleData)
Dates <- rep( dates, nObs)
}
else {
## remove instances missing dates
if (any(M <- is.na(ensDates))) {
ensembleData <- ensembleData[!M,]
ensDates <- ensembleValidDates(ensembleData)
}
Dates <- as.character(ensDates)
L <- as.logical(match( Dates, dates, nomatch=0))
if (all(!L) || !length(L))
stop("model fit dates incompatible with ensemble data")
Dates <- Dates[L]
ensembleData <- ensembleData[L,]
nObs <- length(Dates)
}
nForecasts <- ensembleSize(ensembleData)
Q <- matrix(NA, nObs, length(quantiles))
dimnames(Q) <- list(ensembleObsLabels(ensembleData),as.character(quantiles))
ensembleData <- ensembleForecasts(ensembleData)
l <- 0
for (d in dates) {
l <- l + 1
k <- K[l]
WEIGHTS <- fit$weights[,k]
if (all(Wmiss <- is.na(WEIGHTS))) next
SD <- if (!is.null(dim(fit$sd))) fit$sd[,k] else rep(fit$sd[k], nForecasts)
I <- which(as.logical(match(Dates, d, nomatch = 0)))
for (i in I) {
f <- ensembleData[i,]
M <- is.na(f) | Wmiss
MEAN <- apply(rbind(1, f)*fit$biasCoefs[,,k], 2, sum)
W <- WEIGHTS
if (any(M)) {
W <- W + weps
W <- W[!M]/sum(W[!M])
}
Q[i,] <- sapply(quantiles, quantBMAnormal,
WEIGHTS=W, MEAN=MEAN[!M], SD=SD[!M])
}
}
Q
}