R/pred.ridgereg.R

Defines functions pred.ridgereg

Documented in pred.ridgereg

#' Return predictions of a ridgereg object
#' @description \code{pred} returns the predictions of a ridgereg object.
#' @param object A ridgereg object.
#' @param ... Further arguments passed to or from other methods.
#' @param newdata A data frame with new data for predictions.
#' @return Return a named vector with predictions.
#' @examples
#' pred(ridgereg(formula = Petal.Length ~ Species, data = iris, lambda = 0))
#' @export

# pred
pred.ridgereg <- function(object, newdata = NULL, ...){
  if(is.null(newdata)){
    res <- c(object[[5]])
    names(res) <- rownames(object[[5]])
    return(res)
  } else {
    data <- stats::model.frame(object$formula, newdata)
    y <- stats::model.response(data)
    X <- stats::model.matrix(attr(data,"terms"),data)
    preds <- X %*% object$beta_hat
    res <- c(preds)
    names(res) <- rownames(preds)
    return(res)
  }
}
Sidryd/lab4sidjac documentation built on Oct. 17, 2020, 11:05 p.m.