Skip to content

Commit

Permalink
Fix OneStepAhead RCPP prediction.
Browse files Browse the repository at this point in the history
  • Loading branch information
manuhuth committed Dec 4, 2024
1 parent e531d05 commit e2263cf
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export(cocoReg)
export(cocoResid)
export(cocoScore)
export(cocoSim)
export(cocoSim_cov)
export(cocoSoc)
export(dGP1)
export(installJuliaPackages)
Expand Down
15 changes: 11 additions & 4 deletions R/cocoForecastKStepsRCPP.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@ cocoForecastKStepsRCPP <- function(fit, k=1, number_simulations=500, covariates=
}

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)
#cannot be cocosim as the init argument is not available in cocoSim
if (!is.null(covariates)){
output <- cocoSim_cov(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
} else {
output <- cocoSim_base(fit$type, order = fit$order, par = fit$par,
size= k+length(init),
seasonality = c(1, 2), init = init)$data
}
}

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)[1, ])
out_matrix <- matrix(sapply(1:number_simulations, run_sim), ncol=1)
}

freq_table <- function(i){
Expand Down
2 changes: 1 addition & 1 deletion R/cocoForecastOneStep.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ cocoForecastOneStep <- function(coco, max=NULL, epsilon=1e-12, xcast=NULL,

if (!is.null(coco$julia_reg) & julia){
addJuliaFunctions()
if ((k == 1) & (is.matrix(xcast))) {
if ((is.matrix(xcast))) {
xcast <- c(xcast)
}
coco_forecast <- JuliaConnectoR::juliaGet( JuliaConnectoR::juliaCall("cocoPredictOneStep",
Expand Down
1 change: 1 addition & 0 deletions R/cocoSim_cov.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#' @export
cocoSim_cov <- function(type, order, par, size, xreg, seasonality = c(1, 2), init = NULL, link_function="log") {

if (length(seasonality == 1)) {
Expand Down
4 changes: 2 additions & 2 deletions src/cfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ std::vector<int> simGP1cov(double sumlimit, double alpha, double eta,
int y = data[t-seas-1];

NumericVector covar = xreg(t-1-N,_);
double mathelp = innerProduct(covar,lambdas);
double mathelp = innerProduct(covar, lambdas);
// Apply the link function
lambda = applyLinkFunction(mathelp, link_function);
double psi = eta*(1-alpha)/lambda;
Expand All @@ -494,7 +494,7 @@ std::vector<int> simGP1cov(double sumlimit, double alpha, double eta,
r = r+1;

} //end r
data[t-1] = r-1 + innovations[t-1];
data[t-1] = r-1 + innovations[t-N-1];

} // end tasdsa

Expand Down

0 comments on commit e2263cf

Please sign in to comment.