>R code for OLS

>Just to show the mathematics involved in computing the OLS coefficients on a simulated data set (which is a good approach to really learning econometrics):

set.seed(1234)n <- 1000x1 <- rnorm(n)x2 <- runif(n)X <- cbind(1, x1, x2)k <- ncol(X)beta <- c(2, 4, 6)y <- X %*% beta + rnorm(n)XtX <- crossprod(X)Xty <- crossprod(X, y)b <- solve(XtX, Xty)yhat <- X %*% bresid <- y - yhats2 <- sum(resid^2)/(n - k)sigma.hat <- s2 * solve(XtX)se <- sqrt(diag(sigma.hat))res <- cbind(b, se)colnames(res) <- c("b", "se")rownames(res) <- c("const.", "x1", "x2")ressummary(lm(y ~ X - 1))

Note how the code makes use of efficient matrix algebra on computing the OLS coefficients:

crossprod(X)

for

t(X) %*% X
crossprod(X, y)

for

t(X) %*% y

and especially,

solve(XtX, Xty)

which is a numerically stable method for solving a system of linear equations, instead of the standard textbook OLS computation $$\beta_{\text{OLS}} = (X’ X)^{-1} X’ y$$, translated to R code as

solve(t(X) %*% X) %*% t(X) %*% y