# TODO: Add comment
# 
# Author: E.Korsching 10.9.2006
###############################################################################



linfit.ssq <- function(x,y){
	# linear fit based on least square estimation
	# ssq in y -- therefore x, y can not be swapped, results would be different
	# ei : distance from the fit line to the real value - seen from the fit line : minus to the bottom, plus to the top
	xbar <- mean(x)
	ybar <- mean(y)
	
	b <- sum((x-xbar)*(y-ybar))/sum((x-xbar)^2)			# estimator for b (slope)
	a <- ybar-b*xbar									# estimator for a (intercept)
	ei <- y-a-b*x										# estimator for the theoretical residuals
	return(sum(ei^2))	#ssq
}


#linfit.ssq(c(1,2,3), c(1.9,1.8,2.6))		# 0.135



