Skip to content

Instantly share code, notes, and snippets.

@BioSciEconomist
Created November 1, 2016 19:20
Show Gist options
  • Save BioSciEconomist/528a574864c65a794849428f3a8e39c1 to your computer and use it in GitHub Desktop.
Save BioSciEconomist/528a574864c65a794849428f3a8e39c1 to your computer and use it in GitHub Desktop.
Matrix Algebra for Regression
# to support: http://econometricsense.blogspot.com/2011/11/basic-econometrics-in-r-and-sas.html
#------------------------------------------------------------
# regression with canned lm routine
#------------------------------------------------------------
# read in data manually
x <- c(1,2,3,4,5) # read in x -values
y <- c(3,7,5,11,14) # read in y-values
data1 <- data.frame(x,y) # create data set combining x and y values
# analysis
plot(data1$x, data1$y) # plot data
reg1 <- lm(data1$y~data1$x) # compute regression estimates
summary(reg1) # print regression output
abline(reg1) # plot fitted regression line
#------------------------------------------------------------
# matrix programming based approach
#------------------------------------------------------------
# regression matrices require a column of 1's in order to calculate
# the intercept or constant, create this column of 1's as x0
x0 <- c(1,1,1,1,1) # column of 1's
x1 <- c(1,2,3,4,5) # original x-values
# create the x- matrix of explanatory variables
x <- as.matrix(cbind(x0,x1))
# create the y-matrix of dependent variables
y <- as.matrix(c(3,7,5,11,14))
# estimate b = (X'X)^-1 X'y
b <- solve(t(x)%*%x)%*%t(x)%*%y
print(b) # this gives the intercept and slope - matching exactly
# the results above
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment