Label your data as "X" and "y." The data in matrix form is "X," whereas the output in vector form is "y."
Set up the residual sum of squares function. Introduce a new vector of variables, "beta." This vector represents the coefficients of the linear regression function. The residual sum of squares function is RSS(beta) = t(y - Xbeta)(y - Xbeta), where the function "t()" is the transpose function, which gives the transpose of a matrix (switching columns for rows).
Take the first derivative with respect to "beta" of the residual sum of squares function. Use standard matrix calculus. The solution is always -2t(X)(y-Xbeta).
Set the derivative equal to zero. You will yield the equation -2t(X)(y-Xbeta) = 0. Notice that the -2 goes away when dividing both sides by -2, leaving t(X)(y-Xbeta) = 0.
Solve the equation for beta. Matrix algebra reveals that the solution is beta = inv[t(X)X]t(X)y, where the function "inv()" is the function that gives the inverse of a matrix. Writing beta in this way allows you to compute a number for it. Call this number "betahat."
Write the linear regression equation. The linear regression equations is y = Xbetahat. In this equation "X" is not your data matrix, but a matrix of variables. Using new data or estimates for X can yield linear regression estimates.