2
0
Fork 0
CVE/CVE_C/R/predict.R

63 lines
1.6 KiB
R

#' Predict method for CVE Fits.
#'
#' Predict responces using reduced data with \code{\link{mars}}.
#'
#' @param object instance of class \code{cve} (result of \code{cve},
#' \code{cve.call}).
#' @param newdata Matrix of the new data to be predicted.
#' @param dim dimension of SDR space to be used for data projecition.
#' @param ... further arguments passed to \code{\link{mars}}.
#'
#' @return prediced response of data \code{newdata}.
#'
#' @examples
#' # create B for simulation
#' B <- rep(1, 5) / sqrt(5)
#'
#' set.seed(21)
#' # creat predictor data x ~ N(0, I_p)
#' x <- matrix(rnorm(500), 100)
#'
#' # simulate response variable
#' # y = f(B'x) + err
#' # with f(x1) = x1 and err ~ N(0, 0.25^2)
#' y <- x %*% B + 0.25 * rnorm(100)
#'
#' x.train <- x[1:80, ]
#' x.test <- x[81:100, ]
#' y.train <- y[1:80, ]
#' y.test <- y[81:100, ]
#'
#' # calculate cve with method 'simple' for k = 1
#' cve.obj.simple <- cve(y.train ~ x.train, k = 1)
#'
#' # predict y.test from x.test
#' yhat <- predict(cve.obj.simple, x.test, 1)
#'
#' # plot prediction against y.test
#' plot(yhat, y.test)
#' @seealso \code{\link{cve}}, \code{\link{cve.call}} or \pkg{\link{mars}}.
#'
#' @rdname predict.cve
#'
#' @importFrom mda mars
#' @method predict cve
#' @export
predict.cve <- function(object, newdata, dim, ...) {
if (missing(newdata)) {
stop("No data supplied.")
}
if (missing(dim)) {
stop("No dimension supplied.")
}
if (!is.matrix(newdata)) {
newdata <- matrix(newdata, nrow = 1L)
}
B <- object$res[[as.character(dim)]]$B
model <- mda::mars(object$X %*% B, object$Y)
predict(model, newdata %*% B)
}