R/linear_regression.R

Defines functions linear_reg

Documented in linear_reg

#' Basic linear regression
#'
#' @param y a vector of outcomes
#' @param x a matrix of covariates
#'
#' @return a matrix of the corresponding betas
#' @export
#'
#' @examples
#' x1 <- mtcars$disp
#' x2 <- mtcars$hp
#' x <- cbind(x1, x2)
#' y <- mtcars$mpg

#' lm(mpg ~ disp + hp, data = mtcars)
#' linear_reg(y, x)
linear_reg <- function(y, x) {
  n <- length(y)
  p <- ncol(x) + 1
  X <- matrix(nrow = n, ncol = p)
  Y <- matrix(y, ncol = 1)

  X[, 1] <- 1
  for (i in 1:ncol(x)) {
    X[, i + 1] <- x[, i]
  }

  XTX <- t(X) %*% X
  XTy <- t(X) %*% y

  iXTX <- MASS::ginv(XTX)
  beta <- iXTX %*% XTy
  return(beta)
}
nt-williams/knowledge documentation built on Jan. 26, 2020, 1:08 a.m.