-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegressionAnalysis.m
36 lines (35 loc) · 1.23 KB
/
RegressionAnalysis.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
% init params
N = 1000; % sample size
muX = 12; % params explanatory variable
sigmaX = 2.3;
coeff = 0.8; % regression coefficient
intcept = 4.3; % regression intercept
% simulate explanatory variable
X = normrnd(muX,sigmaX,N,1);
% simulate standard normally distributed innovations
epsilon = randn(N,1);
% calculate Y according to linear model
Y = intcept + coeff*X + epsilon;
%Parameters are estimated on the basis of values simulated.
% because of intercept, expand matrix of explanatory variables
X = [ones(N,1) X];
% OLS estimation
paramsHat = inv(X'*X)*X'*Y; % usual estimation formula
% avoiding single matrix inversion as mlint warning suggests
paramsHat2 = (X'*X)\(X'*Y); % faster way
paramsHat3 = X\Y; % best way
% calculate regression line
xLimits = [floor(min(X(:,2))) ceil(max(X(:,2)))]; % use nearest
% neighbouring integer numbers
grid = xLimits(1):0.1:xLimits(2);
vals = paramsHat(1)+paramsHat(2)*grid;
% plotting data
close
scatter(X(:,2),Y,'.'); % used for visualizing points cloud
% include regression line
hold on;
plot(grid,vals,'LineWidth',2,'Color','r')
set(gca,'xLim',xLimits)
xlabel('regressor variable')
ylabel('dependent variable')
title(['Linear model: estimated beta is ' num2str(paramsHat(2))])