# read in some data and do OLS # R BATCH ols1.R ols1.out # read in the data as a matrix dta <- matrix(scan("exampledta.txt"),byrow=T,ncol=4) # define y & X (including column of 1s to X) y <- dta[,1] N <- nrow(dta) X <- cbind( rep(1,N), dta[,2:4]) # compute the OLS estimates bols <- (solve(t(X) %*% X)) %*% (t(X) %*% y) # compute the residuals e <- y - X %*% bols # compute s^2 ssq <- as.numeric(t(e) %*% e)/(nrow(X) - ncol(X)) # compute the variance covariance matrix for the OLS estimates varcovbols <- ssq*(solve(t(X) %*% X)) # compute the standard errors sebols <- sqrt(diag(varcovbols)) # print the coefficients and standard errors print(cbind(bols,sebols),digits=3)