#' 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)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.