% funtction ols provides the most basic output of linear regression analysis: % i.e. the beta coef, std-errors, t-values, R^2 and sig^2. Since we need % OLS repeatedly, feel free to extend this program by other parameters of % intererest, such as F-value, Durbin-Watson or AIC. function [out,var,rsq,sigs]=ols(y,x); % the lhs must be in the same order as the lhs in the main file test.m [n,k]=size(x); xxi=inv(x'*x); beta=xxi*(x'*y); eps=y-x*beta; sigs=eps'*eps/(n-k); var=xxi*sigs; se=sqrt(diag(var)); % out is a k x 3 matrix with the first column all beta coef, second std-errors and third the t-values out=[beta,se,beta./se]; dy=y-mean(y); rsq=1-(eps'*eps)/(dy'*dy);