Define a measure of discrepancy between the data and the mathematical model
Find the values of β that make Xβ “closest” to y
Visual
Classic example argminβn∑i=1(yi−x′iβ)2 or in matrix form argminβ(y−Xβ)′(y−Xβ) which results in ^β=(X′X)−1X′y
Three ways to do it in program R
Using scalar calculus and algebra (kind of)
y <-c(0.16,2.82,2.24)x <-c(1,2,3)y.bar <-mean(y)x.bar <-mean(x)# Estimate the slope parameterbeta1.hat <-sum((x-x.bar)*(y-y.bar))/sum((x-x.bar)^2)beta1.hat
## [1] 1.04
# Estimate the intercept parameterbeta0.hat <- y.bar -sum((x-x.bar)*(y-y.bar))/sum((x-x.bar)^2)*x.barbeta0.hat
## [1] -0.34
Using matrix calculus and algebra
y <-c(0.16,2.82,2.24)X <-matrix(c(1,1,1,1,2,3),nrow=3,ncol=2,byrow=FALSE)solve(t(X)%*%X)%*%t(X)%*%y
## [,1]
## [1,] -0.34
## [2,] 1.04
Using modern (circa 1970’s) optimization techniques
y <-c(0.16,2.82,2.24)x <-c(1,2,3)optim(par=c(0,0),method =c("Nelder-Mead"),fn=function(beta){sum((y-(beta[1]+beta[2]*x))^2)})