>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 <- 1000
x1 <- 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 %*% b
resid <- y - yhat
s2 <- 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")
res
summary(lm(y ~ X - 1))

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

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
Advertisements
This entry was posted in Econometrics, Numerical methods, Programming, R. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s