R/predict.R

Defines functions predict_mpredcc

Documented in predict_mpredcc

#' Prediction for the Mean and Predictor Covariance-Connected (MPREDCC)
#' regression model; iid or autoregressive structures supported.
#'
#' @param X_new The observed X values for which to predict Y.
#' @param Beta The coefficient for X in the distribution of Y | X,
#' equal to Psi x alpha.
#' @param mu_Y Mean parameter for responses.
#' @param a Vector of autoregressive parameters for the response, first element 
#' corresponds to first lag.
#' @param Y_rec Vector of the most recently observed responses,
#' needed to forecast if there is autoregressive structure in the response. 
#' @return Vector with predicted responses (Y_hat).
#' @export
#'                
predict_mpredcc <- function(X_new, Beta, mu_Y = 0, a = NULL, Y_rec = NULL)
{
  if(is.null(a)){
    Y_hat <- X_new %*% Beta + mu_Y
  } else{
    if(is.null(Y_rec)) stop("Need recently observed responses for autoregressive forecast.")
    if(length(a) != length(Y_rec)) stop("Length of a and Y_rec must be the same.")
    n_new <- nrow(X_new)
    q_Y <- length(a)
    Y <- numeric(n_new + q_Y)
    Y[1:q_Y] <- Y_rec
    for(ii in 1:n_new){
      Y[ii + q_Y] <- mu_Y + X_new[ii, ] %*% Beta + sum(a * Y[(ii + q_Y - 1):ii])
    }
    Y_hat <- Y[-c(1:q_Y)]
  }
  return(Y_hat)
}
koekvall/mpredcc documentation built on Nov. 4, 2019, 3:54 p.m.