Last active
October 16, 2015 02:30
-
-
Save mikedecr/a683a4bce86ab1face9d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Creates a 3D plot to demonstrate how a combination of OLS parameters minimizes residual sum of squares | |
# set values for X | |
x <- seq(-50, 50, 1) | |
# Y as a function of X with random error | |
y <- NA | |
for (i in 1:length(x)) { | |
y[i] <- 2*x[i] + rnorm(1, 0, 5) | |
} | |
# combine x values in design matrix with a column of 1's for the intercept | |
X <- cbind(1, x) | |
# Define function for RSS | |
RSS <- function(beta0, beta1) { | |
beta <- c(beta0, beta1) | |
# define how to calculate e'e (RSS matrix) | |
ee <- t(y - X %*% beta) %*% (y - X %*% beta) | |
return(ee) | |
} | |
# sequences for "possible" values of the parameters | |
a <- seq(-200,200,10) | |
b <- seq(-4,8,0.5) | |
# get RSS for combinations of possible parameter values | |
rss <- outer(a, b, function(x,y) mapply(RSS, x, y)) | |
# 3D plot of the contour | |
persp(x = a, y = b, z = rss, | |
theta=120, phi=20, | |
col="cyan", border="darkblue", shade=TRUE, | |
ticktype="simple", | |
xlim=c(-200,200), | |
ylim=c(-4,8), | |
ylab="α", | |
xlab="β", | |
zlab="RSS" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment