R/predict_from_coefs.R

Defines functions predict_from_coefs

Documented in predict_from_coefs

#' Computes predicted values given coefficients
#'
#' This function takes a data frame of coefficients in the form outputted by
#' functions like \code{multiple_linear_regression} or \code{ridge_regression}.
#'
#' It calculates the predicted values of a response variable from these coefficients.
#'
#' @param dat A data frame
#' @param response The name of a response variable in the data frame (unquoted)
#' @param coefs A data frame of coefficient estimates
#'
#' @return A data frame of true and predicted values
#'
#' @import dplyr
#'
#' @export
predict_from_coefs <- function(dat, response, coefs){

  x <- dat %>% select(-{{response}})
  x <- as.matrix(x)
  betas <- as.matrix(coefs[-1])

  predictions <- x %*% betas
  predictions <- predictions + coefs[1]
  predictions <- as.data.frame(predictions)
  names(predictions) <- "Predictions"

  return(predictions)
}
mknauss58/Lab-6-Group documentation built on May 22, 2022, 12:26 a.m.