Skip to content

Commit

Permalink
Fix coco.predict.
Browse files Browse the repository at this point in the history
  • Loading branch information
manuhuth committed Dec 1, 2024
1 parent 85f2d90 commit 25871f3
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 30 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export(cocoResid)
export(cocoScore)
export(cocoSim)
export(cocoSoc)
export(dGP1)
export(installJuliaPackages)
export(setJuliaSeed)
importFrom(Rcpp,sourceCpp)
Expand Down
9 changes: 9 additions & 0 deletions R/cocoForecastKSteps.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ cocoForecastKSteps <- function(fit, k=3, number_simulations=1000, alpha=0.05, co
decimals=4,
julia=FALSE){
if (julia){

forecasts <- cocoForecastKStepsJulia(fit, k=k, number_simulations = number_simulations,
covariates=covariates)

} else {
forecasts <- cocoForecastKStepsRCPP(fit, k=k, number_simulations = number_simulations,
covariates=covariates)
Expand All @@ -13,6 +15,13 @@ cocoForecastKSteps <- function(fit, k=3, number_simulations=1000, alpha=0.05, co
densities <- forecasts[[i]][,"frequency"]
x <- as.numeric(forecasts[[i]][,"value"])

if (min(x) > 0){
x_fill <- 0:(min(x)-1)
dens <- rep(0, (min(x)))
x <- c(x_fill, x)
densities <- c(dens, densities)
}

mode <- match(max(densities), densities) - 1
distribution_function <- cumsum(densities)
median <- min(which(distribution_function >= 0.5)) - 1
Expand Down
24 changes: 9 additions & 15 deletions R/cocoForecastKStepsRCPP.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,24 @@ cocoForecastKStepsRCPP <- function(fit, k=1, number_simulations=500, covariates=
init <- fit$ts[(length(fit$ts)-1):length(fit$ts)]
}

if (is.null(covariates)){
run_sim <- function(i){
cocoSim_base(
type = fit$type, order = fit$order, par = fit$par, size = k+length(init),
seasonality = c(1,2), init = init)$data
}
} else {
run_sim <- function(i){
output <- cocoSim_cov(
type = fit$type, order = fit$order, par = fit$par, size = k+length(init),
xreg = covariates,
seasonality = c(1,2), init = init, link_function=fit$link_function)$data
}
run_sim <- function(i){
output <- cocoSim(type = fit$type, order = fit$order, par = fit$par, length = k+length(init),
xreg = covariates,
julia=FALSE, link_function=fit$link_function)
}

if (k > 1) {
out_matrix <- t(sapply(1:number_simulations, run_sim))
} else if (k==1){
out_matrix <- as.matrix(sapply(1:number_simulations, run_sim))
out_matrix <- as.matrix(sapply(1:number_simulations, run_sim)[1, ])
}

freq_table <- function(i){
df <- as.data.frame(table(out_matrix[,i]) / number_simulations)
colnames(df) <- c("value", "frequency")
colnames <- c("value", "frequency")
colnames(df) <- colnames
df["value"] <- as.numeric(levels(df[, "value"]))

return(df)
}

Expand Down
6 changes: 5 additions & 1 deletion R/cocoForecastOneStep.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ cocoForecastOneStep <- function(coco, max=NULL, epsilon=1e-12, xcast=NULL,

if (!is.null(coco$julia_reg) & julia){
addJuliaFunctions()
coco_forecast <- JuliaConnectoR::juliaGet( JuliaConnectoR::juliaCall("cocoPredictOneStep", coco$julia_reg, 0:max_use, xcast))
if ((k == 1) & (is.matrix(xcast))) {
xcast <- c(xcast)
}
coco_forecast <- JuliaConnectoR::juliaGet( JuliaConnectoR::juliaCall("cocoPredictOneStep",
coco$julia_reg, 0:max_use, xcast))
densities <- coco_forecast$values[[4]]
if (is.null(max)){
cumulative <- cumsum(densities)
Expand Down
1 change: 1 addition & 0 deletions R/dGP1.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#' @export
dGP1 <- function(x, y, par) {
if ((round(y) != y) | (y < 0)) {
stop("y must be a non-negative integer")
Expand Down
7 changes: 4 additions & 3 deletions R/plot.cocoForecast.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
#' @export
ggplot2::autoplot
#' @export
autoplot.cocoForecast <- function(object, ...){
autoplot.cocoForecast <- function(object, breaks=NULL, width=0.1, ...){

pl <- ggplot2::ggplot(mapping = ggplot2::aes(x = object$x, y = object$densities_plot)) +
ggplot2::geom_bar(stat="identity", position="dodge", width=0.04) +
ggplot2::geom_bar(stat="identity", position="dodge", width=width) +
ggplot2::labs(title = "Probability mass forecast", x = "Support", y = "Probability mass") +
ggplot2::scale_x_continuous(breaks=object$x) +
ggplot2::xlim(c(0, max(object$x))) +
ggplot2::theme_bw() + ggplot2::theme(text = ggplot2::element_text(size = 20))

pl
}

Expand Down
4 changes: 4 additions & 0 deletions R/predict.coco.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ predict.coco <- function(object, k=1, number_simulations=1000,
decimals = 4,
julia=FALSE,...) {

if ((k == 1) & (!is.matrix(xcast))){
xcast <- matrix(xcast, nrow=1)
}

if ((k==1) & (!simulate_one_step_ahead)){
return(cocoForecastOneStep(object, max=max, epsilon=epsilon, xcast=xcast,
alpha=alpha,
Expand Down
4 changes: 2 additions & 2 deletions man/cocoPit.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions man/cocoReg.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/cocoScore.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/cocoSim.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/predict.coco.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/setJuliaSeed.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 25871f3

Please sign in to comment.